Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ConstraintManager.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/Mutex.h>
10
12
13class IslandBuilder;
14class BodyManager;
15#ifdef JPH_DEBUG_RENDERER
16class DebugRenderer;
17#endif // JPH_DEBUG_RENDERER
18
21
24{
25public:
27
28#ifdef JPH_ENABLE_ASSERTS
30 ConstraintManager(PhysicsLockContext inContext) : mLockContext(inContext) { }
31#endif // JPH_ENABLE_ASSERTS
32
35 void Add(Constraint **inConstraints, int inNumber);
36
39 void Remove(Constraint **inConstraint, int inNumber);
40
43
45 inline uint32 GetNumConstraints() const { return (uint32)mConstraints.size(); }
46
48 void GetActiveConstraints(uint32 inStartConstraintIdx, uint32 inEndConstraintIdx, Constraint **outActiveConstraints, uint32 &outNumActiveConstraints) const;
49
51 static void sBuildIslands(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, IslandBuilder &ioBuilder, BodyManager &inBodyManager);
52
54 static void sSortConstraints(Constraint **inActiveConstraints, uint32 *inConstraintIdxBegin, uint32 *inConstraintIdxEnd);
55
57 static void sSetupVelocityConstraints(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, float inDeltaTime);
58
60 static void sSetupVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime);
61
63 static void sWarmStartVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inWarmStartImpulseRatio);
64
66 static void sWarmStartVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inWarmStartImpulseRatio, int &ioNumVelocitySteps);
67
69 static bool sSolveVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime);
70
72 static bool sSolvePositionConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime, float inBaumgarte);
73
75 static bool sSolvePositionConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime, float inBaumgarte, int &ioNumPositionSteps);
76
77#ifdef JPH_DEBUG_RENDERER
79 void DrawConstraints(DebugRenderer *inRenderer) const;
80
82 void DrawConstraintLimits(DebugRenderer *inRenderer) const;
83
85 void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const;
86#endif // JPH_DEBUG_RENDERER
87
89 void SaveState(StateRecorder &inStream) const;
90
92 bool RestoreState(StateRecorder &inStream);
93
95 void LockAllConstraints() { PhysicsLock::sLock(mConstraintsMutex JPH_IF_ENABLE_ASSERTS(, mLockContext, EPhysicsLockTypes::ConstraintsList)); }
96 void UnlockAllConstraints() { PhysicsLock::sUnlock(mConstraintsMutex JPH_IF_ENABLE_ASSERTS(, mLockContext, EPhysicsLockTypes::ConstraintsList)); }
97
98private:
99#ifdef JPH_ENABLE_ASSERTS
100 PhysicsLockContext mLockContext;
101#endif // JPH_ENABLE_ASSERTS
102 Constraints mConstraints;
103 mutable Mutex mConstraintsMutex;
104};
105
Array< Ref< Constraint > > Constraints
A list of constraints.
Definition: ConstraintManager.h:20
uint32_t uint32
Definition: Core.h:312
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
#define JPH_IF_ENABLE_ASSERTS(...)
Definition: IssueReporting.h:35
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
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
A constraint manager manages all constraints of the same type.
Definition: ConstraintManager.h:24
void SaveState(StateRecorder &inStream) const
Save state of constraints.
Definition: ConstraintManager.cpp:229
static void sSortConstraints(Constraint **inActiveConstraints, uint32 *inConstraintIdxBegin, uint32 *inConstraintIdxEnd)
In order to have a deterministic simulation, we need to sort the constraints of an island before solv...
Definition: ConstraintManager.cpp:102
Constraints GetConstraints() const
Get a list of all constraints.
Definition: ConstraintManager.cpp:62
static void sWarmStartVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inWarmStartImpulseRatio)
Apply last frame's impulses, must be called prior to SolveVelocityConstraints.
Definition: ConstraintManager.cpp:128
static bool sSolvePositionConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime, float inBaumgarte)
This function is called multiple times to iteratively come to a solution that meets all position cons...
Definition: ConstraintManager.cpp:166
void GetActiveConstraints(uint32 inStartConstraintIdx, uint32 inEndConstraintIdx, Constraint **outActiveConstraints, uint32 &outNumActiveConstraints) const
Determine the active constraints of a subset of the constraints.
Definition: ConstraintManager.cpp:70
static void sBuildIslands(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, IslandBuilder &ioBuilder, BodyManager &inBodyManager)
Link bodies to form islands.
Definition: ConstraintManager.cpp:91
void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const
Draw all constraint reference frames.
Definition: ConstraintManager.cpp:218
bool RestoreState(StateRecorder &inStream)
Restore the state of constraints. Returns false if failed.
Definition: ConstraintManager.cpp:240
static bool sSolveVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime)
This function is called multiple times to iteratively come to a solution that meets all velocity cons...
Definition: ConstraintManager.cpp:151
void Remove(Constraint **inConstraint, int inNumber)
Definition: ConstraintManager.cpp:35
void DrawConstraints(DebugRenderer *inRenderer) const
Draw all constraints.
Definition: ConstraintManager.cpp:198
uint32 GetNumConstraints() const
Get total number of constraints.
Definition: ConstraintManager.h:45
void DrawConstraintLimits(DebugRenderer *inRenderer) const
Draw all constraint limits.
Definition: ConstraintManager.cpp:208
JPH_OVERRIDE_NEW_DELETE void Add(Constraint **inConstraints, int inNumber)
Definition: ConstraintManager.cpp:16
void LockAllConstraints()
Lock all constraints. This should only be done during PhysicsSystem::Update().
Definition: ConstraintManager.h:95
void UnlockAllConstraints()
Definition: ConstraintManager.h:96
static void sSetupVelocityConstraints(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, float inDeltaTime)
Prior to solving the velocity constraints, you must call SetupVelocityConstraints once to precalculat...
Definition: ConstraintManager.cpp:109
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: Mutex.h:122
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11
static void sUnlock(LockType &inMutex JPH_IF_ENABLE_ASSERTS(, PhysicsLockContext inContext, EPhysicsLockTypes inType))
Definition: PhysicsLock.h:61
static void sLock(LockType &inMutex JPH_IF_ENABLE_ASSERTS(, PhysicsLockContext inContext, EPhysicsLockTypes inType))
Definition: PhysicsLock.h:54
Definition: StateRecorder.h:15