Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
PhysicsSystem.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
16
18
19class JobSystem;
20class StateRecorder;
21class TempAllocator;
23
28{
29public:
31
33 PhysicsSystem() : mContactManager(mPhysicsSettings) JPH_IF_ENABLE_ASSERTS(, mConstraintManager(&mBodyManager)) { }
35
44 void Init(uint inMaxBodies, uint inNumBodyMutexes, uint inMaxBodyPairs, uint inMaxContactConstraints, const BroadPhaseLayerInterface &inBroadPhaseLayerInterface, const ObjectVsBroadPhaseLayerFilter &inObjectVsBroadPhaseLayerFilter, const ObjectLayerPairFilter &inObjectLayerPairFilter);
45
47 void SetBodyActivationListener(BodyActivationListener *inListener) { mBodyManager.SetBodyActivationListener(inListener); }
48 BodyActivationListener * GetBodyActivationListener() const { return mBodyManager.GetBodyActivationListener(); }
49
51 void SetContactListener(ContactListener *inListener) { mContactManager.SetContactListener(inListener); }
52 ContactListener * GetContactListener() const { return mContactManager.GetContactListener(); }
53
56 void SetCombineFriction(ContactConstraintManager::CombineFunction inCombineFriction) { mContactManager.SetCombineFriction(inCombineFriction); }
57 ContactConstraintManager::CombineFunction GetCombineFriction() const { return mContactManager.GetCombineFriction(); }
58
61 void SetCombineRestitution(ContactConstraintManager::CombineFunction inCombineRestition) { mContactManager.SetCombineRestitution(inCombineRestition); }
62 ContactConstraintManager::CombineFunction GetCombineRestitution() const { return mContactManager.GetCombineRestitution(); }
63
65 void SetPhysicsSettings(const PhysicsSettings &inSettings) { mPhysicsSettings = inSettings; }
66 const PhysicsSettings & GetPhysicsSettings() const { return mPhysicsSettings; }
67
69 const BodyInterface & GetBodyInterface() const { return mBodyInterfaceLocking; }
70 BodyInterface & GetBodyInterface() { return mBodyInterfaceLocking; }
71 const BodyInterface & GetBodyInterfaceNoLock() const { return mBodyInterfaceNoLock; }
72 BodyInterface & GetBodyInterfaceNoLock() { return mBodyInterfaceNoLock; }
73
75 const BroadPhaseQuery & GetBroadPhaseQuery() const { return *mBroadPhase; }
76
78 const NarrowPhaseQuery & GetNarrowPhaseQuery() const { return mNarrowPhaseQueryLocking; }
79 const NarrowPhaseQuery & GetNarrowPhaseQueryNoLock() const { return mNarrowPhaseQueryNoLock; }
80
82 void AddConstraint(Constraint *inConstraint) { mConstraintManager.Add(&inConstraint, 1); }
83
85 void RemoveConstraint(Constraint *inConstraint) { mConstraintManager.Remove(&inConstraint, 1); }
86
88 void AddConstraints(Constraint **inConstraints, int inNumber) { mConstraintManager.Add(inConstraints, inNumber); }
89
91 void RemoveConstraints(Constraint **inConstraints, int inNumber) { mConstraintManager.Remove(inConstraints, inNumber); }
92
94 Constraints GetConstraints() const { return mConstraintManager.GetConstraints(); }
95
97 void OptimizeBroadPhase();
98
100 void AddStepListener(PhysicsStepListener *inListener);
101
103 void RemoveStepListener(PhysicsStepListener *inListener);
104
109 EPhysicsUpdateError Update(float inDeltaTime, int inCollisionSteps, TempAllocator *inTempAllocator, JobSystem *inJobSystem);
110
112 void SaveState(StateRecorder &inStream, EStateRecorderState inState = EStateRecorderState::All, const StateRecorderFilter *inFilter = nullptr) const;
113
115 bool RestoreState(StateRecorder &inStream);
116
118 void SaveBodyState(const Body &inBody, StateRecorder &inStream) const;
119
121 void RestoreBodyState(Body &ioBody, StateRecorder &inStream);
122
123#ifdef JPH_DEBUG_RENDERER
124 // Drawing properties
126
128 void DrawBodies(const BodyManager::DrawSettings &inSettings, DebugRenderer *inRenderer, const BodyDrawFilter *inBodyFilter = nullptr) { mBodyManager.Draw(inSettings, mPhysicsSettings, inRenderer, inBodyFilter); }
129
131 void DrawConstraints(DebugRenderer *inRenderer) { mConstraintManager.DrawConstraints(inRenderer); }
132
134 void DrawConstraintLimits(DebugRenderer *inRenderer) { mConstraintManager.DrawConstraintLimits(inRenderer); }
135
137 void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) { mConstraintManager.DrawConstraintReferenceFrame(inRenderer); }
138#endif // JPH_DEBUG_RENDERER
139
141 void SetGravity(Vec3Arg inGravity) { mGravity = inGravity; }
142 Vec3 GetGravity() const { return mGravity; }
143
145 inline const BodyLockInterfaceNoLock & GetBodyLockInterfaceNoLock() const { return mBodyLockInterfaceNoLock; }
146
148 inline const BodyLockInterfaceLocking & GetBodyLockInterface() const { return mBodyLockInterfaceLocking; }
149
151 DefaultBroadPhaseLayerFilter GetDefaultBroadPhaseLayerFilter(ObjectLayer inLayer) const { return DefaultBroadPhaseLayerFilter(*mObjectVsBroadPhaseLayerFilter, inLayer); }
152
154 DefaultObjectLayerFilter GetDefaultLayerFilter(ObjectLayer inLayer) const { return DefaultObjectLayerFilter(*mObjectLayerPairFilter, inLayer); }
155
157 uint GetNumBodies() const { return mBodyManager.GetNumBodies(); }
158
160 uint32 GetNumActiveBodies(EBodyType inType) const { return mBodyManager.GetNumActiveBodies(inType); }
161
163 uint GetMaxBodies() const { return mBodyManager.GetMaxBodies(); }
164
167
169 BodyStats GetBodyStats() const { return mBodyManager.GetBodyStats(); }
170
173 void GetBodies(BodyIDVector &outBodyIDs) const { return mBodyManager.GetBodyIDs(outBodyIDs); }
174
178 void GetActiveBodies(EBodyType inType, BodyIDVector &outBodyIDs) const { return mBodyManager.GetActiveBodies(inType, outBodyIDs); }
179
182 const BodyID * GetActiveBodiesUnsafe(EBodyType inType) const { return mBodyManager.GetActiveBodiesUnsafe(inType); }
183
189 bool WereBodiesInContact(const BodyID &inBody1ID, const BodyID &inBody2ID) const { return mContactManager.WereBodiesInContact(inBody1ID, inBody2ID); }
190
191#ifdef JPH_TRACK_BROADPHASE_STATS
193 void ReportBroadphaseStats() { mBroadPhase->ReportStats(); }
194#endif // JPH_TRACK_BROADPHASE_STATS
195
196private:
198
199 // Various job entry points
200 void JobStepListeners(PhysicsUpdateContext::Step *ioStep);
201 void JobDetermineActiveConstraints(PhysicsUpdateContext::Step *ioStep) const;
202 void JobApplyGravity(const PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep);
203 void JobSetupVelocityConstraints(float inDeltaTime, PhysicsUpdateContext::Step *ioStep) const;
204 void JobBuildIslandsFromConstraints(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep);
205 void JobFindCollisions(PhysicsUpdateContext::Step *ioStep, int inJobIndex);
206 void JobFinalizeIslands(PhysicsUpdateContext *ioContext);
207 void JobBodySetIslandIndex();
208 void JobSolveVelocityConstraints(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep);
209 void JobPreIntegrateVelocity(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep);
210 void JobIntegrateVelocity(const PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep);
211 void JobPostIntegrateVelocity(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep) const;
212 void JobFindCCDContacts(const PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep);
213 void JobResolveCCDContacts(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep);
214 void JobContactRemovedCallbacks(const PhysicsUpdateContext::Step *ioStep);
215 void JobSolvePositionConstraints(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep);
216 void JobSoftBodyPrepare(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep);
217 void JobSoftBodyCollide(PhysicsUpdateContext *ioContext) const;
218 void JobSoftBodySimulate(PhysicsUpdateContext *ioContext, uint inThreadIndex) const;
219 void JobSoftBodyFinalize(PhysicsUpdateContext *ioContext);
220
222 void TrySpawnJobFindCollisions(PhysicsUpdateContext::Step *ioStep) const;
223
224 using ContactAllocator = ContactConstraintManager::ContactAllocator;
225
227 void ProcessBodyPair(ContactAllocator &ioContactAllocator, const BodyPair &inBodyPair);
228
230 class BodiesToSleep;
231
233 void CheckSleepAndUpdateBounds(uint32 inIslandIndex, const PhysicsUpdateContext *ioContext, const PhysicsUpdateContext::Step *ioStep, BodiesToSleep &ioBodiesToSleep);
234
236 static constexpr int cDetermineActiveConstraintsBatchSize = 64;
237
239 static constexpr int cApplyGravityBatchSize = 64;
240
242 static constexpr int cActiveBodiesBatchSize = 16;
243
245 static constexpr int cIntegrateVelocityBatchSize = 64;
246
248 static constexpr int cNarrowPhaseBatchSize = 16;
249
251 static constexpr int cNumCCDBodiesPerJob = 4;
252
254 const ObjectVsBroadPhaseLayerFilter *mObjectVsBroadPhaseLayerFilter = nullptr;
255
257 const ObjectLayerPairFilter *mObjectLayerPairFilter = nullptr;
258
260 BodyManager mBodyManager;
261
263 BodyLockInterfaceNoLock mBodyLockInterfaceNoLock { mBodyManager };
264 BodyLockInterfaceLocking mBodyLockInterfaceLocking { mBodyManager };
265
267 BodyInterface mBodyInterfaceNoLock;
268 BodyInterface mBodyInterfaceLocking;
269
271 NarrowPhaseQuery mNarrowPhaseQueryNoLock;
272 NarrowPhaseQuery mNarrowPhaseQueryLocking;
273
275 BroadPhase * mBroadPhase = nullptr;
276
278 PhysicsSettings mPhysicsSettings;
279
281 ContactConstraintManager mContactManager;
282
284 ConstraintManager mConstraintManager;
285
287 IslandBuilder mIslandBuilder;
288
290 LargeIslandSplitter mLargeIslandSplitter;
291
293 Mutex mStepListenersMutex;
294
296 using StepListeners = Array<PhysicsStepListener *>;
297 StepListeners mStepListeners;
298
300 Vec3 mGravity = Vec3(0, -9.81f, 0);
301
303 float mPreviousStepDeltaTime = 0.0f;
304};
305
Array< BodyID > BodyIDVector
Array of body ID's.
Definition: BodyManager.h:28
EBodyType
Type of body.
Definition: BodyType.h:11
Array< Ref< Constraint > > Constraints
A list of constraints.
Definition: ConstraintManager.h:21
#define JPH_EXPORT
Definition: Core.h:214
unsigned int uint
Definition: Core.h:426
#define JPH_NAMESPACE_END
Definition: Core.h:354
std::uint32_t uint32
Definition: Core.h:429
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:348
EPhysicsUpdateError
Enum used by PhysicsSystem to report error conditions during the PhysicsSystem::Update call....
Definition: EPhysicsUpdateError.h:11
#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
uint16 ObjectLayer
Definition: ObjectLayer.h:16
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
EStateRecorderState
A bit field that determines which aspects of the simulation to save.
Definition: StateRecorder.h:18
Definition: BodyActivationListener.h:14
Class function to filter out bodies for debug rendering, returns true if body should be rendered.
Definition: BodyFilter.h:89
Definition: Body.h:35
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition: BodyID.h:13
Definition: BodyInterface.h:35
Implementation that uses the body manager to lock the correct mutex for a body.
Definition: BodyLockInterface.h:78
Implementation that performs no locking (assumes the lock has already been taken)
Definition: BodyLockInterface.h:58
Class that contains all bodies.
Definition: BodyManager.h:32
Used to do coarse collision detection operations to quickly prune out bodies that will not collide.
Definition: BroadPhase.h:26
Interface that the application should implement to allow mapping object layers to broadphase layers.
Definition: BroadPhaseLayer.h:56
Definition: BroadPhaseQuery.h:29
Base class for all physics constraints. A constraint removes one or more degrees of freedom for a rig...
Definition: Constraint.h:103
A constraint manager manages all constraints of the same type.
Definition: ConstraintManager.h:25
Contacts are allocated in a lock free hash map.
Definition: ContactConstraintManager.h:80
Definition: ContactConstraintManager.h:28
float(*)(const Body &inBody1, const SubShapeID &inSubShapeID1, const Body &inBody2, const SubShapeID &inSubShapeID2) CombineFunction
Definition: ContactConstraintManager.h:48
Definition: ContactListener.h:68
Simple triangle renderer for debugging purposes.
Definition: DebugRenderer.h:30
Default filter class that uses the pair filter in combination with a specified layer to filter layers...
Definition: BroadPhaseLayer.h:103
Default filter class that uses the pair filter in combination with a specified layer to filter layers...
Definition: ObjectLayer.h:64
Keeps track of connected bodies and builds islands for multithreaded velocity/position update.
Definition: IslandBuilder.h:19
Definition: JobSystem.h:70
Definition: LargeIslandSplitter.h:24
Definition: Mutex.h:122
Definition: NarrowPhaseQuery.h:23
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11
Filter class to test if two objects can collide based on their object layer. Used while finding colli...
Definition: ObjectLayer.h:50
Class to test if an object can collide with a broadphase layer. Used while finding collision pairs.
Definition: BroadPhaseLayer.h:75
A listener class that receives a callback before every physics simulation step.
Definition: PhysicsStepListener.h:13
Definition: PhysicsSystem.h:28
void GetBodies(BodyIDVector &outBodyIDs) const
Definition: PhysicsSystem.h:173
void DrawConstraintReferenceFrame(DebugRenderer *inRenderer)
Draw the constraint reference frames only (debugging purposes)
Definition: PhysicsSystem.h:137
const NarrowPhaseQuery & GetNarrowPhaseQuery() const
Interface that allows fine collision queries against first the broad phase and then the narrow phase.
Definition: PhysicsSystem.h:78
void RemoveConstraints(Constraint **inConstraints, int inNumber)
Batch remove constraints. Note that the inConstraints array is allowed to have nullptrs,...
Definition: PhysicsSystem.h:91
uint GetNumBodies() const
Gets the current amount of bodies that are in the body manager.
Definition: PhysicsSystem.h:157
uint32 GetNumActiveBodies(EBodyType inType) const
Gets the current amount of active bodies that are in the body manager.
Definition: PhysicsSystem.h:160
void SetPhysicsSettings(const PhysicsSettings &inSettings)
Control the main constants of the physics simulation.
Definition: PhysicsSystem.h:65
const BodyInterface & GetBodyInterface() const
Access to the body interface. This interface allows to to create / remove bodies and to change their ...
Definition: PhysicsSystem.h:69
const BroadPhaseQuery & GetBroadPhaseQuery() const
Access to the broadphase interface that allows coarse collision queries.
Definition: PhysicsSystem.h:75
const BodyLockInterfaceLocking & GetBodyLockInterface() const
Returns a locking interface that locks the body so other threads cannot modify it.
Definition: PhysicsSystem.h:148
const NarrowPhaseQuery & GetNarrowPhaseQueryNoLock() const
Version that does not lock the bodies, use with great care!
Definition: PhysicsSystem.h:79
void DrawConstraints(DebugRenderer *inRenderer)
Draw the constraints only (debugging purposes)
Definition: PhysicsSystem.h:131
const PhysicsSettings & GetPhysicsSettings() const
Definition: PhysicsSystem.h:66
const BodyInterface & GetBodyInterfaceNoLock() const
Version that does not lock the bodies, use with great care!
Definition: PhysicsSystem.h:71
BodyInterface & GetBodyInterface()
Definition: PhysicsSystem.h:70
void DrawConstraintLimits(DebugRenderer *inRenderer)
Draw the constraint limits only (debugging purposes)
Definition: PhysicsSystem.h:134
Constraints GetConstraints() const
Get a list of all constraints.
Definition: PhysicsSystem.h:94
void AddConstraint(Constraint *inConstraint)
Add constraint to the world.
Definition: PhysicsSystem.h:82
Vec3 GetGravity() const
Definition: PhysicsSystem.h:142
uint GetMaxBodies() const
Get the maximum amount of bodies that this physics system supports.
Definition: PhysicsSystem.h:163
void RemoveConstraint(Constraint *inConstraint)
Remove constraint from the world.
Definition: PhysicsSystem.h:85
void DrawBodies(const BodyManager::DrawSettings &inSettings, DebugRenderer *inRenderer, const BodyDrawFilter *inBodyFilter=nullptr)
Draw the state of the bodies (debugging purposes)
Definition: PhysicsSystem.h:128
BodyActivationListener * GetBodyActivationListener() const
Definition: PhysicsSystem.h:48
DefaultBroadPhaseLayerFilter GetDefaultBroadPhaseLayerFilter(ObjectLayer inLayer) const
Get an broadphase layer filter that uses the default pair filter and a specified object layer to dete...
Definition: PhysicsSystem.h:151
BodyInterface & GetBodyInterfaceNoLock()
Version that does not lock the bodies, use with great care!
Definition: PhysicsSystem.h:72
void GetActiveBodies(EBodyType inType, BodyIDVector &outBodyIDs) const
Definition: PhysicsSystem.h:178
ContactConstraintManager::CombineFunction GetCombineRestitution() const
Definition: PhysicsSystem.h:62
void SetCombineRestitution(ContactConstraintManager::CombineFunction inCombineRestition)
Definition: PhysicsSystem.h:61
JPH_OVERRIDE_NEW_DELETE PhysicsSystem()
Constructor / Destructor.
Definition: PhysicsSystem.h:33
static bool sDrawMotionQualityLinearCast
Draw debug info for objects that perform continuous collision detection through the linear cast motio...
Definition: PhysicsSystem.h:125
void SetGravity(Vec3Arg inGravity)
Set gravity value.
Definition: PhysicsSystem.h:141
const BodyLockInterfaceNoLock & GetBodyLockInterfaceNoLock() const
Returns a locking interface that won't actually lock the body. Use with great care!
Definition: PhysicsSystem.h:145
BodyStats GetBodyStats() const
Get stats about the bodies in the body manager (slow, iterates through all bodies)
Definition: PhysicsSystem.h:169
void AddConstraints(Constraint **inConstraints, int inNumber)
Batch add constraints. Note that the inConstraints array is allowed to have nullptrs,...
Definition: PhysicsSystem.h:88
bool WereBodiesInContact(const BodyID &inBody1ID, const BodyID &inBody2ID) const
Definition: PhysicsSystem.h:189
DefaultObjectLayerFilter GetDefaultLayerFilter(ObjectLayer inLayer) const
Get an object layer filter that uses the default pair filter and a specified layer to determine if la...
Definition: PhysicsSystem.h:154
void SetContactListener(ContactListener *inListener)
Listener that is notified whenever a contact point between two bodies is added/updated/removed.
Definition: PhysicsSystem.h:51
ContactConstraintManager::CombineFunction GetCombineFriction() const
Definition: PhysicsSystem.h:57
void SetCombineFriction(ContactConstraintManager::CombineFunction inCombineFriction)
Definition: PhysicsSystem.h:56
const BodyID * GetActiveBodiesUnsafe(EBodyType inType) const
Definition: PhysicsSystem.h:182
void SetBodyActivationListener(BodyActivationListener *inListener)
Listener that is notified whenever a body is activated/deactivated.
Definition: PhysicsSystem.h:47
ContactListener * GetContactListener() const
Definition: PhysicsSystem.h:52
Information used during the Update call.
Definition: PhysicsUpdateContext.h:24
User callbacks that allow determining which parts of the simulation should be saved by a StateRecorde...
Definition: StateRecorder.h:29
Definition: StateRecorder.h:48
Definition: TempAllocator.h:16
Definition: Vec3.h:16
Helper struct that counts the number of bodies of each type.
Definition: BodyManager.h:50
Draw settings.
Definition: BodyManager.h:219
Structure that holds a body pair.
Definition: BodyPair.h:14
Definition: PhysicsSettings.h:28
Contains the information needed to cast a body through the scene to do continuous collision detection...
Definition: PhysicsUpdateContext.h:94
Structure that contains data needed for each collision step.
Definition: PhysicsUpdateContext.h:52