Skip to content

Commit 83d1f15

Browse files
committed
Move vehicle debug from 'Update' to 'ToolUpdate'
1 parent cdfc8ca commit 83d1f15

5 files changed

Lines changed: 99 additions & 42 deletions

File tree

‎src/engine/Component/Physics/Constraint/Vehicle/VehicleComponent.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace vg::engine
4646

4747
void FixedUpdate (const Context & _context) final override;
4848
void Update (const Context & _context) final override;
49+
void ToolUpdate (const Context & _context) final override;
4950

5051
bool StartEngine () final override;
5152
bool StopEngine () final override;

‎src/engine/Component/Physics/Constraint/Vehicle/VehicleComponent.hpp‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#include "VehicleComponent.h"
22
#include "renderer/IRenderer.h"
3-
#include "renderer/IDebugDraw.h"
43
#include "engine/ISoundComponent.h"
5-
#include "engine/Component/Renderer/Instance/Mesh/MeshComponent.h"
64
#include "engine/IParticleComponent.h"
5+
#include "engine/Component/Renderer/Instance/Mesh/MeshComponent.h"
76

87
using namespace vg::core;
98

@@ -115,7 +114,7 @@ namespace vg::engine
115114
if (m_vehicleConstraint == nullptr)
116115
createVehicleConstraint();
117116

118-
EnableUpdateFlags(UpdateFlags::FixedUpdate | UpdateFlags::Update);
117+
SetUpdateFlags(UpdateFlags::FixedUpdate | UpdateFlags::Update | UpdateFlags::ToolUpdate);
119118
}
120119

121120
//--------------------------------------------------------------------------------------
@@ -280,7 +279,7 @@ namespace vg::engine
280279
void VehicleComponent::FixedUpdate(const Context & _context)
281280
{
282281
if (m_vehicleConstraint)
283-
m_vehicleConstraint->FixedUpdate(m_driveState);
282+
m_vehicleConstraint->UpdateDriveState(m_driveState);
284283
}
285284

286285
//--------------------------------------------------------------------------------------
@@ -345,6 +344,13 @@ namespace vg::engine
345344
}
346345
}
347346

347+
//--------------------------------------------------------------------------------------
348+
void VehicleComponent::ToolUpdate(const Context & _context)
349+
{
350+
if (m_vehicleConstraint)
351+
m_vehicleConstraint->DrawDebug();
352+
}
353+
348354
//--------------------------------------------------------------------------------------
349355
bool VehicleComponent::EnterVehicle(core::IGameObject * _owner, core::uint & _slotIndex)
350356
{

‎src/physics/Constraint/Vehicle/VehicleConstraint.cpp‎

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ namespace vg::physics
348348
}
349349

