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;
33 float mMaxSteerAngle = DegreesToRadians(70.0f);
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
50 const WheelSettingsWV * GetSettings() const { return StaticCast<WheelSettingsWV>(mSettings); }
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;
63 float mCombinedLongitudinalFriction = 0.0f;
64 float mCombinedLateralFriction = 0.0f;
65 float mBrakeImpulse = 0.0f;
66};
67
73{
75
76public:
77 // See: VehicleControllerSettings
78 virtual VehicleController * ConstructController(VehicleConstraint &inConstraint) const override;
79 virtual void SaveBinaryState(StreamOut &inStream) const override;
80 virtual void RestoreBinaryState(StreamIn &inStream) override;
81
85 float mDifferentialLimitedSlipRatio = 1.4f;
86};
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
127 VehicleEngine & GetEngine() { return mEngine; }
128
130 const VehicleTransmission & GetTransmission() const { return mTransmission; }
131
133 VehicleTransmission & GetTransmission() { return mTransmission; }
134
136 const Differentials & GetDifferentials() const { return mDifferentials; }
137
139 Differentials & GetDifferentials() { return mDifferentials; }
140
142 float GetDifferentialLimitedSlipRatio() const { return mDifferentialLimitedSlipRatio; }
143 void SetDifferentialLimitedSlipRatio(float inV) { mDifferentialLimitedSlipRatio = inV; }
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)>;
150 const TireMaxImpulseCallback&GetTireMaxImpulseCallback() const { return mTireMaxImpulseCallback; }
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
158protected:
159 // See: VehicleController
160 virtual Wheel * ConstructWheel(const WheelSettings &inWheel) const override { JPH_ASSERT(IsKindOf(&inWheel, JPH_RTTI(WheelSettingsWV))); return new WheelWV(static_cast<const WheelSettingsWV &>(inWheel)); }
161 virtual bool AllowSleep() const override;
162 virtual void PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
163 virtual void PostCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
164 virtual bool SolveLongitudinalAndLateralConstraints(float inDeltaTime) override;
165 virtual void SaveState(StateRecorder &inStream) const override;
166 virtual void RestoreState(StateRecorder &inStream) override;
167#ifdef JPH_DEBUG_RENDERER
168 virtual void Draw(DebugRenderer *inRenderer) const override;
169#endif // JPH_DEBUG_RENDERER
170
171 // Control information
172 float mForwardInput = 0.0f;
173 float mRightInput = 0.0f;
174 float mBrakeInput = 0.0f;
175 float mHandBrakeInput = 0.0f;
176
177 // Simulation information
182 float mPreviousDeltaTime = 0.0f;
183
184 // Callback that calculates the max impulse that the tire can apply to the ground
185 TireMaxImpulseCallback mTireMaxImpulseCallback =
186 [](uint, float &outLongitudinalImpulse, float &outLateralImpulse, float inSuspensionImpulse, float inLongitudinalFriction, float inLateralFriction, float, float, float)
187 {
188 outLongitudinalImpulse = inLongitudinalFriction * inSuspensionImpulse;
189 outLateralImpulse = inLateralFriction * inSuspensionImpulse;
190 };
191
192#ifdef JPH_DEBUG_RENDERER
193 // Debug settings
194 Vec3 mRPMMeterPosition { 0, 1, 0 };
195 float mRPMMeterSize = 0.5f;
196#endif // JPH_DEBUG_RENDERER
197};
198
#define JPH_EXPORT
Definition Core.h:271
unsigned int uint
Definition Core.h:481
#define JPH_NAMESPACE_END
Definition Core.h:414
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
#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
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:31
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 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
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
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
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
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
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
void ApplyTorque(float inTorque, float inDeltaTime)
Apply a torque (N m) to the wheel for a particular delta time.
Definition WheeledVehicleController.h:53
Runtime controller class.
Definition WheeledVehicleController.h:90
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
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:179
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:180
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:178
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
virtual Wheel * ConstructWheel(const WheelSettings &inWheel) const override
Definition WheeledVehicleController.h:160
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
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:181
Differentials & GetDifferentials()
Get the differentials this vehicle has (writable interface, allows you to make changes to the configu...
Definition WheeledVehicleController.h:139
VehicleEngine & GetEngine()
Get current engine state (writable interface, allows you to make changes to the configuration which w...
Definition WheeledVehicleController.h:127
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