Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Constraint.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
9#include <Jolt/Core/Result.h>
11
13
14class BodyID;
15class IslandBuilder;
17class BodyManager;
18class StateRecorder;
19class StreamIn;
20class StreamOut;
21#ifdef JPH_DEBUG_RENDERER
22class DebugRenderer;
23#endif // JPH_DEBUG_RENDERER
24
27{
30};
31
34{
35 Fixed,
36 Point,
37 Hinge,
38 Slider,
40 Cone,
42 SixDOF,
43 Path,
44 Vehicle,
46 Gear,
47 Pulley,
48
50 User1,
51 User2,
52 User3,
53 User4
54};
55
58{
61};
62
64class ConstraintSettings : public SerializableObject, public RefTarget<ConstraintSettings>
65{
66public:
68
70
72 virtual void SaveBinaryState(StreamOut &inStream) const;
73
76
78 bool mEnabled = true;
79
82
85
87 float mDrawConstraintSize = 1.0f;
88
91
92protected:
94 virtual void RestoreBinaryState(StreamIn &inStream);
95};
96
99{
100public:
102
104 explicit Constraint(const ConstraintSettings &inSettings) :
105#ifdef JPH_DEBUG_RENDERER
107#endif // JPH_DEBUG_RENDERER
110 mEnabled(inSettings.mEnabled),
111 mUserData(inSettings.mUserData)
112 {
113 }
114
116 virtual ~Constraint() = default;
117
119 virtual EConstraintType GetType() const { return EConstraintType::Constraint; }
120
122 virtual EConstraintSubType GetSubType() const = 0;
123
127
131
136 void SetEnabled(bool inEnabled) { mEnabled = inEnabled; }
137
139 bool GetEnabled() const { return mEnabled; }
140
142 uint64 GetUserData() const { return mUserData; }
143 void SetUserData(uint64 inUserData) { mUserData = inUserData; }
144
149 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) = 0;
150
153 virtual bool IsActive() const { return mEnabled; }
154 virtual void SetupVelocityConstraint(float inDeltaTime) = 0;
155 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) = 0;
156 virtual bool SolveVelocityConstraint(float inDeltaTime) = 0;
157 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) = 0;
159
161 virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) = 0;
162
164 virtual uint BuildIslandSplits(LargeIslandSplitter &ioSplitter) const = 0;
165
166#ifdef JPH_DEBUG_RENDERER
167 // Drawing interface
168 virtual void DrawConstraint(DebugRenderer *inRenderer) const = 0;
169 virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const { }
170 virtual void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const { }
171
174 void SetDrawConstraintSize(float inSize) { mDrawConstraintSize = inSize; }
175#endif // JPH_DEBUG_RENDERER
176
178 virtual void SaveState(StateRecorder &inStream) const;
179
181 virtual void RestoreState(StateRecorder &inStream);
182
185
186protected:
188 void ToConstraintSettings(ConstraintSettings &outSettings) const;
189
190#ifdef JPH_DEBUG_RENDERER
193#endif // JPH_DEBUG_RENDERER
194
195private:
196 friend class ConstraintManager;
197
199 static constexpr uint32 cInvalidConstraintIndex = 0xffffffff;
200
202 uint32 mConstraintIndex = cInvalidConstraintIndex;
203
206
209
211 bool mEnabled = true;
212
215};
216
EConstraintType
Enum to identify constraint type.
Definition: Constraint.h:27
EConstraintSpace
Certain constraints support setting them up in local or world space. This governs what is used.
Definition: Constraint.h:58
@ LocalToBodyCOM
All constraint properties are specified in local space to center of mass of the bodies that are being...
@ WorldSpace
All constraint properties are specified in world space.
EConstraintSubType
Enum to identify constraint sub type.
Definition: Constraint.h:34
@ User1
User defined constraint types start here.
uint32_t uint32
Definition: Core.h:312
unsigned int uint
Definition: Core.h:309
#define JPH_NAMESPACE_END
Definition: Core.h:240
uint64_t uint64
Definition: Core.h:313
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(class_name)
Definition: SerializableObject.h:100
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 ~Constraint()=default
Virtual destructor.
virtual void SetupVelocityConstraint(float inDeltaTime)=0
virtual bool IsActive() const
Definition: Constraint.h:153
int GetNumVelocityStepsOverride() const
Definition: Constraint.h:126
virtual Ref< ConstraintSettings > GetConstraintSettings() const =0
Debug function to convert a constraint to its settings, note that this will not save to which bodies ...
bool GetEnabled() const
Test if a constraint is enabled.
Definition: Constraint.h:139
void SetUserData(uint64 inUserData)
Definition: Constraint.h:143
virtual bool SolveVelocityConstraint(float inDeltaTime)=0
virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM)=0
uint64 GetUserData() const
Access to the user data, can be used for anything by the application.
Definition: Constraint.h:142
virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const
Definition: Constraint.h:169
virtual EConstraintSubType GetSubType() const =0
Get the sub type of a constraint.
virtual uint BuildIslandSplits(LargeIslandSplitter &ioSplitter) const =0
Link bodies that are connected by this constraint in the same split. Returns the split index.
void SetNumVelocityStepsOverride(int inN)
Override for the number of solver velocity iterations to run, the total amount of iterations is the m...
Definition: Constraint.h:125
virtual EConstraintType GetType() const
Get the type of a constraint.
Definition: Constraint.h:119
float GetDrawConstraintSize() const
Size of constraint when drawing it through the debug renderer.
Definition: Constraint.h:173
void SetNumPositionStepsOverride(int inN)
Override for the number of position velocity iterations to run, the total amount of iterations is the...
Definition: Constraint.h:129
int GetNumPositionStepsOverride() const
Definition: Constraint.h:130
virtual void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const
Definition: Constraint.h:170
void SetEnabled(bool inEnabled)
Definition: Constraint.h:136
virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio)=0
virtual void DrawConstraint(DebugRenderer *inRenderer) const =0
virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte)=0
JPH_OVERRIDE_NEW_DELETE Constraint(const ConstraintSettings &inSettings)
Constructor.
Definition: Constraint.h:104
void SetDrawConstraintSize(float inSize)
Definition: Constraint.h:174
float mDrawConstraintSize
Size of constraint when drawing it through the debug renderer.
Definition: Constraint.h:192
virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager)=0
Link bodies that are connected by this constraint in the island builder.
A constraint manager manages all constraints of the same type.
Definition: ConstraintManager.h:24
Class used to store the configuration of a constraint. Allows run-time creation of constraints.
Definition: Constraint.h:65
static ConstraintResult sRestoreFromBinaryState(StreamIn &inStream)
Creates a constraint of the correct type and restores its contents from the binary stream inStream.
Definition: Constraint.cpp:45
int mNumPositionStepsOverride
Override for the number of position velocity iterations to run, the total amount of iterations is the...
Definition: Constraint.h:84
bool mEnabled
If this constraint is enabled initially. Use Constraint::SetEnabled to toggle after creation.
Definition: Constraint.h:78
virtual void SaveBinaryState(StreamOut &inStream) const
Saves the contents of the constraint settings in binary form to inStream.
Definition: Constraint.cpp:27
uint64 mUserData
User data value (can be used by application)
Definition: Constraint.h:90
int mNumVelocityStepsOverride
Override for the number of solver velocity iterations to run, the total amount of iterations is the m...
Definition: Constraint.h:81
virtual void RestoreBinaryState(StreamIn &inStream)
This function should not be called directly, it is used by sRestoreFromBinaryState.
Definition: Constraint.cpp:36
float mDrawConstraintSize
Size of constraint when drawing it through the debug renderer.
Definition: Constraint.h:87
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
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11
Definition: Reference.h:101
Definition: Reference.h:35
Helper class that either contains a valid result or an error.
Definition: Result.h:15
Definition: SerializableObject.h:147
Definition: StateRecorder.h:15
Simple binary input stream.
Definition: StreamIn.h:11
Simple binary output stream.
Definition: StreamOut.h:11
Definition: Vec3.h:16