Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
IslandBuilder.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/Atomics.h>
10
12
13class TempAllocator;
14
15//#define JPH_VALIDATE_ISLAND_BUILDER
16
19{
20public:
23
25 void Init(uint32 inMaxActiveBodies);
26
28 void PrepareContactConstraints(uint32 inMaxContactConstraints, TempAllocator *inTempAllocator);
29
31 void PrepareNonContactConstraints(uint32 inNumConstraints, TempAllocator *inTempAllocator);
32
34 void LinkBodies(uint32 inFirst, uint32 inSecond);
35
37 void LinkConstraint(uint32 inConstraintIndex, uint32 inFirst, uint32 inSecond);
38
40 void LinkContact(uint32 inContactIndex, uint32 inFirst, uint32 inSecond);
41
43 void Finalize(const BodyID *inActiveBodies, uint32 inNumActiveBodies, uint32 inNumContacts, TempAllocator *inTempAllocator);
44
46 uint32 GetNumIslands() const { return mNumIslands; }
47
49 void GetBodiesInIsland(uint32 inIslandIndex, BodyID *&outBodiesBegin, BodyID *&outBodiesEnd) const;
50 bool GetConstraintsInIsland(uint32 inIslandIndex, uint32 *&outConstraintsBegin, uint32 *&outConstraintsEnd) const;
51 bool GetContactsInIsland(uint32 inIslandIndex, uint32 *&outContactsBegin, uint32 *&outContactsEnd) const;
52
54 void SetNumPositionSteps(uint32 inIslandIndex, uint inNumPositionSteps) { JPH_ASSERT(inIslandIndex < mNumIslands); JPH_ASSERT(inNumPositionSteps < 256); mNumPositionSteps[inIslandIndex] = uint8(inNumPositionSteps); }
55 uint GetNumPositionSteps(uint32 inIslandIndex) const { JPH_ASSERT(inIslandIndex < mNumIslands); return mNumPositionSteps[inIslandIndex]; }
56
58 void ResetIslands(TempAllocator *inTempAllocator);
59
60private:
62 uint32 GetLowestBodyIndex(uint32 inActiveBodyIndex) const;
63
64#ifdef JPH_VALIDATE_ISLAND_BUILDER
66 void ValidateIslands(uint32 inNumActiveBodies) const;
67#endif
68
69 // Helper functions to build various islands
70 void BuildBodyIslands(const BodyID *inActiveBodies, uint32 inNumActiveBodies, TempAllocator *inTempAllocator);
71 void BuildConstraintIslands(const uint32 *inConstraintToBody, uint32 inNumConstraints, uint32 *&outConstraints, uint32 *&outConstraintsEnd, TempAllocator *inTempAllocator) const;
72
74 void SortIslands(TempAllocator *inTempAllocator);
75
77 struct BodyLink
78 {
80
81 atomic<uint32> mLinkedTo;
82 uint32 mIslandIndex;
83 };
84
85 // Intermediate data
86 BodyLink * mBodyLinks = nullptr;
87 uint32 * mConstraintLinks = nullptr;
88 uint32 * mContactLinks = nullptr;
89
90 // Final data
91 BodyID * mBodyIslands = nullptr;
92 uint32 * mBodyIslandEnds = nullptr;
93
94 uint32 * mConstraintIslands = nullptr;
95 uint32 * mConstraintIslandEnds = nullptr;
96
97 uint32 * mContactIslands = nullptr;
98 uint32 * mContactIslandEnds = nullptr;
99
100 uint32 * mIslandsSorted = nullptr;
101
102 uint8 * mNumPositionSteps = nullptr;
103
104 // Counters
105 uint32 mMaxActiveBodies;
106 uint32 mNumActiveBodies = 0;
107 uint32 mNumConstraints = 0;
108 uint32 mMaxContacts = 0;
109 uint32 mNumContacts = 0;
110 uint32 mNumIslands = 0;
111
112#ifdef JPH_VALIDATE_ISLAND_BUILDER
114 struct LinkValidation
115 {
116 uint32 mFirst;
117 uint32 mSecond;
118 };
119
120 LinkValidation * mLinkValidation = nullptr;
121 atomic<uint32> mNumLinkValidation;
122#endif
123};
124
std::uint8_t uint8
Definition Core.h:454
unsigned int uint
Definition Core.h:453
#define JPH_NAMESPACE_END
Definition Core.h:379
std::uint32_t uint32
Definition Core.h:456
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:31
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition BodyID.h:13
Keeps track of connected bodies and builds islands for multithreaded velocity/position update.
Definition IslandBuilder.h:19
void Init(uint32 inMaxActiveBodies)
Initialize the island builder with the maximum amount of bodies that could be active.
Definition IslandBuilder.cpp:32
uint GetNumPositionSteps(uint32 inIslandIndex) const
Definition IslandBuilder.h:55
void LinkConstraint(uint32 inConstraintIndex, uint32 inFirst, uint32 inSecond)
Link a constraint to a body by their index in the BodyManager::mActiveBodies.
Definition IslandBuilder.cpp:159
void LinkContact(uint32 inContactIndex, uint32 inFirst, uint32 inSecond)
Link a contact to a body by their index in the BodyManager::mActiveBodies.
Definition IslandBuilder.cpp:169
bool GetContactsInIsland(uint32 inIslandIndex, uint32 *&outContactsBegin, uint32 *&outContactsEnd) const
Definition IslandBuilder.cpp:415
void ResetIslands(TempAllocator *inTempAllocator)
After you're done calling the three functions above, call this function to free associated data.
Definition IslandBuilder.cpp:433
void SetNumPositionSteps(uint32 inIslandIndex, uint inNumPositionSteps)
The number of position iterations for each island.
Definition IslandBuilder.h:54
uint32 GetNumIslands() const
Get the amount of islands formed.
Definition IslandBuilder.h:46
~IslandBuilder()
Destructor.
Definition IslandBuilder.cpp:17
void PrepareContactConstraints(uint32 inMaxContactConstraints, TempAllocator *inTempAllocator)
Prepare for simulation step by allocating space for the contact constraints.
Definition IslandBuilder.cpp:43
bool GetConstraintsInIsland(uint32 inIslandIndex, uint32 *&outConstraintsBegin, uint32 *&outConstraintsEnd) const
Definition IslandBuilder.cpp:397
void PrepareNonContactConstraints(uint32 inNumConstraints, TempAllocator *inTempAllocator)
Prepare for simulation step by allocating space for the non-contact constraints.
Definition IslandBuilder.cpp:67
void GetBodiesInIsland(uint32 inIslandIndex, BodyID *&outBodiesBegin, BodyID *&outBodiesEnd) const
Get iterator for a particular island, return false if there are no constraints.
Definition IslandBuilder.cpp:389
void LinkBodies(uint32 inFirst, uint32 inSecond)
Link two bodies by their index in the BodyManager::mActiveBodies list to form islands.
Definition IslandBuilder.cpp:98
void Finalize(const BodyID *inActiveBodies, uint32 inNumActiveBodies, uint32 inNumContacts, TempAllocator *inTempAllocator)
Finalize the islands after all bodies have been Link()-ed.
Definition IslandBuilder.cpp:375
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition NonCopyable.h:11
Definition TempAllocator.h:16