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{
21public:
23
24
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 WheelWV : public Wheel
42{
43public:
45
47 explicit WheelWV(const WheelSettingsWV &inWheel);
48
50 const WheelSettingsWV * GetSettings() const { return static_cast<const WheelSettingsWV *>(mSettings.GetPtr()); }
51
53 void ApplyTorque(float inTorque, float inDeltaTime)
54 {
55 mAngularVelocity += inTorque * inDeltaTime / GetSettings()->mInertia;
56 }
57
59 void Update(float inDeltaTime, const VehicleConstraint &inConstraint);
60
61 float mLongitudinalSlip = 0.0f;
64 float mBrakeImpulse = 0.0f;
65};
66
72{
73public:
75
76 // See: VehicleControllerSettings
77 virtual VehicleController * ConstructController(VehicleConstraint &inConstraint) const override;
78 virtual void SaveBinaryState(StreamOut &inStream) const override;
79 virtual void RestoreBinaryState(StreamIn &inStream) override;
80
85};
86
89{
90public:
92
95
98
104 void SetDriverInput(float inForward, float inRight, float inBrake, float inHandBrake) { mForwardInput = inForward; mRightInput = inRight; mBrakeInput = inBrake; mHandBrakeInput = inHandBrake; }
105
107 const VehicleEngine & GetEngine() const { return mEngine; }
108
111
114
117
120
123
127
128#ifdef JPH_DEBUG_RENDERER
130 void SetRPMMeter(Vec3Arg inPosition, float inSize) { mRPMMeterPosition = inPosition; mRPMMeterSize = inSize; }
131#endif // JPH_DEBUG_RENDERER
132
133protected:
134 // See: VehicleController
135 virtual Wheel * ConstructWheel(const WheelSettings &inWheel) const override { JPH_ASSERT(IsKindOf(&inWheel, JPH_RTTI(WheelSettingsWV))); return new WheelWV(static_cast<const WheelSettingsWV &>(inWheel)); }
136 virtual bool AllowSleep() const override { return mForwardInput == 0.0f; }
137 virtual void PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
138 virtual void PostCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
139 virtual bool SolveLongitudinalAndLateralConstraints(float inDeltaTime) override;
140 virtual void SaveState(StateRecorder &inStream) const override;
141 virtual void RestoreState(StateRecorder &inStream) override;
142#ifdef JPH_DEBUG_RENDERER
143 virtual void Draw(DebugRenderer *inRenderer) const override;
144#endif // JPH_DEBUG_RENDERER
145
146 // Control information
147 float mForwardInput = 0.0f;
148 float mRightInput = 0.0f;
149 float mBrakeInput = 0.0f;
150 float mHandBrakeInput = 0.0f;
151
152 // Simluation information
157
158#ifdef JPH_DEBUG_RENDERER
159 // Debug settings
161 float mRPMMeterSize = 0.5f;
162#endif // JPH_DEBUG_RENDERER
163};
164
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
constexpr float DegreesToRadians(float inV)
Convert a value from degrees to radians.
Definition: Math.h:13
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
bool IsKindOf(const Type *inObject, const RTTI *inRTTI)
Check if inObject is or is derived from DstType.
Definition: RTTI.h:365
#define JPH_RTTI(class_name)
Definition: RTTI.h:315
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(class_name)
Definition: SerializableObject.h:100
Simple triangle renderer for debugging purposes.
Definition: DebugRenderer.h:25
Definition: LinearCurve.h:17
Definition: PhysicsSystem.h:28
const T * GetPtr() const
Get pointer.
Definition: Reference.h:188
Definition: StateRecorder.h:15
Simple binary input stream.
Definition: StreamIn.h:11
Simple binary output stream.
Definition: StreamOut.h:11
Definition: Vec3.h:16
Definition: VehicleConstraint.h:67
Runtime data for interface that controls acceleration / decelleration of the vehicle.
Definition: VehicleController.h:34
Basic settings object for interface that controls acceleration / decelleration of the vehicle.
Definition: VehicleController.h:18
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:45
RefConst< WheelSettings > mSettings
Configuration settings for this wheel.
Definition: Wheel.h:122
float mAngularVelocity
Rotation speed of wheel, positive when the wheels cause the vehicle to move forwards (rad/s)
Definition: Wheel.h:136
Base class for wheel settings, each VehicleController can implement a derived class of this.
Definition: Wheel.h:19
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
virtual void RestoreBinaryState(StreamIn &inStream) override
Restores the contents in binary form to inStream.
Definition: WheeledVehicleController.cpp:65
LinearCurve mLateralFriction
Friction in sideway direction of tire as a function of the slip angle (degrees): angle between relati...
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.
Definition: WheeledVehicleController.h:32
LinearCurve mLongitudinalFriction
Friction in forward direction of tire as a function of the slip ratio (fraction): (omega_wheel * r_wh...
Definition: WheeledVehicleController.h:34
virtual void SaveBinaryState(StreamOut &inStream) const override
Saves the contents in binary form to inStream.
Definition: WheeledVehicleController.cpp:54
WheelSettingsWV()
Constructor.
Definition: WheeledVehicleController.cpp:41
float mMaxSteerAngle
How much this wheel can steer (radians)
Definition: WheeledVehicleController.h:33
Wheel object specifically for WheeledVehicleController.
Definition: WheeledVehicleController.h:42
void Update(float inDeltaTime, const VehicleConstraint &inConstraint)
Update the wheel rotation based on the current angular velocity.
Definition: WheeledVehicleController.cpp:86
const WheelSettingsWV * GetSettings() const
Override GetSettings and cast to the correct class.
Definition: WheeledVehicleController.h:50
float mCombinedLongitudinalFriction
Combined friction coefficient in longitudinal direction (combines terrain and tires)
Definition: WheeledVehicleController.h:62
void ApplyTorque(float inTorque, float inDeltaTime)
Apply a torque (N m) to the wheel for a particular delta time.
Definition: WheeledVehicleController.h:53
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:63
float mBrakeImpulse
Amount of impulse that the brakes can apply to the floor (excluding friction)
Definition: WheeledVehicleController.h:64
Runtime controller class.
Definition: WheeledVehicleController.h:89
virtual bool SolveLongitudinalAndLateralConstraints(float inDeltaTime) override
Definition: WheeledVehicleController.cpp:567
float mHandBrakeInput
Value between 0 and 1 indicating how strong the hand brake is pulled.
Definition: WheeledVehicleController.h:150
float mForwardInput
Value between -1 and 1 for auto transmission and value between 0 and 1 indicating desired driving dir...
Definition: WheeledVehicleController.h:147
void SetDifferentialLimitedSlipRatio(float inV)
Definition: WheeledVehicleController.h:126
VehicleTransmission mTransmission
Transmission state of the vehicle.
Definition: WheeledVehicleController.h:154
virtual void Draw(DebugRenderer *inRenderer) const override
Definition: WheeledVehicleController.cpp:639
const VehicleTransmission & GetTransmission() const
Get current transmission state.
Definition: WheeledVehicleController.h:113
void SetDriverInput(float inForward, float inRight, float inBrake, float inHandBrake)
Definition: WheeledVehicleController.h:104
Differentials mDifferentials
Differential states of the vehicle.
Definition: WheeledVehicleController.h:155
const VehicleEngine & GetEngine() const
Get current engine state.
Definition: WheeledVehicleController.h:107
VehicleEngine mEngine
Engine state of the vehicle.
Definition: WheeledVehicleController.h:153
float mRightInput
Value between -1 and 1 indicating desired steering angle.
Definition: WheeledVehicleController.h:148
float GetDifferentialLimitedSlipRatio() const
Ratio max / min average wheel speed of each differential (measured at the clutch).
Definition: WheeledVehicleController.h:125
virtual bool AllowSleep() const override
Definition: WheeledVehicleController.h:136
void SetRPMMeter(Vec3Arg inPosition, float inSize)
Debug drawing of RPM meter.
Definition: WheeledVehicleController.h:130
const Differentials & GetDifferentials() const
Get the differentials this vehicle has.
Definition: WheeledVehicleController.h:119
float mRPMMeterSize
Size of the RPM meter when drawing the constraint.
Definition: WheeledVehicleController.h:161
virtual void RestoreState(StateRecorder &inStream) override
Definition: WheeledVehicleController.cpp:720
virtual Wheel * ConstructWheel(const WheelSettings &inWheel) const override
Definition: WheeledVehicleController.h:135
float mBrakeInput
Value between 0 and 1 indicating how strong the brake pedal is pressed.
Definition: WheeledVehicleController.h:149
virtual void PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override
Definition: WheeledVehicleController.cpp:204
VehicleTransmission & GetTransmission()
Get current transmission state (writable interface, allows you to make changes to the configuration w...
Definition: WheeledVehicleController.h:116
float mDifferentialLimitedSlipRatio
Ratio max / min average wheel speed of each differential (measured at the clutch).
Definition: WheeledVehicleController.h:156
Differentials & GetDifferentials()
Get the differentials this vehicle has (writable interface, allows you to make changes to the configu...
Definition: WheeledVehicleController.h:122
Array< VehicleDifferentialSettings > Differentials
Typedefs.
Definition: WheeledVehicleController.h:97
VehicleEngine & GetEngine()
Get current engine state (writable interface, allows you to make changes to the configuration which w...
Definition: WheeledVehicleController.h:110
virtual void PostCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override
Definition: WheeledVehicleController.cpp:217
virtual void SaveState(StateRecorder &inStream) const override
Definition: WheeledVehicleController.cpp:709
Vec3 mRPMMeterPosition
Position (in local space of the body) of the RPM meter when drawing the constraint.
Definition: WheeledVehicleController.h:160
Definition: WheeledVehicleController.h:72
Array< VehicleDifferentialSettings > mDifferentials
List of differentials and their properties.
Definition: WheeledVehicleController.h:83
virtual void RestoreBinaryState(StreamIn &inStream) override
Restore the contents of the controller settings in binary form from inStream.
Definition: WheeledVehicleController.cpp:151
VehicleTransmissionSettings mTransmission
The properties of the transmission (aka gear box)
Definition: WheeledVehicleController.h:82
VehicleEngineSettings mEngine
The properties of the engine.
Definition: WheeledVehicleController.h:81
virtual VehicleController * ConstructController(VehicleConstraint &inConstraint) const override
Create an instance of the vehicle controller class.
Definition: WheeledVehicleController.cpp:132
float mDifferentialLimitedSlipRatio
Ratio max / min average wheel speed of each differential (measured at the clutch)....
Definition: WheeledVehicleController.h:84
virtual void SaveBinaryState(StreamOut &inStream) const override
Saves the contents of the controller settings in binary form to inStream.
Definition: WheeledVehicleController.cpp:137