Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/ConstraintManager.h
9913 views
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
7
#include <Jolt/Physics/Constraints/Constraint.h>
8
#include <Jolt/Physics/PhysicsLock.h>
9
#include <Jolt/Core/Mutex.h>
10
11
JPH_NAMESPACE_BEGIN
12
13
class IslandBuilder;
14
class BodyManager;
15
class StateRecorderFilter;
16
#ifdef JPH_DEBUG_RENDERER
17
class DebugRenderer;
18
#endif // JPH_DEBUG_RENDERER
19
20
/// A list of constraints
21
using Constraints = Array<Ref<Constraint>>;
22
23
/// A constraint manager manages all constraints of the same type
24
class JPH_EXPORT ConstraintManager : public NonCopyable
25
{
26
public:
27
JPH_OVERRIDE_NEW_DELETE
28
29
#ifdef JPH_ENABLE_ASSERTS
30
/// Constructor
31
ConstraintManager(PhysicsLockContext inContext) : mLockContext(inContext) { }
32
#endif // JPH_ENABLE_ASSERTS
33
34
/// Add a new constraint. This is thread safe.
35
void Add(Constraint **inConstraints, int inNumber);
36
37
/// Remove a constraint. This is thread safe.
38
void Remove(Constraint **inConstraint, int inNumber);
39
40
/// Get a list of all constraints
41
Constraints GetConstraints() const;
42
43
/// Get total number of constraints
44
inline uint32 GetNumConstraints() const { return uint32(mConstraints.size()); }
45
46
/// Determine the active constraints of a subset of the constraints
47
void GetActiveConstraints(uint32 inStartConstraintIdx, uint32 inEndConstraintIdx, Constraint **outActiveConstraints, uint32 &outNumActiveConstraints) const;
48
49
/// Link bodies to form islands
50
static void sBuildIslands(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, IslandBuilder &ioBuilder, BodyManager &inBodyManager);
51
52
/// In order to have a deterministic simulation, we need to sort the constraints of an island before solving them
53
static void sSortConstraints(Constraint **inActiveConstraints, uint32 *inConstraintIdxBegin, uint32 *inConstraintIdxEnd);
54
55
/// Prior to solving the velocity constraints, you must call SetupVelocityConstraints once to precalculate values that are independent of velocity
56
static void sSetupVelocityConstraints(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, float inDeltaTime);
57
58
/// Apply last frame's impulses, must be called prior to SolveVelocityConstraints
59
template <class ConstraintCallback>
60
static void sWarmStartVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inWarmStartImpulseRatio, ConstraintCallback &ioCallback);
61
62
/// This function is called multiple times to iteratively come to a solution that meets all velocity constraints
63
static bool sSolveVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime);
64
65
/// This function is called multiple times to iteratively come to a solution that meets all position constraints
66
static bool sSolvePositionConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime, float inBaumgarte);
67
68
#ifdef JPH_DEBUG_RENDERER
69
/// Draw all constraints
70
void DrawConstraints(DebugRenderer *inRenderer) const;
71
72
/// Draw all constraint limits
73
void DrawConstraintLimits(DebugRenderer *inRenderer) const;
74
75
/// Draw all constraint reference frames
76
void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const;
77
#endif // JPH_DEBUG_RENDERER
78
79
/// Save state of constraints
80
void SaveState(StateRecorder &inStream, const StateRecorderFilter *inFilter) const;
81
82
/// Restore the state of constraints. Returns false if failed.
83
bool RestoreState(StateRecorder &inStream);
84
85
/// Lock all constraints. This should only be done during PhysicsSystem::Update().
86
void LockAllConstraints() { PhysicsLock::sLock(mConstraintsMutex JPH_IF_ENABLE_ASSERTS(, mLockContext, EPhysicsLockTypes::ConstraintsList)); }
87
void UnlockAllConstraints() { PhysicsLock::sUnlock(mConstraintsMutex JPH_IF_ENABLE_ASSERTS(, mLockContext, EPhysicsLockTypes::ConstraintsList)); }
88
89
private:
90
#ifdef JPH_ENABLE_ASSERTS
91
PhysicsLockContext mLockContext;
92
#endif // JPH_ENABLE_ASSERTS
93
Constraints mConstraints;
94
mutable Mutex mConstraintsMutex;
95
};
96
97
JPH_NAMESPACE_END
98
99