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{
26
27public:
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 float GetMaxPitchRollAngle() const { return ACos(mCosMaxPitchRollAngle); }
78
80 void SetVehicleCollisionTester(const VehicleCollisionTester *inTester) { mVehicleCollisionTester = inTester; }
81 const VehicleCollisionTester *GetVehicleCollisionTester() const { return mVehicleCollisionTester; }
82
85 using CombineFunction = function<void(uint inWheelIndex, float &ioLongitudinalFriction, float &ioLateralFriction, const Body &inBody2, const SubShapeID &inSubShapeID2)>;
86
89 void SetCombineFriction(const CombineFunction &inCombineFriction) { mCombineFriction = inCombineFriction; }
90 const CombineFunction & GetCombineFriction() const { return mCombineFriction; }
91
93 using StepCallback = function<void(VehicleConstraint &inVehicle, const PhysicsStepListenerContext &inContext)>;
94
98 const StepCallback & GetPreStepCallback() const { return mPreStepCallback; }
99 void SetPreStepCallback(const StepCallback &inPreStepCallback) { mPreStepCallback = inPreStepCallback; }
100
104 const StepCallback & GetPostCollideCallback() const { return mPostCollideCallback; }
105 void SetPostCollideCallback(const StepCallback &inPostCollideCallback) { mPostCollideCallback = inPostCollideCallback; }
106
110 const StepCallback & GetPostStepCallback() const { return mPostStepCallback; }
111 void SetPostStepCallback(const StepCallback &inPostStepCallback) { mPostStepCallback = inPostStepCallback; }
112
114 void OverrideGravity(Vec3Arg inGravity) { mGravityOverride = inGravity; mIsGravityOverridden = true; }
115 bool IsGravityOverridden() const { return mIsGravityOverridden; }
116 Vec3 GetGravityOverride() const { return mGravityOverride; }
117 void ResetGravityOverride() { mIsGravityOverridden = false; mBody->GetMotionProperties()->SetGravityFactor(1.0f); }
118
120 Vec3 GetLocalForward() const { return mForward; }
121
123 Vec3 GetLocalUp() const { return mUp; }
124
126 Vec3 GetWorldUp() const { return mWorldUp; }
127
129 Body * GetVehicleBody() const { return mBody; }
130
132 const VehicleController * GetController() const { return mController; }
133
135 VehicleController * GetController() { return mController; }
136
138 const Wheels & GetWheels() const { return mWheels; }
139
141 Wheels & GetWheels() { return mWheels; }
142
144 Wheel * GetWheel(uint inIdx) { return mWheels[inIdx]; }
145 const Wheel * GetWheel(uint inIdx) const { return mWheels[inIdx]; }
146
152 void GetWheelLocalBasis(const Wheel *inWheel, Vec3 &outForward, Vec3 &outUp, Vec3 &outRight) const;
153
158 Mat44 GetWheelLocalTransform(uint inWheelIndex, Vec3Arg inWheelRight, Vec3Arg inWheelUp) const;
159
164 RMat44 GetWheelWorldTransform(uint inWheelIndex, Vec3Arg inWheelRight, Vec3Arg inWheelUp) const;
165
167 const VehicleAntiRollBars & GetAntiRollBars() const { return mAntiRollBars; }
168 VehicleAntiRollBars & GetAntiRollBars() { return mAntiRollBars; }
169
174 void SetNumStepsBetweenCollisionTestActive(uint inSteps) { mNumStepsBetweenCollisionTestActive = inSteps; }
175 uint GetNumStepsBetweenCollisionTestActive() const { return mNumStepsBetweenCollisionTestActive; }
176
182 void SetNumStepsBetweenCollisionTestInactive(uint inSteps) { mNumStepsBetweenCollisionTestInactive = inSteps; }
183 uint GetNumStepsBetweenCollisionTestInactive() const { return mNumStepsBetweenCollisionTestInactive; }
184
185 // Generic interface of a constraint
186 virtual bool IsActive() const override { return mIsActive && Constraint::IsActive(); }
187 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override { /* Do nothing */ }
188 virtual void SetupVelocityConstraint(float inDeltaTime) override;
189 virtual void ResetWarmStart() override;
190 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
191 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
192 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
193 virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) override;
194 virtual uint BuildIslandSplits(LargeIslandSplitter &ioSplitter) const override;
195#ifdef JPH_DEBUG_RENDERER
196 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
197 virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
198#endif // JPH_DEBUG_RENDERER
199 virtual void SaveState(StateRecorder &inStream) const override;
200 virtual void RestoreState(StateRecorder &inStream) override;
201 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
202
203private:
204 // See: PhysicsStepListener
205 virtual void OnStep(const PhysicsStepListenerContext &inContext) override;
206
207 // Calculate the position where the suspension and traction forces should be applied in world space, relative to the center of mass of both bodies
208 void CalculateSuspensionForcePoint(const Wheel &inWheel, Vec3 &outR1PlusU, Vec3 &outR2) const;
209
210 // Calculate the constraint properties for mPitchRollPart
211 void CalculatePitchRollConstraintProperties(RMat44Arg inBodyTransform);
212
213 // Gravity override
214 bool mIsGravityOverridden = false;
215 Vec3 mGravityOverride = Vec3::sZero();
216
217 // Simulation information
218 Body * mBody;
219 Vec3 mForward;
220 Vec3 mUp;
221 Vec3 mWorldUp;
222 Wheels mWheels;
223 VehicleAntiRollBars mAntiRollBars;
224 VehicleController * mController;
225 bool mIsActive = false;
226 uint mNumStepsBetweenCollisionTestActive = 1;
227 uint mNumStepsBetweenCollisionTestInactive = 1;
228 uint mCurrentStep = 0;
229
230 // Prevent vehicle from toppling over
231 float mCosMaxPitchRollAngle;
232 float mCosPitchRollAngle;
233 Vec3 mPitchRollRotationAxis { 0, 1, 0 };
234 AngleConstraintPart mPitchRollPart;
235
236 // Interfaces
237 RefConst<VehicleCollisionTester> mVehicleCollisionTester;
238 CombineFunction mCombineFriction = [](uint, float &ioLongitudinalFriction, float &ioLateralFriction, const Body &inBody2, const SubShapeID &)
239 {
240 float body_friction = inBody2.GetFriction();
241
242 ioLongitudinalFriction = sqrt(ioLongitudinalFriction * body_friction);
243 ioLateralFriction = sqrt(ioLateralFriction * body_friction);
244 };
245
246 // Callbacks
247 StepCallback mPreStepCallback;
248 StepCallback mPostCollideCallback;
249 StepCallback mPostStepCallback;
250};
251
EConstraintSubType
Enum to identify constraint sub type.
Definition Constraint.h:34
#define JPH_EXPORT
Definition Core.h:275
unsigned int uint
Definition Core.h:492
#define JPH_NAMESPACE_END
Definition Core.h:419
std::uint32_t uint32
Definition Core.h:495
#define JPH_NAMESPACE_BEGIN
Definition Core.h:413
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(linkage, class_name)
Definition SerializableObject.h:109
JPH_INLINE float Cos(float inX)
Cosine of x (input in radians)
Definition Trigonometry.h:20
JPH_INLINE float ACos(float inX)
Definition Trigonometry.h:42
Definition AngleConstraintPart.h:37
Definition Array.h:36
Definition Body.h:39
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:44
Base class for all physics constraints. A constraint removes one or more degrees of freedom for a rig...
Definition Constraint.h:108
virtual bool IsActive() const
Definition Constraint.h:176
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
Context information for the step listener.
Definition PhysicsStepListener.h:13
A listener class that receives a callback before every physics simulation step.
Definition PhysicsStepListener.h:23
Definition PhysicsSystem.h:30
Definition Reference.h:163
Definition Reference.h:107
Definition StateRecorder.h:110
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:17
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:103
Class that does collision detection between wheels and ground.
Definition VehicleCollisionTester.h:20
Definition VehicleConstraint.h:66
const StepCallback & GetPreStepCallback() const
Definition VehicleConstraint.h:98
void SetNumStepsBetweenCollisionTestActive(uint inSteps)
Definition VehicleConstraint.h:174
const VehicleAntiRollBars & GetAntiRollBars() const
Access to the vehicle's anti roll bars.
Definition VehicleConstraint.h:167
VehicleAntiRollBars & GetAntiRollBars()
Definition VehicleConstraint.h:168
function< void(VehicleConstraint &inVehicle, const PhysicsStepListenerContext &inContext)> StepCallback
Callback function to notify of current stage in PhysicsStepListener::OnStep.
Definition VehicleConstraint.h:93
void ResetGravityOverride()
Note that resetting the gravity override will restore the gravity factor of the vehicle body to 1.
Definition VehicleConstraint.h:117
void SetCombineFriction(const CombineFunction &inCombineFriction)
Definition VehicleConstraint.h:89
const Wheel * GetWheel(uint inIdx) const
Definition VehicleConstraint.h:145
uint GetNumStepsBetweenCollisionTestInactive() const
Definition VehicleConstraint.h:183
const CombineFunction & GetCombineFriction() const
Definition VehicleConstraint.h:90
Body * GetVehicleBody() const
Access to the vehicle body.
Definition VehicleConstraint.h:129
void SetNumStepsBetweenCollisionTestInactive(uint inSteps)
Definition VehicleConstraint.h:182
virtual bool IsActive() const override
Definition VehicleConstraint.h:186
bool IsGravityOverridden() const
Definition VehicleConstraint.h:115
VehicleController * GetController()
Access to the vehicle controller interface (determines acceleration / deceleration)
Definition VehicleConstraint.h:135
void SetVehicleCollisionTester(const VehicleCollisionTester *inTester)
Set the interface that tests collision between wheel and ground.
Definition VehicleConstraint.h:80
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
Vec3 GetGravityOverride() const
Definition VehicleConstraint.h:116
const StepCallback & GetPostStepCallback() const
Definition VehicleConstraint.h:110
Vec3 GetLocalForward() const
Get the local space forward vector of the vehicle.
Definition VehicleConstraint.h:120
Wheels & GetWheels()
Get the state of a wheels (writable interface, allows you to make changes to the configuration which ...
Definition VehicleConstraint.h:141
Wheel * GetWheel(uint inIdx)
Get the state of a wheel.
Definition VehicleConstraint.h:144
Vec3 GetWorldUp() const
Vector indicating the world space up direction (used to limit vehicle pitch/roll),...
Definition VehicleConstraint.h:126
const VehicleCollisionTester * GetVehicleCollisionTester() const
Definition VehicleConstraint.h:81
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:85
Vec3 GetLocalUp() const
Get the local space up vector of the vehicle.
Definition VehicleConstraint.h:123
uint GetNumStepsBetweenCollisionTestActive() const
Definition VehicleConstraint.h:175
const StepCallback & GetPostCollideCallback() const
Definition VehicleConstraint.h:104
const VehicleController * GetController() const
Access to the vehicle controller interface (determines acceleration / deceleration)
Definition VehicleConstraint.h:132
void SetPostCollideCallback(const StepCallback &inPostCollideCallback)
Definition VehicleConstraint.h:105
virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override
Definition VehicleConstraint.h:187
void OverrideGravity(Vec3Arg inGravity)
Override gravity for this vehicle. Note that overriding gravity will set the gravity factor of the ve...
Definition VehicleConstraint.h:114
void SetPreStepCallback(const StepCallback &inPreStepCallback)
Definition VehicleConstraint.h:99
float GetMaxPitchRollAngle() const
Definition VehicleConstraint.h:77
const Wheels & GetWheels() const
Get the state of the wheels.
Definition VehicleConstraint.h:138
void SetPostStepCallback(const StepCallback &inPostStepCallback)
Definition VehicleConstraint.h:111
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
VehicleAntiRollBars 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