Skip to content

Commit dd85425

Browse files
committed
Use Jolt's MotorcycleController for bikes
1 parent 5f36027 commit dd85425

2 files changed

Lines changed: 62 additions & 52 deletions

File tree

src/physics/Constraint/Vehicle/VehicleConstraint.cpp

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#pragma push_macro("new")
1414
#undef new
1515
#include "Jolt/Physics/Vehicle/WheeledVehicleController.h"
16+
#include "Jolt/Physics/Vehicle/MotorcycleController.h"
1617
#pragma pop_macro("new")
1718

1819
#include "Jolt/Physics/Vehicle/VehicleConstraint.h"
@@ -206,11 +207,21 @@ namespace vg::physics
206207

207208
jphWheel->mPosition = getJoltVec3(wheelLocalPos);
208209

209-
jphWheel->mSuspensionDirection = JPH::Vec3(0, 0, -1);
210+
if (i == 0)
211+
{
212+
jphWheel->mSteeringAxis = getJoltVec3(m_steeringAxis);
213+
jphWheel->mSuspensionDirection = JPH::Vec3(0, 0, -1);
214+
}
215+
else
216+
{
217+
jphWheel->mSteeringAxis = JPH::Vec3(0, 0, 1);
218+
jphWheel->mSuspensionDirection = JPH::Vec3(0, 0, -1);
219+
}
220+
210221
jphWheel->mWheelUp = JPH::Vec3(0, 0, 1);
211222
jphWheel->mWheelForward = JPH::Vec3(1, 0, 0);
212223

213-
jphWheel->mSteeringAxis = getJoltVec3(m_steeringAxis);
224+
214225
jphWheel->mMaxSteerAngle = isFront ? degreesToRadians(axle.m_maxSteerAngleInDegrees) : 0.0f;
215226

216227
jphWheel->mRadius = axle.m_radius;
@@ -229,14 +240,7 @@ namespace vg::physics
229240
}
230241
}
231242
break;
232-
}
233-
234-
#pragma push_macro("new")
235-
#undef new
236-
auto * controller = new JPH::WheeledVehicleControllerSettings();
237-
#pragma pop_macro("new")
238-
239-
vehicle.mController = controller;
243+
}
240244

