Skip to content

Commit

Permalink
Implemented WheeledVehicleController TireMaxImpulseCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
jrouwe committed May 31, 2024
1 parent 74977ab commit efecad1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Examples/vehicle_wheeled.html
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@
physicsSystem.AddConstraint(constraint);
const controller = Jolt.castObject(constraint.GetController(), Jolt.WheeledVehicleController);

// Optional step: Set the vehicle controller callbacks
let controllerCallbacks = new Jolt.WheeledVehicleControllerCallbacksJS();
controllerCallbacks.OnTireMaxImpulseCallback = (wheelIndex, result, suspensionImpulse, longitudinalFriction, lateralFriction, longitudinalSlip, lateralSlip, deltaTime) => {
result = Jolt.wrapPointer(result, Jolt.TireMaxImpulseCallbackResult);
// This is the default calculation which will is pre-filled in result (just repeating it here)
result.mLongitudinalImpulse = longitudinalFriction * suspensionImpulse;
result.mLateralImpulse = lateralFriction * suspensionImpulse;
};
controllerCallbacks.SetWheeledVehicleController(controller);

const modelWheels = [];
for (let i = 0; i < vehicle.mWheels.size(); i++) {
modelWheels.push(createThreeWheel(constraint, i, car));
Expand Down
33 changes: 33 additions & 0 deletions JoltJS.h
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,39 @@ class VehicleConstraintCallbacksEm
virtual void OnPostStepCallback(VehicleConstraint &inVehicle, float inDeltaTime, PhysicsSystem &inPhysicsSystem) = 0;
};

/// The tire max impulse callback returns multiple parameters, so we need to store them in a class
class TireMaxImpulseCallbackResult
{
public:
float mLongitudinalImpulse;
float mLateralImpulse;
};

/// A wrapper around the wheeled vehicle controller callbacks that is compatible with JavaScript
class WheeledVehicleControllerCallbacksEm
{
public:
virtual ~WheeledVehicleControllerCallbacksEm() = default;

void SetWheeledVehicleController(WheeledVehicleController &inController)
{
inController.SetTireMaxImpulseCallback([this](uint inWheelIndex, float &outLongitudinalImpulse, float &outLateralImpulse, float inSuspensionImpulse, float inLongitudinalFriction, float inLateralFriction, float inLongitudinalSlip, float inLateralSlip, float inDeltaTime) {
// Pre-fill the structure with default calculated values
TireMaxImpulseCallbackResult result;
result.mLongitudinalImpulse = inLongitudinalFriction * inSuspensionImpulse;
result.mLateralImpulse = inLateralFriction * inSuspensionImpulse;

OnTireMaxImpulseCallback(inWheelIndex, &result, inSuspensionImpulse, inLongitudinalFriction, inLateralFriction, inLongitudinalSlip, inLateralSlip, inDeltaTime);

// Read the results
outLongitudinalImpulse = result.mLongitudinalImpulse;
outLateralImpulse = result.mLateralImpulse;
});
}

virtual void OnTireMaxImpulseCallback(uint inWheelIndex, TireMaxImpulseCallbackResult *outResult, float inSuspensionImpulse, float inLongitudinalFriction, float inLateralFriction, float inLongitudinalSlip, float inLateralSlip, float inDeltaTime) = 0;
};

class PathConstraintPathEm: public PathConstraintPath
{
public:
Expand Down
15 changes: 15 additions & 0 deletions JoltJS.idl
Original file line number Diff line number Diff line change
Expand Up @@ -3036,6 +3036,21 @@ interface VehicleConstraintCallbacksJS {
void OnPostStepCallback([Ref] VehicleConstraint inVehicle, float inDeltaTime, [Ref] PhysicsSystem inPhysicsSystem);
};

interface TireMaxImpulseCallbackResult {
attribute float mLongitudinalImpulse;
attribute float mLateralImpulse;
};

interface WheeledVehicleControllerCallbacksEm {
void SetWheeledVehicleController([Ref] WheeledVehicleController inController);
};

[JSImplementation="WheeledVehicleControllerCallbacksEm"]
interface WheeledVehicleControllerCallbacksJS {
void WheeledVehicleControllerCallbacksJS();
void OnTireMaxImpulseCallback(unsigned long inWheelIndex, TireMaxImpulseCallbackResult outResult, float inSuspensionImpulse, float inLongitudinalFriction, float inLateralFriction, float inLongitudinalSlip, float inLateralSlip, float inDeltaTime);
};

interface WheelSettings {
void WheelSettings();
unsigned long GetRefCount();
Expand Down

0 comments on commit efecad1

Please sign in to comment.