Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
WheeledVehicleController.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
13
15
16class PhysicsSystem;
17
20{
22
23public:
26
27 // See: WheelSettings
28 virtual void SaveBinaryState(StreamOut &inStream) const override;
29 virtual void RestoreBinaryState(StreamIn &inStream) override;
30
31 float mInertia = 0.9f;
32 float mAngularDamping = 0.2f;
36 float mMaxBrakeTorque = 1500.0f;
37 float mMaxHandBrakeTorque = 4000.0f;
38};
39
41class JPH_EXPORT WheelWV : public Wheel
42{
43public:
45
47 explicit WheelWV(const WheelSettingsWV &inWheel);
48
51
53 void ApplyTorque(float inTorque, float inDeltaTime)
54 {
55 mAngularVelocity += inTorque * inDeltaTime / GetSettings()->mInertia;
56 }
57
59 void Update(uint inWheelIndex, float inDeltaTime, const VehicleConstraint &inConstraint);
60
61 float mLongitudinalSlip = 0.0f;
62 float mLateralSlip = 0.0f;
65 float mBrakeImpulse = 0.0f;
66};
67
87
90{
91public:
93
96
99
105 void SetDriverInput(float inForward, float inRight, float inBrake, float inHandBrake) { mForwardInput = inForward; mRightInput = inRight; mBrakeInput = inBrake; mHandBrakeInput = inHandBrake; }
106
108 void SetForwardInput(float inForward) { mForwardInput = inForward; }
109 float GetForwardInput() const { return mForwardInput; }
110
112 void SetRightInput(float inRight) { mRightInput = inRight; }
113 float GetRightInput() const { return mRightInput; }
114
116 void SetBrakeInput(float inBrake) { mBrakeInput = inBrake; }
117 float GetBrakeInput() const { return mBrakeInput; }
118
120 void SetHandBrakeInput(float inHandBrake) { mHandBrakeInput = inHandBrake; }
121 float GetHandBrakeInput() const { return mHandBrakeInput; }
122
124 const VehicleEngine & GetEngine() const { return mEngine; }
125
128
131
134
137
140
144
146 float GetWheelSpeedAtClutch() const;
147
149 using TireMaxImpulseCallback = function<void(uint inWheelIndex, float &outLongitudinalImpulse, float &outLateralImpulse, float inSuspensionImpulse, float inLongitudinalFriction, float inLateralFriction, float inLongitudinalSlip, float inLateralSlip, float inDeltaTime)>;
151 void SetTireMaxImpulseCallback(const TireMaxImpulseCallback &inTireMaxImpulseCallback) { mTireMaxImpulseCallback = inTireMaxImpulseCallback; }
152
153#ifdef JPH_DEBUG_RENDERER
155 void SetRPMMeter(Vec3Arg inPosition, float inSize) { mRPMMeterPosition = inPosition; mRPMMeterSize = inSize; }
156#endif // JPH_DEBUG_RENDERER
157
158 // See: VehicleController
159 virtual Ref<VehicleControllerSettings> GetSettings() const override;
160
161protected:
163 void ToSettings(WheeledVehicleControllerSettings &outSettings) const;
164
165 // See: VehicleController
166 virtual Wheel * ConstructWheel(const WheelSettings &inWheel) const override { JPH_ASSERT(IsKindOf(&inWheel, JPH_RTTI(WheelSettingsWV))); return new WheelWV(static_cast<const WheelSettingsWV &>(inWheel)); }
167 virtual bool AllowSleep() const override;
168 virtual void PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
169 virtual void PostCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
170 virtual bool SolveLongitudinalAndLateralConstraints(float inDeltaTime) override;
171 virtual void SaveState(StateRecorder &inStream) const override;
172 virtual void RestoreState(StateRecorder &inStream) override;
173#ifdef JPH_DEBUG_RENDERER
174 virtual void Draw(DebugRenderer *inRenderer) const override;
175#endif // JPH_DEBUG_RENDERER
176
177 // Control information
178 float mForwardInput = 0.0f;
179 float mRightInput = 0.0f;
180 float mBrakeInput = 0.0f;
181 float mHandBrakeInput = 0.0f;
182
183 // Simulation information
188 float mPreviousDeltaTime = 0.0f;
189
190 // Callback that calculates the max impulse that the tire can apply to the ground
192 [](uint, float &outLongitudinalImpulse, float &outLateralImpulse, float inSuspensionImpulse, float inLongitudinalFriction, float inLateralFriction, float, float, float)
193 {
194 outLongitudinalImpulse = inLongitudinalFriction * inSuspensionImpulse;
195 outLateralImpulse = inLateralFriction * inSuspensionImpulse;
196 };
197
198#ifdef JPH_DEBUG_RENDERER
199 // Debug settings
201 float mRPMMeterSize = 0.5f;
202#endif // JPH_DEBUG_RENDERER
203};
204
#define JPH_EXPORT
Definition Core.h:275
unsigned int uint
Definition Core.h:500
#define JPH_NAMESPACE_END
Definition Core.h:425
#define JPH_NAMESPACE_BEGIN
Definition Core.h:419
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
JPH_INLINE constexpr float DegreesToRadians(float inV)
Convert a value from degrees to radians.
Definition Math.h:16
const Vec3 Vec3Arg
Definition MathTypes.h:19
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:50
const DstType * StaticCast(const SrcType *inObject)
Cast inObject to DstType, asserts on failure.
Definition RTTI.h:388
bool IsKindOf(const Type *inObject, const RTTI *inRTTI)
Check if inObject is or is derived from DstType.
Definition RTTI.h:369
#define JPH_RTTI(class_name)
Definition RTTI.h:319
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(linkage, class_name)
Definition SerializableObject.h:109
Definition Array.h:36
Definition DebugRenderer.h:47
Definition LinearCurve.h:17
Definition PhysicsSystem.h:30
Definition Reference.h:107
Definition StateRecorder.h:110
Simple binary input stream.
Definition StreamIn.h:13
Simple binary output stream.
Definition StreamOut.h:13
Definition Vec3.h:17
Definition VehicleConstraint.h:66
Runtime data for interface that controls acceleration / deceleration of the vehicle.
Definition VehicleController.h:41
friend class VehicleConstraint
Definition VehicleController.h:58
JPH_OVERRIDE_NEW_DELETE VehicleController(VehicleConstraint &inConstraint)
Constructor / destructor.
Definition VehicleController.h:46
Basic settings object for interface that controls acceleration / deceleration of the vehicle.
Definition VehicleController.h:25
Runtime data for engine.
Definition VehicleEngine.h:44
Generic properties for a vehicle engine.
Definition VehicleEngine.h:21
Runtime data for transmission.
Definition VehicleTransmission.h:46
Configuration for the transmission of a vehicle (gear box).
Definition VehicleTransmission.h:23
Base class for runtime data for a wheel, each VehicleController can implement a derived class of this...
Definition Wheel.h:46
RefConst< WheelSettings > mSettings
Configuration settings for this wheel.
Definition Wheel.h:123
JPH_OVERRIDE_NEW_DELETE Wheel(const WheelSettings &inSettings)
Constructor / destructor.
Definition Wheel.cpp:66
float mAngularVelocity
Rotation speed of wheel, positive when the wheels cause the vehicle to move forwards (rad/s).
Definition Wheel.h:137
Base class for wheel settings, each VehicleController can implement a derived class of this.
Definition Wheel.h:19
virtual void SaveBinaryState(StreamOut &inStream) const
Saves the contents in binary form to inStream.
Definition Wheel.cpp:32
virtual void RestoreBinaryState(StreamIn &inStream)
Restores the contents in binary form to inStream.
Definition Wheel.cpp:49
WheelSettings object specifically for WheeledVehicleController.
Definition WheeledVehicleController.h:20
float mInertia
Moment of inertia (kg m^2), for a cylinder this would be 0.5 * M * R^2 which is 0....
Definition WheeledVehicleController.h:31
LinearCurve mLateralFriction
On the Y-axis: friction in the sideways direction of the tire. Friction is normally between 0 (no fri...
Definition WheeledVehicleController.h:35
float mMaxBrakeTorque
How much torque (Nm) the brakes can apply to this wheel.
Definition WheeledVehicleController.h:36
float mMaxHandBrakeTorque
How much torque (Nm) the hand brake can apply to this wheel (usually only applied to the rear wheels)...
Definition WheeledVehicleController.h:37
float mAngularDamping
Angular damping factor of the wheel: dw/dt = -c * w. Value should be zero or positive and is usually ...
Definition WheeledVehicleController.h:32
LinearCurve mLongitudinalFriction
On the Y-axis: friction in the forward direction of the tire. Friction is normally between 0 (no fric...
Definition WheeledVehicleController.h:34
WheelSettingsWV()
Constructor.
Definition WheeledVehicleController.cpp:45
float mMaxSteerAngle
How much this wheel can steer (radians).
Definition WheeledVehicleController.h:33
Wheel object specifically for WheeledVehicleController.
Definition WheeledVehicleController.h:42
const WheelSettingsWV * GetSettings() const
Override GetSettings and cast to the correct class.
Definition WheeledVehicleController.h:50
JPH_OVERRIDE_NEW_DELETE WheelWV(const WheelSettingsWV &inWheel)
Constructor.
Definition WheeledVehicleController.cpp:84
float mCombinedLongitudinalFriction
Combined friction coefficient in longitudinal direction (combines terrain and tires).
Definition WheeledVehicleController.h:63
void ApplyTorque(float inTorque, float inDeltaTime)
Apply a torque (N m) to the wheel for a particular delta time.
Definition WheeledVehicleController.h:53
float mLateralSlip
Angular difference (in radians) between ground and wheel relative to ground velocity.
Definition WheeledVehicleController.h:62
float mLongitudinalSlip
Velocity difference between ground and wheel relative to ground velocity.
Definition WheeledVehicleController.h:61
float mCombinedLateralFriction
Combined friction coefficient in lateral direction (combines terrain and tires).
Definition WheeledVehicleController.h:64
float mBrakeImpulse
Amount of impulse that the brakes can apply to the floor (excluding friction).
Definition WheeledVehicleController.h:65
function< void(uint inWheelIndex, float &outLongitudinalImpulse, float &outLateralImpulse, float inSuspensionImpulse, float inLongitudinalFriction, float inLateralFriction, float inLongitudinalSlip, float inLateralSlip, float inDeltaTime)> TireMaxImpulseCallback
Calculate max tire impulses by combining friction, slip, and suspension impulse. Note that the actual...
Definition WheeledVehicleController.h:149
float mHandBrakeInput
Value between 0 and 1 indicating how strong the hand brake is pulled.
Definition WheeledVehicleController.h:181
float mForwardInput
Value between -1 and 1 for auto transmission and value between 0 and 1 indicating desired driving dir...
Definition WheeledVehicleController.h:178
void SetDifferentialLimitedSlipRatio(float inV)
Definition WheeledVehicleController.h:143
void SetForwardInput(float inForward)
Value between -1 and 1 for auto transmission and value between 0 and 1 indicating desired driving dir...
Definition WheeledVehicleController.h:108
VehicleTransmission mTransmission
Transmission state of the vehicle.
Definition WheeledVehicleController.h:185
void SetBrakeInput(float inBrake)
Value between 0 and 1 indicating how strong the brake pedal is pressed.
Definition WheeledVehicleController.h:116
const VehicleTransmission & GetTransmission() const
Get current transmission state.
Definition WheeledVehicleController.h:130
void SetDriverInput(float inForward, float inRight, float inBrake, float inHandBrake)
Definition WheeledVehicleController.h:105
Differentials mDifferentials
Differential states of the vehicle.
Definition WheeledVehicleController.h:186
float GetRightInput() const
Definition WheeledVehicleController.h:113
const VehicleEngine & GetEngine() const
Get current engine state.
Definition WheeledVehicleController.h:124
VehicleEngine mEngine
Engine state of the vehicle.
Definition WheeledVehicleController.h:184
float mRightInput
Value between -1 and 1 indicating desired steering angle.
Definition WheeledVehicleController.h:179
float GetDifferentialLimitedSlipRatio() const
Ratio max / min average wheel speed of each differential (measured at the clutch).
Definition WheeledVehicleController.h:142
void SetRPMMeter(Vec3Arg inPosition, float inSize)
Debug drawing of RPM meter.
Definition WheeledVehicleController.h:155
const Differentials & GetDifferentials() const
Get the differentials this vehicle has.
Definition WheeledVehicleController.h:136
float GetForwardInput() const
Definition WheeledVehicleController.h:109
float GetHandBrakeInput() const
Definition WheeledVehicleController.h:121
void SetHandBrakeInput(float inHandBrake)
Value between 0 and 1 indicating how strong the hand brake is pulled.
Definition WheeledVehicleController.h:120
float mRPMMeterSize
Size of the RPM meter when drawing the constraint.
Definition WheeledVehicleController.h:201
virtual Wheel * ConstructWheel(const WheelSettings &inWheel) const override
Definition WheeledVehicleController.h:166
float mBrakeInput
Value between 0 and 1 indicating how strong the brake pedal is pressed.
Definition WheeledVehicleController.h:180
void SetTireMaxImpulseCallback(const TireMaxImpulseCallback &inTireMaxImpulseCallback)
Definition WheeledVehicleController.h:151
VehicleTransmission & GetTransmission()
Get current transmission state (writable interface, allows you to make changes to the configuration w...
Definition WheeledVehicleController.h:133
TireMaxImpulseCallback mTireMaxImpulseCallback
Definition WheeledVehicleController.h:191
const TireMaxImpulseCallback & GetTireMaxImpulseCallback() const
Definition WheeledVehicleController.h:150
float GetBrakeInput() const
Definition WheeledVehicleController.h:117
float mDifferentialLimitedSlipRatio
Ratio max / min average wheel speed of each differential (measured at the clutch).
Definition WheeledVehicleController.h:187
Differentials & GetDifferentials()
Get the differentials this vehicle has (writable interface, allows you to make changes to the configu...
Definition WheeledVehicleController.h:139
Array< VehicleDifferentialSettings > Differentials
Typedefs.
Definition WheeledVehicleController.h:98
VehicleEngine & GetEngine()
Get current engine state (writable interface, allows you to make changes to the configuration which w...
Definition WheeledVehicleController.h:127
JPH_OVERRIDE_NEW_DELETE WheeledVehicleController(const WheeledVehicleControllerSettings &inSettings, VehicleConstraint &inConstraint)
Constructor.
Definition WheeledVehicleController.cpp:178
float mPreviousDeltaTime
Delta time of the last step.
Definition WheeledVehicleController.h:188
Vec3 mRPMMeterPosition
Position (in local space of the body) of the RPM meter when drawing the constraint.
Definition WheeledVehicleController.h:200
void SetRightInput(float inRight)
Value between -1 and 1 indicating desired steering angle (1 = right).
Definition WheeledVehicleController.h:112
Definition WheeledVehicleController.h:73
Array< VehicleDifferentialSettings > mDifferentials
List of differentials and their properties.
Definition WheeledVehicleController.h:84
VehicleTransmissionSettings mTransmission
The properties of the transmission (aka gear box).
Definition WheeledVehicleController.h:83
VehicleEngineSettings mEngine
The properties of the engine.
Definition WheeledVehicleController.h:82
virtual VehicleController * ConstructController(VehicleConstraint &inConstraint) const override
Create an instance of the vehicle controller class.
Definition WheeledVehicleController.cpp:144
float mDifferentialLimitedSlipRatio
Ratio max / min average wheel speed of each differential (measured at the clutch)....
Definition WheeledVehicleController.h:85