350350
//--------------------------------------------------------------------------------------
351-
void VehicleConstraint::FixedUpdate(const DriveState & _driveState)
351+
void VehicleConstraint::UpdateDriveState(const DriveState & _driveState)
352352
{
353353
if (m_joltVehicleConstraint)
354354
{
@@ -430,10 +430,80 @@ namespace vg::physics
430430
m_joltVehicleConstraint->SetVehicleCollisionTester(m_joltVehicleCollisionTester);
431431
}
432432

433-
//--------------------------------------------------------------------------------------
434-
// TODO: move debug display to tool update pass
435433
//--------------------------------------------------------------------------------------
436434
void VehicleConstraint::updateVisuals()
435+
{
436+
if (m_joltVehicleConstraint)
437+
{
438+
switch (m_vehicleConstraintDesc->GetVehicleType())
439+
{
440+
default:
441+
{
442+
VG_ASSERT_ENUM_NOT_IMPLEMENTED(m_vehicleConstraintDesc->GetVehicleType());
443+
}
444+
break;
445+
446+
case VehicleType::Car:
447+
{
448+
const CarConstraintDesc * desc = VG_SAFE_STATIC_CAST(const CarConstraintDesc, m_vehicleConstraintDesc);
449+
450+
const uint wheelCount = desc->GetWheelCount();
451+
452+
for (uint i = 0; i < wheelCount; ++i)
453+
{
454+
const JPH::Wheel * wheel = m_joltVehicleConstraint->GetWheel(i);
455+
const JPH::WheelSettings * wheelSettings = wheel->GetSettings();
456+
457+
const float radius = wheelSettings->mRadius;
458+
const float width = wheelSettings->mWidth;
459+
460+
// transform the wheel visual
461+
const TwoWheeledAxleDesc & axle = (i < 2) ? desc->m_front : desc->m_rear;
462+
const ObjectHandle & handle = (i & 1) ? axle.m_rightWheel : axle.m_leftWheel;
463+
464+
IObject * obj = handle.getObject();
465+
if (IGameObject * wheelGameobject = VG_SAFE_STATIC_CAST(IGameObject, obj))
466+
{
467+
float4x4 wheelMat = GetWheelMatrix(i);
468+
wheelGameobject->SetGlobalMatrix(wheelMat);
469+
}
470+
}
471+
}
472+
break;
473+
474+
case VehicleType::Bike:
475+
{
476+
const BikeConstraintDesc * desc = VG_SAFE_STATIC_CAST(const BikeConstraintDesc, m_vehicleConstraintDesc);
477+
478+
const uint wheelCount = desc->GetWheelCount();
479+
480+
for (uint i = 0; i < wheelCount; ++i)
481+
{
482+
const JPH::Wheel * wheel = m_joltVehicleConstraint->GetWheel(i);
483+
const JPH::WheelSettings * wheelSettings = wheel->GetSettings();
484+
485+
const float radius = wheelSettings->mRadius;
486+
const float width = wheelSettings->mWidth;
487+
488+
// transform the wheel visual
489+
const OneWheeledAxleDesc & axle = (i == 0) ? desc->m_front : desc->m_rear;
490+
const ObjectHandle & handle = axle.m_wheel;
491+
492+
IObject * obj = handle.getObject();
493+
if (IGameObject * wheelGameobject = VG_SAFE_STATIC_CAST(IGameObject, obj))
494+
{
495+
float4x4 wheelMat = GetWheelMatrix(i);
496+
wheelGameobject->SetGlobalMatrix(wheelMat);
497+
}
498+
}
499+
}
500+
break;
501+
}
502+
}
503+
}
504+
505+
//--------------------------------------------------------------------------------------
506+
void VehicleConstraint::DrawDebug()
437507
{
438508
// debug
439509
renderer::IDebugDraw * debugDraw = Physics::get()->getDebugDraw();
@@ -472,21 +542,10 @@ namespace vg::physics
472542
if (0 == (i & 1))
473543
{
474544
float3 leftWheelPos = wheelMatDebug[3].xyz;
475-
float3 rightWheelPos = fromJoltVec3(m_joltVehicleConstraint->GetWheelWorldTransform(i+1, JPH::Vec3::sAxisZ(), JPH::Vec3::sAxisY()).GetTranslation());
545+
float3 rightWheelPos = fromJoltVec3(m_joltVehicleConstraint->GetWheelWorldTransform(i + 1, JPH::Vec3::sAxisZ(), JPH::Vec3::sAxisY()).GetTranslation());
476546
debugDraw->AddLine(m_world, leftWheelPos, rightWheelPos, 0xFF0000FF);
477547
}
478548
}
479-
480-
// transform the wheel visual
481-
const TwoWheeledAxleDesc & axle = (i < 2) ? desc->m_front : desc->m_rear;
482-
const ObjectHandle & handle = (i & 1) ? axle.m_rightWheel : axle.m_leftWheel;
483-
484-
IObject * obj = handle.getObject();
485-
if (IGameObject * wheelGameobject = VG_SAFE_STATIC_CAST(IGameObject, obj))
486-
{
487-
float4x4 wheelMat = GetWheelMatrix(i);
488-
wheelGameobject->SetGlobalMatrix(wheelMat);
489-
}
490549
}
491550
}
492551
break;
@@ -512,17 +571,6 @@ namespace vg::physics
512571
float4x4 wheelMatDebug = fromJoltMatrix(m_joltVehicleConstraint->GetWheelWorldTransform(i, JPH::Vec3::sAxisZ(), JPH::Vec3::sAxisY()));
513572
debugDraw->AddWireframeCylinder(m_world, radius, width, 0xFF0000FF, wheelMatDebug);
514573
}
515-
516-
// transform the wheel visual
517-
const OneWheeledAxleDesc & axle = (i == 0) ? desc->m_front : desc->m_rear;
518-
const ObjectHandle & handle = axle.m_wheel;
519-
520-
IObject * obj = handle.getObject();
521-
if (IGameObject * wheelGameobject = VG_SAFE_STATIC_CAST(IGameObject, obj))
522-
{
523-
float4x4 wheelMat = GetWheelMatrix(i);
524-
wheelGameobject->SetGlobalMatrix(wheelMat);
525-
}
526574
}
527575
}
528576
break;

‎src/physics/Constraint/Vehicle/VehicleConstraint.h‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@ namespace vg::physics
2828
VehicleConstraint(Body * _body, const VehicleConstraintDesc * _vehicleConstraintDesc, const core::string & _name, core::IObject * _parent);
2929
~VehicleConstraint();
3030

31-
void FixedUpdate (const DriveState & _driveState) final override;
31+
void UpdateDriveState (const DriveState & _driveState) final override;
32+
void DrawDebug () final override;
3233

33-
float GetEngineRPM () const final override;
34-
core::uint GetCurrentGear () const final override;
34+
float GetEngineRPM () const final override;
35+
core::uint GetCurrentGear () const final override;
3536

36-
core::float4x4 GetWheelMatrix (core::uint _index) const final override;
37-
void Reset () final override;
37+
core::float4x4 GetWheelMatrix (core::uint _index) const final override;
38+
void Reset () final override;
3839

3940
private:
40-
void updateDrive (const DriveState & _driveState);
41-
void updateVisuals ();
41+
void updateDrive (const DriveState & _driveState);
42+
void updateVisuals ();
4243

4344
private:
4445
VehicleConstraintDesc * m_vehicleConstraintDesc = nullptr;

‎src/physics/IVehicleConstraint.h‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ namespace vg::physics
2727
public:
2828
VG_CLASS_DECL_ABSTRACT(IVehicleConstraint, core::Object)
2929

30-
virtual void FixedUpdate (const DriveState & _driveState) = 0;
30+
virtual void UpdateDriveState (const DriveState & _driveState) = 0;
31+
virtual void DrawDebug () = 0;
3132

32-
virtual float GetEngineRPM () const = 0;
33-
virtual core::uint GetCurrentGear () const = 0;
33+
virtual float GetEngineRPM () const = 0;
34+
virtual core::uint GetCurrentGear () const = 0;
3435

35-
virtual core::float4x4 GetWheelMatrix (core::uint _index) const = 0;
36-
virtual void Reset () = 0;
36+
virtual core::float4x4 GetWheelMatrix (core::uint _index) const = 0;
37+
virtual void Reset () = 0;
3738
};
3839
}

0 commit comments

Comments
 (0)