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 JPH_EXPORT ConstraintSettings : public SerializableObject, public RefTarget<ConstraintSettings>
65{
66public:
68
70
72 virtual void SaveBinaryState(StreamOut &inStream) const;
73
75 static ConstraintResult sRestoreFromBinaryState(StreamIn &inStream);
76
78 bool mEnabled = true;
79
82 uint32 mConstraintPriority = 0;
83
85 uint mNumVelocityStepsOverride = 0;
86
88 uint mNumPositionStepsOverride = 0;
89
91 float mDrawConstraintSize = 1.0f;
92
94 uint64 mUserData = 0;
95
96protected:
98 virtual void RestoreBinaryState(StreamIn &inStream);
99};
100
103{
104public:
106
108 explicit Constraint(const ConstraintSettings &inSettings) :
109#ifdef JPH_DEBUG_RENDERER
110 mDrawConstraintSize(inSettings.mDrawConstraintSize),
111#endif // JPH_DEBUG_RENDERER
112 mConstraintPriority(inSettings.mConstraintPriority),
113 mNumVelocityStepsOverride(uint8(inSettings.mNumVelocityStepsOverride)),
114 mNumPositionStepsOverride(uint8(inSettings.mNumPositionStepsOverride)),
115 mEnabled(inSettings.mEnabled),
116 mUserData(inSettings.mUserData)
117 {
118 JPH_ASSERT(inSettings.mNumVelocityStepsOverride < 256);
119 JPH_ASSERT(inSettings.mNumPositionStepsOverride < 256);
120 }
121
123 virtual ~Constraint() = default;
124
126 virtual EConstraintType GetType() const { return EConstraintType::Constraint; }
127
129 virtual EConstraintSubType GetSubType() const = 0;
130
133 uint32 GetConstraintPriority() const { return mConstraintPriority; }
134 void SetConstraintPriority(uint32 inPriority) { mConstraintPriority = inPriority; }
135
137 void SetNumVelocityStepsOverride(uint inN) { JPH_ASSERT(inN < 256); mNumVelocityStepsOverride = uint8(inN); }
138 uint GetNumVelocityStepsOverride() const { return mNumVelocityStepsOverride; }
139
141 void SetNumPositionStepsOverride(uint inN) { JPH_ASSERT(inN < 256); mNumPositionStepsOverride = uint8(inN); }
142 uint GetNumPositionStepsOverride() const { return mNumPositionStepsOverride; }
143
148 void SetEnabled(bool inEnabled) { mEnabled = inEnabled; }
149
151 bool GetEnabled() const { return mEnabled; }
152
154 uint64 GetUserData() const { return mUserData; }
155 void SetUserData(uint64 inUserData) { mUserData = inUserData; }
156
161 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) = 0;
162
167 virtual void ResetWarmStart() = 0;
168
171 virtual bool IsActive() const { return mEnabled; }
172 virtual void SetupVelocityConstraint(float inDeltaTime) = 0;
173 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) = 0;
174 virtual bool SolveVelocityConstraint(float inDeltaTime) = 0;
175 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) = 0;
177
179 virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) = 0;
180
182 virtual uint BuildIslandSplits(LargeIslandSplitter &ioSplitter) const = 0;
183
184#ifdef JPH_DEBUG_RENDERER
185 // Drawing interface
186 virtual void DrawConstraint(DebugRenderer *inRenderer) const = 0;
187 virtual void DrawConstraintLimits([[maybe_unused]] DebugRenderer *inRenderer) const { }
188 virtual void DrawConstraintReferenceFrame([[maybe_unused]] DebugRenderer *inRenderer) const { }
189
191 float GetDrawConstraintSize() const { return mDrawConstraintSize; }
192 void SetDrawConstraintSize(float inSize) { mDrawConstraintSize = inSize; }
193#endif // JPH_DEBUG_RENDERER
194
196 virtual void SaveState(StateRecorder &inStream) const;
197
199 virtual void RestoreState(StateRecorder &inStream);
200
203
204protected:
206 void ToConstraintSettings(ConstraintSettings &outSettings) const;
207
208#ifdef JPH_DEBUG_RENDERER
211#endif // JPH_DEBUG_RENDERER
212
213private:
214 friend class ConstraintManager;
215
217 static constexpr uint32 cInvalidConstraintIndex = 0xffffffff;
218
220 uint32 mConstraintIndex = cInvalidConstraintIndex;
221
223 uint32 mConstraintPriority = 0;
224
226 uint8 mNumVelocityStepsOverride = 0;
227
229 uint8 mNumPositionStepsOverride = 0;
230
232 bool mEnabled = true;
233
235 uint64 mUserData;
236};
237
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.
std::uint8_t uint8
Definition: Core.h:453
#define JPH_EXPORT
Definition: Core.h:236
std::uint64_t uint64
Definition: Core.h:456
unsigned int uint
Definition: Core.h:452
#define JPH_NAMESPACE_END
Definition: Core.h:378
std::uint32_t uint32
Definition: Core.h:455
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:31
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(linkage, class_name)
Definition: SerializableObject.h:109
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:103
virtual ~Constraint()=default
Virtual destructor.
virtual void SetupVelocityConstraint(float inDeltaTime)=0
virtual bool IsActive() const
Definition: Constraint.h:171
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:151
void SetUserData(uint64 inUserData)
Definition: Constraint.h:155
virtual bool SolveVelocityConstraint(float inDeltaTime)=0
virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM)=0
void SetConstraintPriority(uint32 inPriority)
Definition: Constraint.h:134
void SetNumVelocityStepsOverride(uint inN)
Used only when the constraint is active. Override for the number of solver velocity iterations to run...
Definition: Constraint.h:137
uint64 GetUserData() const
Access to the user data, can be used for anything by the application.
Definition: Constraint.h:154
virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const
Definition: Constraint.h:187
uint GetNumVelocityStepsOverride() const
Definition: Constraint.h:138
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.
uint32 GetConstraintPriority() const
Definition: Constraint.h:133
virtual EConstraintType GetType() const
Get the type of a constraint.
Definition: Constraint.h:126
float GetDrawConstraintSize() const
Size of constraint when drawing it through the debug renderer.
Definition: Constraint.h:191
void SetNumPositionStepsOverride(uint inN)
Used only when the constraint is active. Override for the number of solver position iterations to run...
Definition: Constraint.h:141
virtual void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const
Definition: Constraint.h:188
void SetEnabled(bool inEnabled)
Definition: Constraint.h:148
virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio)=0
virtual void DrawConstraint(DebugRenderer *inRenderer) const =0
uint GetNumPositionStepsOverride() const
Definition: Constraint.h:142
virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte)=0
JPH_OVERRIDE_NEW_DELETE Constraint(const ConstraintSettings &inSettings)
Constructor.
Definition: Constraint.h:108
void SetDrawConstraintSize(float inSize)
Definition: Constraint.h:192
float mDrawConstraintSize
Size of constraint when drawing it through the debug renderer.
Definition: Constraint.h:210
virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager)=0
Link bodies that are connected by this constraint in the island builder.
virtual void ResetWarmStart()=0
A constraint manager manages all constraints of the same type.
Definition: ConstraintManager.h:25
Class used to store the configuration of a constraint. Allows run-time creation of constraints.
Definition: Constraint.h:65
uint mNumPositionStepsOverride
Used only when the constraint is active. Override for the number of solver position iterations to run...
Definition: Constraint.h:88
uint mNumVelocityStepsOverride
Used only when the constraint is active. Override for the number of solver velocity iterations to run...
Definition: Constraint.h:85
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
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:12
Definition: SerializableObject.h:156
Definition: StateRecorder.h:48
Simple binary input stream.
Definition: StreamIn.h:13
Simple binary output stream.
Definition: StreamOut.h:13
Definition: Vec3.h:17