241245
switch (_vehicleConstraintDesc->GetVehicleType())
242246
{
@@ -248,21 +252,35 @@ namespace vg::physics
248252

249253
case VehicleType::Bike:
250254
{
255+
#pragma push_macro("new")
256+
#undef new
257+
auto * motorcycleController = new JPH::MotorcycleControllerSettings();
258+
#pragma pop_macro("new")
259+
260+
vehicle.mController = motorcycleController;
261+
251262
const BikeConstraintDesc * desc = VG_SAFE_STATIC_CAST(const BikeConstraintDesc, _vehicleConstraintDesc);
252263

253264
// One "fake" differential driving ONLY the rear wheel (index 1)
254-
controller->mDifferentials.resize(1);
255-
controller->mDifferentials[0].mLeftWheel = 1;
256-
controller->mDifferentials[0].mRightWheel = -1;
257-
controller->mDifferentials[0].mLeftRightSplit = 0.0f;
258-
controller->mDifferentials[0].mEngineTorqueRatio = 1.0f;
265+
motorcycleController->mDifferentials.resize(1);
266+
motorcycleController->mDifferentials[0].mLeftWheel = 1;
267+
motorcycleController->mDifferentials[0].mRightWheel = -1;
268+
motorcycleController->mDifferentials[0].mLeftRightSplit = 0.0f;
269+
motorcycleController->mDifferentials[0].mEngineTorqueRatio = 1.0f;
259270

260271
// No anti-roll bars for bikes (on purpose)
261272
}
262273
break;
263274

264275
case VehicleType::Car:
265276
{
277+
#pragma push_macro("new")
278+
#undef new
279+
auto * controller = new JPH::WheeledVehicleControllerSettings();
280+
#pragma pop_macro("new")
281+
282+
vehicle.mController = controller;
283+
266284
const CarConstraintDesc * desc = VG_SAFE_STATIC_CAST(const CarConstraintDesc, _vehicleConstraintDesc);
267285

268286
if (desc->m_fourWheelDrive)
@@ -379,9 +397,7 @@ namespace vg::physics
379397
JPH::BodyInterface & bodyInterface = m_vehicleBody->getPhysicsWorld()->getBodyInterface();
380398
const auto & bodyID = m_vehicleBody->getBodyID();
381399
bodyInterface.ActivateBody(bodyID);
382-
383-
// To get controller
384-
JPH::WheeledVehicleController * vehicleController = (JPH::WheeledVehicleController *)m_joltVehicleConstraint->GetController();
400+
385401

386402
// Probably does not need to change every frame ...
387403
switch (m_vehicleConstraintDesc->GetVehicleType())
@@ -396,64 +412,58 @@ namespace vg::physics
396412
{
397413
const BikeConstraintDesc * desc = VG_SAFE_STATIC_CAST(const BikeConstraintDesc, m_vehicleConstraintDesc);
398414

399-
auto & engine = vehicleController->GetEngine();
415+
JPH::MotorcycleController *motorcycleController = (JPH::MotorcycleController *)m_joltVehicleConstraint->GetController();
416+
417+
auto & engine = motorcycleController->GetEngine();
400418
engine.mMaxTorque = desc->m_maxEngineTorque;
401419
engine.mMinRPM = desc->m_minRPM;
402420
engine.mMaxRPM = desc->m_maxRPM;
403421

404-
vehicleController->GetTransmission().mClutchStrength = desc->m_clutchStrength;
422+
motorcycleController->GetTransmission().mClutchStrength = desc->m_clutchStrength;
423+
motorcycleController->EnableLeanController(true);
424+
425+
// Set slip ratios to the same for everything
426+
float limited_slip_ratio = sLimitedSlipDifferentials ? 1.4f : FLT_MAX;
427+
motorcycleController->SetDifferentialLimitedSlipRatio(limited_slip_ratio);
428+
for (JPH::VehicleDifferentialSettings & d : motorcycleController->GetDifferentials())
429+
d.mLimitedSlipRatio = limited_slip_ratio;
430+
431+
// Pass the input on to the constraint
432+
motorcycleController->SetDriverInput(_driveState.m_forward, _driveState.m_right, _driveState.m_brake, _driveState.m_handBrake);
405433
}
406434
break;
407435

408436
case VehicleType::Car:
409437
{
410438
const CarConstraintDesc * desc = VG_SAFE_STATIC_CAST(const CarConstraintDesc, m_vehicleConstraintDesc);
411439

440+
JPH::WheeledVehicleController * vehicleController = (JPH::WheeledVehicleController *)m_joltVehicleConstraint->GetController();
441+
412442
auto & engine = vehicleController->GetEngine();
413443
engine.mMaxTorque = desc->m_maxEngineTorque;
414444
engine.mMinRPM = desc->m_minRPM;
415445
engine.mMaxRPM = desc->m_maxRPM;
446+
416447
vehicleController->GetTransmission().mClutchStrength = desc->m_clutchStrength;
448+
449+
// Set slip ratios to the same for everything
450+
float limited_slip_ratio = sLimitedSlipDifferentials ? 1.4f : FLT_MAX;
451+
vehicleController->SetDifferentialLimitedSlipRatio(limited_slip_ratio);
452+
for (JPH::VehicleDifferentialSettings & d : vehicleController->GetDifferentials())
453+
d.mLimitedSlipRatio = limited_slip_ratio;
454+
455+
// Pass the input on to the constraint
456+
vehicleController->SetDriverInput(_driveState.m_forward, _driveState.m_right, _driveState.m_brake, _driveState.m_handBrake);
417457
}
418458
break;
419459
}
420460

421-
// Set slip ratios to the same for everything
422-
float limited_slip_ratio = sLimitedSlipDifferentials ? 1.4f : FLT_MAX;
423-
vehicleController->SetDifferentialLimitedSlipRatio(limited_slip_ratio);
424-
for (JPH::VehicleDifferentialSettings & d : vehicleController->GetDifferentials())
425-
d.mLimitedSlipRatio = limited_slip_ratio;
461+
426462

427-
// Pass the input on to the constraint
428-
vehicleController->SetDriverInput(_driveState.m_forward, _driveState.m_right, _driveState.m_brake, _driveState.m_handBrake);
429463

430464
m_joltVehicleConstraint->SetVehicleCollisionTester(m_joltVehicleCollisionTester);
431465
}
432466

433-
static hlslpp_inline float3x3 rotation_axis2(const float3 & axis, float angle_rad)
434-
{
435-
#if defined(HLSLPP_LAYOUT_COORDINATES_FLIP_SIGN)
436-
angle_rad = -angle_rad;
437-
#endif
438-
439-
// Axis MUST be normalized
440-
const float3 a = normalize(axis);
441-
442-
const float s = sinf(angle_rad);
443-
const float c = cosf(angle_rad);
444-
const float t = 1.0f - c;
445-
446-
const float x = a.x;
447-
const float y = a.y;
448-
const float z = a.z;
449-
450-
return float3x3(
451-
t * x * x + c, t * x * y + s * z, t * x * z - s * y,
452-
t * x * y - s * z, t * y * y + c, t * y * z + s * x,
453-
t * x * z + s * y, t * y * z - s * x, t * z * z + c
454-
);
455-
}
456-
457467
//--------------------------------------------------------------------------------------
458468
void VehicleConstraint::updateVisuals()
459469
{

src/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
#define VG_FRAMEWORK_VERSION_MAJOR 0
44
#define VG_FRAMEWORK_VERSION_MINOR 55
5-
#define VG_FRAMEWORK_VERSION_PATCH 14
5+
#define VG_FRAMEWORK_VERSION_PATCH 15

0 commit comments

Comments
 (0)