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
13
15
16class PhysicsSystem;
19
43
67{
68public:
70 VehicleConstraint(Body &inVehicleBody, const VehicleConstraintSettings &inSettings);
71 virtual ~VehicleConstraint() override;
72
74 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Vehicle; }
75
77 void SetMaxPitchRollAngle(float inMaxPitchRollAngle) { mCosMaxPitchRollAngle = Cos(inMaxPitchRollAngle); }
78
80 void SetVehicleCollisionTester(const VehicleCollisionTester *inTester) { mVehicleCollisionTester = inTester; }
81
83 Vec3 GetLocalForward() const { return mForward; }
84
86 Vec3 GetLocalUp() const { return mUp; }
87
89 Body * GetVehicleBody() const { return mBody; }
90
92 const VehicleController * GetController() const { return mController; }
93
95 VehicleController * GetController() { return mController; }
96
98 const Wheels & GetWheels() const { return mWheels; }
99
101 Wheels & GetWheels() { return mWheels; }
102
104 Wheel * GetWheel(uint inIdx) { return mWheels[inIdx]; }
105 const Wheel * GetWheel(uint inIdx) const { return mWheels[inIdx]; }
106
112 void GetWheelLocalBasis(const Wheel *inWheel, Vec3 &outForward, Vec3 &outUp, Vec3 &outRight) const;
113
118 Mat44 GetWheelLocalTransform(uint inWheelIndex, Vec3Arg inWheelRight, Vec3Arg inWheelUp) const;
119
124 RMat44 GetWheelWorldTransform(uint inWheelIndex, Vec3Arg inWheelRight, Vec3Arg inWheelUp) const;
125
126 // Generic interface of a constraint
127 virtual bool IsActive() const override { return mIsActive && Constraint::IsActive(); }
128 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override { /* Do nothing */ }
129 virtual void SetupVelocityConstraint(float inDeltaTime) override;
130 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
131 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
132 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
133 virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) override;
134 virtual uint BuildIslandSplits(LargeIslandSplitter &ioSplitter) const override;
135#ifdef JPH_DEBUG_RENDERER
136 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
137 virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
138#endif // JPH_DEBUG_RENDERER
139 virtual void SaveState(StateRecorder &inStream) const override;
140 virtual void RestoreState(StateRecorder &inStream) override;
141 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
142
143private:
144 // See: PhysicsStepListener
145 virtual void OnStep(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
146
147 // Calculate the contact positions of the wheel in world space, relative to the center of mass of both bodies
148 void CalculateWheelContactPoint(const Wheel &inWheel, Vec3 &outR1PlusU, Vec3 &outR2) const;
149
150 // Calculate the constraint properties for mPitchRollPart
151 void CalculatePitchRollConstraintProperties(float inDeltaTime, RMat44Arg inBodyTransform);
152
153 // Simluation information
154 Body * mBody;
155 Vec3 mForward;
156 Vec3 mUp;
157 Wheels mWheels;
158 Array<VehicleAntiRollBar> mAntiRollBars;
159 VehicleController * mController;
160 bool mIsActive = false;
161
162 // Prevent vehicle from toppling over
163 float mCosMaxPitchRollAngle;
164 float mCosPitchRollAngle;
165 Vec3 mPitchRollRotationAxis { 0, 1, 0 };
166 AngleConstraintPart mPitchRollPart;
167
168 // Interfaces
169 RefConst<VehicleCollisionTester> mVehicleCollisionTester;
170};
171
EConstraintSubType
Enum to identify constraint sub type.
Definition Constraint.h:34
uint32_t uint32
Definition Core.h:312
unsigned int uint
Definition Core.h:309
#define JPH_NAMESPACE_END
Definition Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition Core.h:234
std::vector< T, STLAllocator< T > > Array
Definition STLAllocator.h:81
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(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:145
Definition AngleConstraintPart.h:36
Definition Body.h:33
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:30
Base class for all physics constraints. A constraint removes one or more degrees of freedom for a rig...
Definition Constraint.h:99
virtual bool IsActive() const
Definition Constraint.h:153
Class used to store the configuration of a constraint. Allows run-time creation of constraints.
Definition Constraint.h:65
Simple triangle renderer for debugging purposes.
Definition DebugRenderer.h:25
Keeps track of connected bodies and builds islands for multithreaded velocity/position update.
Definition IslandBuilder.h:19
Definition LargeIslandSplitter.h:24
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:28
Definition Reference.h:154
Definition Reference.h:101
Definition StateRecorder.h:15
Simple binary input stream.
Definition StreamIn.h:11
Simple binary output stream.
Definition StreamOut.h:11
Definition Vec3.h:16
Class that does collision detection between wheels and ground.
Definition VehicleCollisionTester.h:19
Definition VehicleConstraint.h:67
const Wheel * GetWheel(uint inIdx) const
Definition VehicleConstraint.h:105
Mat44 GetWheelLocalTransform(uint inWheelIndex, Vec3Arg inWheelRight, Vec3Arg inWheelUp) const
Definition VehicleConstraint.cpp:131
virtual void RestoreState(StateRecorder &inStream) override
Restoring state for replay.
Definition VehicleConstraint.cpp:560
virtual bool SolveVelocityConstraint(float inDeltaTime) override
Definition VehicleConstraint.cpp:454
virtual uint BuildIslandSplits(LargeIslandSplitter &ioSplitter) const override
Link bodies that are connected by this constraint in the same split. Returns the split index.
Definition VehicleConstraint.cpp:309
Body * GetVehicleBody() const
Access to the vehicle body.
Definition VehicleConstraint.h:89
virtual bool IsActive() const override
Definition VehicleConstraint.h:127
VehicleController * GetController()
Access to the vehicle controller interface (determines acceleration / decelleration)
Definition VehicleConstraint.h:95
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:77
Vec3 GetLocalForward() const
Get the local space forward vector of the vehicle.
Definition VehicleConstraint.h:83
Wheels & GetWheels()
Get the state of a wheels (writable interface, allows you to make changes to the configuration which ...
Definition VehicleConstraint.h:101
virtual void SaveState(StateRecorder &inStream) const override
Saving state for replay.
Definition VehicleConstraint.cpp:536
Wheel * GetWheel(uint inIdx)
Get the state of a wheel.
Definition VehicleConstraint.h:104
virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override
Definition VehicleConstraint.cpp:438
virtual void SetupVelocityConstraint(float inDeltaTime) override
Definition VehicleConstraint.cpp:345
virtual EConstraintSubType GetSubType() const override
Get the type of a constraint.
Definition VehicleConstraint.h:74
Vec3 GetLocalUp() const
Get the local space up vector of the vehicle.
Definition VehicleConstraint.h:86
virtual ~VehicleConstraint() override
Definition VehicleConstraint.cpp:110
virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override
Definition VehicleConstraint.cpp:530
const VehicleController * GetController() const
Access to the vehicle controller interface (determines acceleration / decelleration)
Definition VehicleConstraint.h:92
RMat44 GetWheelWorldTransform(uint inWheelIndex, Vec3Arg inWheelRight, Vec3Arg inWheelUp) const
Definition VehicleConstraint.cpp:151
virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override
Definition VehicleConstraint.h:128
virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) override
Link bodies that are connected by this constraint in the island builder.
Definition VehicleConstraint.cpp:255
void GetWheelLocalBasis(const Wheel *inWheel, Vec3 &outForward, Vec3 &outUp, Vec3 &outRight) const
Definition VehicleConstraint.cpp:120
VehicleConstraint(Body &inVehicleBody, const VehicleConstraintSettings &inSettings)
Constructor / destructor.
Definition VehicleConstraint.cpp:78
virtual void DrawConstraint(DebugRenderer *inRenderer) const override
Definition VehicleConstraint.cpp:525
const Wheels & GetWheels() const
Get the state of the wheels.
Definition VehicleConstraint.h:98
virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override
Definition VehicleConstraint.cpp:483
virtual Ref< ConstraintSettings > GetConstraintSettings() const override
Debug function to convert a constraint to its settings, note that this will not save to which bodies ...
Definition VehicleConstraint.cpp:585
Definition VehicleConstraint.h:25
Ref< VehicleControllerSettings > mController
Defines how the vehicle can accelerate / decellerate.
Definition VehicleConstraint.h:37
Array< Ref< WheelSettings > > mWheels
List of wheels and their properties.
Definition VehicleConstraint.h:35
Array< VehicleAntiRollBar > mAntiRollBars
List of anti rollbars and their properties.
Definition VehicleConstraint.h:36
virtual void RestoreBinaryState(StreamIn &inStream) override
This function should not be called directly, it is used by sRestoreFromBinaryState.
Definition VehicleConstraint.cpp:51
Vec3 mForward
Vector indicating forward direction of the vehicle (in local space to the body)
Definition VehicleConstraint.h:33
Vec3 mUp
Vector indicating the up direction of the vehicle (in local space to the body)
Definition VehicleConstraint.h:32
float mMaxPitchRollAngle
Defines the maximum pitch/roll angle (rad), can be used to avoid the car from getting upside down....
Definition VehicleConstraint.h:34
virtual void SaveBinaryState(StreamOut &inStream) const override
Saves the contents of the constraint settings in binary form to inStream.
Definition VehicleConstraint.cpp:29
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
Base class for runtime data for a wheel, each VehicleController can implement a derived class of this...
Definition Wheel.h:45