Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
VehicleConstraint.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
14
16
17class PhysicsSystem;
18
24{
25public:
27
28
29 virtual void SaveBinaryState(StreamOut &inStream) const override;
30
31 Vec3 mUp { 0, 1, 0 };
32 Vec3 mForward { 0, 0, 1 };
33 float mMaxPitchRollAngle = JPH_PI;
37
38protected:
40 virtual void RestoreBinaryState(StreamIn &inStream) override;
41};
42
66{
67public:
69 VehicleConstraint(Body &inVehicleBody, const VehicleConstraintSettings &inSettings);
70 virtual ~VehicleConstraint() override;
71
73 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Vehicle; }
74
76 void SetMaxPitchRollAngle(float inMaxPitchRollAngle) { mCosMaxPitchRollAngle = Cos(inMaxPitchRollAngle); }
77
79 void SetVehicleCollisionTester(const VehicleCollisionTester *inTester) { mVehicleCollisionTester = inTester; }
80
83 using CombineFunction = function<void(uint inWheelIndex, float &ioLongitudinalFriction, float &ioLateralFriction, const Body &inBody2, const SubShapeID &inSubShapeID2)>;
84
87 void SetCombineFriction(const CombineFunction &inCombineFriction) { mCombineFriction = inCombineFriction; }
88 const CombineFunction & GetCombineFriction() const { return mCombineFriction; }
89
91 using StepCallback = function<void(VehicleConstraint &inVehicle, float inDeltaTime, PhysicsSystem &inPhysicsSystem)>;
92
96 const StepCallback & GetPreStepCallback() const { return mPreStepCallback; }
97 void SetPreStepCallback(const StepCallback &inPreStepCallback) { mPreStepCallback = inPreStepCallback; }
98
102 const StepCallback & GetPostCollideCallback() const { return mPostCollideCallback; }
103 void SetPostCollideCallback(const StepCallback &inPostCollideCallback) { mPostCollideCallback = inPostCollideCallback; }
104
108 const StepCallback & GetPostStepCallback() const { return mPostStepCallback; }
109 void SetPostStepCallback(const StepCallback &inPostStepCallback) { mPostStepCallback = inPostStepCallback; }
110
112 Vec3 GetLocalForward() const { return mForward; }
113
115 Vec3 GetLocalUp() const { return mUp; }
116
118 Vec3 GetWorldUp() const { return mWorldUp; }
119
121 Body * GetVehicleBody() const { return mBody; }
122
124 const VehicleController * GetController() const { return mController; }
125
127 VehicleController * GetController() { return mController; }
128
130 const Wheels & GetWheels() const { return mWheels; }
131
133 Wheels & GetWheels() { return mWheels; }
134
136 Wheel * GetWheel(uint inIdx) { return mWheels[inIdx]; }
137 const Wheel * GetWheel(uint inIdx) const { return mWheels[inIdx]; }
138
144 void GetWheelLocalBasis(const Wheel *inWheel, Vec3 &outForward, Vec3 &outUp, Vec3 &outRight) const;
145
150 Mat44 GetWheelLocalTransform(uint inWheelIndex, Vec3Arg inWheelRight, Vec3Arg inWheelUp) const;
151
156 RMat44 GetWheelWorldTransform(uint inWheelIndex, Vec3Arg inWheelRight, Vec3Arg inWheelUp) const;
157
162 void SetNumStepsBetweenCollisionTestActive(uint inSteps) { mNumStepsBetweenCollisionTestActive = inSteps; }
163 uint GetNumStepsBetweenCollisionTestActive() const { return mNumStepsBetweenCollisionTestActive; }
164
170 void SetNumStepsBetweenCollisionTestInactive(uint inSteps) { mNumStepsBetweenCollisionTestInactive = inSteps; }
171 uint GetNumStepsBetweenCollisionTestInactive() const { return mNumStepsBetweenCollisionTestInactive; }
172
173 // Generic interface of a constraint
174 virtual bool IsActive() const override { return mIsActive && Constraint::IsActive(); }
175 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override { /* Do nothing */ }
176 virtual void SetupVelocityConstraint(float inDeltaTime) override;
177 virtual void ResetWarmStart() override;
178 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
179 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
180 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
181 virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) override;
182 virtual uint BuildIslandSplits(LargeIslandSplitter &ioSplitter) const override;
183#ifdef JPH_DEBUG_RENDERER
184 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
185 virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
186#endif // JPH_DEBUG_RENDERER
187 virtual void SaveState(StateRecorder &inStream) const override;
188 virtual void RestoreState(StateRecorder &inStream) override;
189 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
190
191private:
192 // See: PhysicsStepListener
193 virtual void OnStep(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
194
195 // Calculate the position where the suspension and traction forces should be applied in world space, relative to the center of mass of both bodies
196 void CalculateSuspensionForcePoint(const Wheel &inWheel, Vec3 &outR1PlusU, Vec3 &outR2) const;
197
198 // Calculate the constraint properties for mPitchRollPart
199 void CalculatePitchRollConstraintProperties(RMat44Arg inBodyTransform);
200
201 // Simulation information
202 Body * mBody;
203 Vec3 mForward;
204 Vec3 mUp;
205 Vec3 mWorldUp;
206 Wheels mWheels;
207 Array<VehicleAntiRollBar> mAntiRollBars;
208 VehicleController * mController;
209 bool mIsActive = false;
210 uint mNumStepsBetweenCollisionTestActive = 1;
211 uint mNumStepsBetweenCollisionTestInactive = 1;
212 uint mCurrentStep = 0;
213
214 // Prevent vehicle from toppling over
215 float mCosMaxPitchRollAngle;
216 float mCosPitchRollAngle;
217 Vec3 mPitchRollRotationAxis { 0, 1, 0 };
218 AngleConstraintPart mPitchRollPart;
219
220 // Interfaces
221 RefConst<VehicleCollisionTester> mVehicleCollisionTester;
222 CombineFunction mCombineFriction = [](uint, float &ioLongitudinalFriction, float &ioLateralFriction, const Body &inBody2, const SubShapeID &)
223 {
224 float body_friction = inBody2.GetFriction();
225
226 ioLongitudinalFriction = sqrt(ioLongitudinalFriction * body_friction);
227 ioLateralFriction = sqrt(ioLateralFriction * body_friction);
228 };
229
230 // Callbacks
231 StepCallback mPreStepCallback;
232 StepCallback mPostCollideCallback;
233 StepCallback mPostStepCallback;
234};
235
EConstraintSubType
Enum to identify constraint sub type.
Definition: Constraint.h:34
#define JPH_EXPORT
Definition: Core.h:227
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
std::uint32_t uint32
Definition: Core.h:442
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(linkage, class_name)
Definition: SerializableObject.h:100
JPH_INLINE float Cos(float inX)
Cosine of x (input in radians)
Definition: Trigonometry.h:20
Array< Wheel * > Wheels
Definition: Wheel.h:146
Definition: AngleConstraintPart.h:37
Definition: Body.h:35
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition: BodyID.h:13
Class that contains all bodies.
Definition: BodyManager.h:32
Base class for all physics constraints. A constraint removes one or more degrees of freedom for a rig...
Definition: Constraint.h:103
virtual bool IsActive() const
Definition: Constraint.h:171
Class used to store the configuration of a constraint. Allows run-time creation of constraints.
Definition: Constraint.h:65
virtual void SaveBinaryState(StreamOut &inStream) const
Saves the contents of the constraint settings in binary form to inStream.
Definition: Constraint.cpp:26
virtual void RestoreBinaryState(StreamIn &inStream)
This function should not be called directly, it is used by sRestoreFromBinaryState.
Definition: Constraint.cpp:36
Definition: DebugRenderer.h:47
Keeps track of connected bodies and builds islands for multithreaded velocity/position update.
Definition: IslandBuilder.h:19
Definition: LargeIslandSplitter.h:25
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
A listener class that receives a callback before every physics simulation step.
Definition: PhysicsStepListener.h:13
Definition: PhysicsSystem.h:29
Definition: Reference.h:151
Definition: Reference.h:101
Definition: StateRecorder.h:48
Simple binary input stream.
Definition: StreamIn.h:13
Simple binary output stream.
Definition: StreamOut.h:13
A sub shape id contains a path to an element (usually a triangle or other primitive type) of a compou...
Definition: SubShapeID.h:23
Definition: Vec3.h:16
Class that does collision detection between wheels and ground.
Definition: VehicleCollisionTester.h:20
Definition: VehicleConstraint.h:66
const StepCallback & GetPreStepCallback() const
Definition: VehicleConstraint.h:96
void SetNumStepsBetweenCollisionTestActive(uint inSteps)
Definition: VehicleConstraint.h:162
void SetCombineFriction(const CombineFunction &inCombineFriction)
Definition: VehicleConstraint.h:87
const Wheel * GetWheel(uint inIdx) const
Definition: VehicleConstraint.h:137
uint GetNumStepsBetweenCollisionTestInactive() const
Definition: VehicleConstraint.h:171
const CombineFunction & GetCombineFriction() const
Definition: VehicleConstraint.h:88
Body * GetVehicleBody() const
Access to the vehicle body.
Definition: VehicleConstraint.h:121
void SetNumStepsBetweenCollisionTestInactive(uint inSteps)
Definition: VehicleConstraint.h:170
virtual bool IsActive() const override
Definition: VehicleConstraint.h:174
VehicleController * GetController()
Access to the vehicle controller interface (determines acceleration / deceleration)
Definition: VehicleConstraint.h:127
void SetVehicleCollisionTester(const VehicleCollisionTester *inTester)
Set the interface that tests collision between wheel and ground.
Definition: VehicleConstraint.h:79
void SetMaxPitchRollAngle(float inMaxPitchRollAngle)
Defines the maximum pitch/roll angle (rad), can be used to avoid the car from getting upside down....
Definition: VehicleConstraint.h:76
const StepCallback & GetPostStepCallback() const
Definition: VehicleConstraint.h:108
Vec3 GetLocalForward() const
Get the local space forward vector of the vehicle.
Definition: VehicleConstraint.h:112
Wheels & GetWheels()
Get the state of a wheels (writable interface, allows you to make changes to the configuration which ...
Definition: VehicleConstraint.h:133
Wheel * GetWheel(uint inIdx)
Get the state of a wheel.
Definition: VehicleConstraint.h:136
Vec3 GetWorldUp() const
Vector indicating the world space up direction (used to limit vehicle pitch/roll),...
Definition: VehicleConstraint.h:118
virtual EConstraintSubType GetSubType() const override
Get the type of a constraint.
Definition: VehicleConstraint.h:73
function< void(uint inWheelIndex, float &ioLongitudinalFriction, float &ioLateralFriction, const Body &inBody2, const SubShapeID &inSubShapeID2)> CombineFunction
Definition: VehicleConstraint.h:83
Vec3 GetLocalUp() const
Get the local space up vector of the vehicle.
Definition: VehicleConstraint.h:115
uint GetNumStepsBetweenCollisionTestActive() const
Definition: VehicleConstraint.h:163
const StepCallback & GetPostCollideCallback() const
Definition: VehicleConstraint.h:102
const VehicleController * GetController() const
Access to the vehicle controller interface (determines acceleration / deceleration)
Definition: VehicleConstraint.h:124
void SetPostCollideCallback(const StepCallback &inPostCollideCallback)
Definition: VehicleConstraint.h:103
virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override
Definition: VehicleConstraint.h:175
void SetPreStepCallback(const StepCallback &inPreStepCallback)
Definition: VehicleConstraint.h:97
const Wheels & GetWheels() const
Get the state of the wheels.
Definition: VehicleConstraint.h:130
void SetPostStepCallback(const StepCallback &inPostStepCallback)
Definition: VehicleConstraint.h:109
function< void(VehicleConstraint &inVehicle, float inDeltaTime, PhysicsSystem &inPhysicsSystem)> StepCallback
Callback function to notify of current stage in PhysicsStepListener::OnStep.
Definition: VehicleConstraint.h:91
Definition: VehicleConstraint.h:24
Ref< VehicleControllerSettings > mController
Defines how the vehicle can accelerate / decelerate.
Definition: VehicleConstraint.h:36
Array< Ref< WheelSettings > > mWheels
List of wheels and their properties.
Definition: VehicleConstraint.h:34
Array< VehicleAntiRollBar > mAntiRollBars
List of anti rollbars and their properties.
Definition: VehicleConstraint.h:35
Runtime data for interface that controls acceleration / deceleration of the vehicle.
Definition: VehicleController.h:41
Base class for runtime data for a wheel, each VehicleController can implement a derived class of this...
Definition: Wheel.h:46