BodyManager.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Body/Body.h>
  6. #include <Jolt/Core/Mutex.h>
  7. #include <Jolt/Core/MutexArray.h>
  8. JPH_NAMESPACE_BEGIN
  9. // Classes
  10. class BodyCreationSettings;
  11. class SoftBodyCreationSettings;
  12. class BodyActivationListener;
  13. class StateRecorderFilter;
  14. struct PhysicsSettings;
  15. #ifdef JPH_DEBUG_RENDERER
  16. class DebugRenderer;
  17. class BodyDrawFilter;
  18. #endif // JPH_DEBUG_RENDERER
  19. /// Array of bodies
  20. using BodyVector = Array<Body *>;
  21. /// Array of body ID's
  22. using BodyIDVector = Array<BodyID>;
  23. /// Class that contains all bodies
  24. class JPH_EXPORT BodyManager : public NonCopyable
  25. {
  26. public:
  27. JPH_OVERRIDE_NEW_DELETE
  28. /// Destructor
  29. ~BodyManager();
  30. /// Initialize the manager
  31. void Init(uint inMaxBodies, uint inNumBodyMutexes, const BroadPhaseLayerInterface &inLayerInterface);
  32. /// Gets the current amount of bodies that are in the body manager
  33. uint GetNumBodies() const;
  34. /// Gets the max bodies that we can support
  35. uint GetMaxBodies() const { return uint(mBodies.capacity()); }
  36. /// Helper struct that counts the number of bodies of each type
  37. struct BodyStats
  38. {
  39. uint mNumBodies = 0; ///< Total number of bodies in the body manager
  40. uint mMaxBodies = 0; ///< Max allowed number of bodies in the body manager (as configured in Init(...))
  41. uint mNumBodiesStatic = 0; ///< Number of static bodies
  42. uint mNumBodiesDynamic = 0; ///< Number of dynamic bodies
  43. uint mNumActiveBodiesDynamic = 0; ///< Number of dynamic bodies that are currently active
  44. uint mNumBodiesKinematic = 0; ///< Number of kinematic bodies
  45. uint mNumActiveBodiesKinematic = 0; ///< Number of kinematic bodies that are currently active
  46. uint mNumSoftBodies = 0; ///< Number of soft bodies
  47. uint mNumActiveSoftBodies = 0; ///< Number of soft bodies that are currently active
  48. };
  49. /// Get stats about the bodies in the body manager (slow, iterates through all bodies)
  50. BodyStats GetBodyStats() const;
  51. /// Create a body using creation settings. The returned body will not be part of the body manager yet.
  52. Body * AllocateBody(const BodyCreationSettings &inBodyCreationSettings) const;
  53. /// Create a soft body using creation settings. The returned body will not be part of the body manager yet.
  54. Body * AllocateSoftBody(const SoftBodyCreationSettings &inSoftBodyCreationSettings) const;
  55. /// Free a body that has not been added to the body manager yet (if it has, use DestroyBodies).
  56. void FreeBody(Body *inBody) const;
  57. /// Add a body to the body manager, assigning it the next available ID. Returns false if no more IDs are available.
  58. bool AddBody(Body *ioBody);
  59. /// Add a body to the body manager, assigning it a custom ID. Returns false if the ID is not valid.
  60. bool AddBodyWithCustomID(Body *ioBody, const BodyID &inBodyID);
  61. /// Remove a list of bodies from the body manager
  62. void RemoveBodies(const BodyID *inBodyIDs, int inNumber, Body **outBodies);
  63. /// Remove a set of bodies from the body manager and frees them.
  64. void DestroyBodies(const BodyID *inBodyIDs, int inNumber);
  65. /// Activate a list of bodies.
  66. /// This function should only be called when an exclusive lock for the bodies are held.
  67. void ActivateBodies(const BodyID *inBodyIDs, int inNumber);
  68. /// Deactivate a list of bodies.
  69. /// This function should only be called when an exclusive lock for the bodies are held.
  70. void DeactivateBodies(const BodyID *inBodyIDs, int inNumber);
  71. /// Update the motion quality for a body
  72. void SetMotionQuality(Body &ioBody, EMotionQuality inMotionQuality);
  73. /// Get copy of the list of active bodies under protection of a lock.
  74. void GetActiveBodies(EBodyType inType, BodyIDVector &outBodyIDs) const;
  75. /// Get the list of active bodies. Note: Not thread safe. The active bodies list can change at any moment.
  76. const BodyID * GetActiveBodiesUnsafe(EBodyType inType) const { return mActiveBodies[int(inType)]; }
  77. /// Get the number of active bodies.
  78. uint32 GetNumActiveBodies(EBodyType inType) const { return mNumActiveBodies[int(inType)]; }
  79. /// Get the number of active bodies that are using continuous collision detection
  80. uint32 GetNumActiveCCDBodies() const { return mNumActiveCCDBodies; }
  81. /// Listener that is notified whenever a body is activated/deactivated
  82. void SetBodyActivationListener(BodyActivationListener *inListener);
  83. BodyActivationListener * GetBodyActivationListener() const { return mActivationListener; }
  84. /// Check if this is a valid body pointer. When a body is freed the memory that the pointer occupies is reused to store a freelist.
  85. static inline bool sIsValidBodyPointer(const Body *inBody) { return (uintptr_t(inBody) & cIsFreedBody) == 0; }
  86. /// Get all bodies. Note that this can contain invalid body pointers, call sIsValidBodyPointer to check.
  87. const BodyVector & GetBodies() const { return mBodies; }
  88. /// Get all bodies. Note that this can contain invalid body pointers, call sIsValidBodyPointer to check.
  89. BodyVector & GetBodies() { return mBodies; }
  90. /// Get all body IDs under the protection of a lock
  91. void GetBodyIDs(BodyIDVector &outBodies) const;
  92. /// Access a body (not protected by lock)
  93. const Body & GetBody(const BodyID &inID) const { return *mBodies[inID.GetIndex()]; }
  94. /// Access a body (not protected by lock)
  95. Body & GetBody(const BodyID &inID) { return *mBodies[inID.GetIndex()]; }
  96. /// Access a body, will return a nullptr if the body ID is no longer valid (not protected by lock)
  97. const Body * TryGetBody(const BodyID &inID) const
  98. {
  99. uint32 idx = inID.GetIndex();
  100. if (idx >= mBodies.size())
  101. return nullptr;
  102. const Body *body = mBodies[idx];
  103. if (sIsValidBodyPointer(body) && body->GetID() == inID)
  104. return body;
  105. return nullptr;
  106. }
  107. /// Access a body, will return a nullptr if the body ID is no longer valid (not protected by lock)
  108. Body * TryGetBody(const BodyID &inID)
  109. {
  110. uint32 idx = inID.GetIndex();
  111. if (idx >= mBodies.size())
  112. return nullptr;
  113. Body *body = mBodies[idx];
  114. if (sIsValidBodyPointer(body) && body->GetID() == inID)
  115. return body;
  116. return nullptr;
  117. }
  118. /// Access the mutex for a single body
  119. SharedMutex & GetMutexForBody(const BodyID &inID) const { return mBodyMutexes.GetMutexByObjectIndex(inID.GetIndex()); }
  120. /// Bodies are protected using an array of mutexes (so a fixed number, not 1 per body). Each bit in this mask indicates a locked mutex.
  121. using MutexMask = uint64;
  122. ///@name Batch body mutex access (do not use directly)
  123. ///@{
  124. MutexMask GetAllBodiesMutexMask() const { return mBodyMutexes.GetNumMutexes() == sizeof(MutexMask) * 8? ~MutexMask(0) : (MutexMask(1) << mBodyMutexes.GetNumMutexes()) - 1; }
  125. MutexMask GetMutexMask(const BodyID *inBodies, int inNumber) const;
  126. void LockRead(MutexMask inMutexMask) const;
  127. void UnlockRead(MutexMask inMutexMask) const;
  128. void LockWrite(MutexMask inMutexMask) const;
  129. void UnlockWrite(MutexMask inMutexMask) const;
  130. ///@}
  131. /// Lock all bodies. This should only be done during PhysicsSystem::Update().
  132. void LockAllBodies() const;
  133. /// Unlock all bodies. This should only be done during PhysicsSystem::Update().
  134. void UnlockAllBodies() const;
  135. /// Function to update body's layer (should only be called by the BodyInterface since it also requires updating the broadphase)
  136. inline void SetBodyObjectLayerInternal(Body &ioBody, ObjectLayer inLayer) const { ioBody.mObjectLayer = inLayer; ioBody.mBroadPhaseLayer = mBroadPhaseLayerInterface->GetBroadPhaseLayer(inLayer); }
  137. /// Set the Body::EFlags::InvalidateContactCache flag for the specified body. This means that the collision cache is invalid for any body pair involving that body until the next physics step.
  138. void InvalidateContactCacheForBody(Body &ioBody);
  139. /// Reset the Body::EFlags::InvalidateContactCache flag for all bodies. All contact pairs in the contact cache will now by valid again.
  140. void ValidateContactCacheForAllBodies();
  141. /// Saving state for replay
  142. void SaveState(StateRecorder &inStream, const StateRecorderFilter *inFilter) const;
  143. /// Restoring state for replay. Returns false if failed.
  144. bool RestoreState(StateRecorder &inStream);
  145. /// Save the state of a single body for replay
  146. void SaveBodyState(const Body &inBody, StateRecorder &inStream) const;
  147. /// Save the state of a single body for replay
  148. void RestoreBodyState(Body &inBody, StateRecorder &inStream);
  149. enum class EShapeColor
  150. {
  151. InstanceColor, ///< Random color per instance
  152. ShapeTypeColor, ///< Convex = green, scaled = yellow, compound = orange, mesh = red
  153. MotionTypeColor, ///< Static = grey, keyframed = green, dynamic = random color per instance
  154. SleepColor, ///< Static = grey, keyframed = green, dynamic = yellow, sleeping = red
  155. IslandColor, ///< Static = grey, active = random color per island, sleeping = light grey
  156. MaterialColor, ///< Color as defined by the PhysicsMaterial of the shape
  157. };
  158. #ifdef JPH_DEBUG_RENDERER
  159. /// Draw settings
  160. struct DrawSettings
  161. {
  162. bool mDrawGetSupportFunction = false; ///< Draw the GetSupport() function, used for convex collision detection
  163. bool mDrawSupportDirection = false; ///< When drawing the support function, also draw which direction mapped to a specific support point
  164. bool mDrawGetSupportingFace = false; ///< Draw the faces that were found colliding during collision detection
  165. bool mDrawShape = true; ///< Draw the shapes of all bodies
  166. bool mDrawShapeWireframe = false; ///< When mDrawShape is true and this is true, the shapes will be drawn in wireframe instead of solid.
  167. EShapeColor mDrawShapeColor = EShapeColor::MotionTypeColor; ///< Coloring scheme to use for shapes
  168. bool mDrawBoundingBox = false; ///< Draw a bounding box per body
  169. bool mDrawCenterOfMassTransform = false; ///< Draw the center of mass for each body
  170. bool mDrawWorldTransform = false; ///< Draw the world transform (which can be different than the center of mass) for each body
  171. bool mDrawVelocity = false; ///< Draw the velocity vector for each body
  172. bool mDrawMassAndInertia = false; ///< Draw the mass and inertia (as the box equivalent) for each body
  173. bool mDrawSleepStats = false; ///< Draw stats regarding the sleeping algorithm of each body
  174. bool mDrawSoftBodyVertices = false; ///< Draw the vertices of soft bodies
  175. bool mDrawSoftBodyEdgeConstraints = false; ///< Draw the edge constraints of soft bodies
  176. bool mDrawSoftBodyVolumeConstraints = false; ///< Draw the volume constraints of soft bodies
  177. bool mDrawSoftBodyPredictedBounds = false; ///< Draw the predicted bounds of soft bodies
  178. };
  179. /// Draw the state of the bodies (debugging purposes)
  180. void Draw(const DrawSettings &inSettings, const PhysicsSettings &inPhysicsSettings, DebugRenderer *inRenderer, const BodyDrawFilter *inBodyFilter = nullptr);
  181. #endif // JPH_DEBUG_RENDERER
  182. #ifdef JPH_ENABLE_ASSERTS
  183. /// Lock the active body list, asserts when Activate/DeactivateBody is called.
  184. void SetActiveBodiesLocked(bool inLocked) { mActiveBodiesLocked = inLocked; }
  185. /// Per thread override of the locked state, to be used by the PhysicsSystem only!
  186. class GrantActiveBodiesAccess
  187. {
  188. public:
  189. inline GrantActiveBodiesAccess(bool inAllowActivation, bool inAllowDeactivation)
  190. {
  191. JPH_ASSERT(!sGetOverrideAllowActivation());
  192. sSetOverrideAllowActivation(inAllowActivation);
  193. JPH_ASSERT(!sGetOverrideAllowDeactivation());
  194. sSetOverrideAllowDeactivation(inAllowDeactivation);
  195. }
  196. inline ~GrantActiveBodiesAccess()
  197. {
  198. sSetOverrideAllowActivation(false);
  199. sSetOverrideAllowDeactivation(false);
  200. }
  201. };
  202. #endif
  203. #ifdef _DEBUG
  204. /// Validate if the cached bounding boxes are correct for all active bodies
  205. void ValidateActiveBodyBounds();
  206. #endif // _DEBUG
  207. private:
  208. /// Increment and get the sequence number of the body
  209. #ifdef JPH_COMPILER_CLANG
  210. __attribute__((no_sanitize("implicit-conversion"))) // We intentionally overflow the uint8 sequence number
  211. #endif
  212. inline uint8 GetNextSequenceNumber(int inBodyIndex) { return ++mBodySequenceNumbers[inBodyIndex]; }
  213. /// Add a single body to mActiveBodies, note doesn't lock the active body mutex!
  214. inline void AddBodyToActiveBodies(Body &ioBody);
  215. /// Remove a single body from mActiveBodies, note doesn't lock the active body mutex!
  216. inline void RemoveBodyFromActiveBodies(Body &ioBody);
  217. /// Helper function to remove a body from the manager
  218. JPH_INLINE Body * RemoveBodyInternal(const BodyID &inBodyID);
  219. /// Helper function to delete a body (which could actually be a BodyWithMotionProperties)
  220. inline static void sDeleteBody(Body *inBody);
  221. #if defined(_DEBUG) && defined(JPH_ENABLE_ASSERTS)
  222. /// Function to check that the free list is not corrupted
  223. void ValidateFreeList() const;
  224. #endif // defined(_DEBUG) && _defined(JPH_ENABLE_ASSERTS)
  225. /// List of pointers to all bodies. Contains invalid pointers for deleted bodies, check with sIsValidBodyPointer. Note that this array is reserved to the max bodies that is passed in the Init function so that adding bodies will not reallocate the array.
  226. BodyVector mBodies;
  227. /// Current number of allocated bodies
  228. uint mNumBodies = 0;
  229. /// Indicates that there are no more freed body IDs
  230. static constexpr uintptr_t cBodyIDFreeListEnd = ~uintptr_t(0);
  231. /// Bit that indicates a pointer in mBodies is actually the index of the next freed body. We use the lowest bit because we know that Bodies need to be 16 byte aligned so addresses can never end in a 1 bit.
  232. static constexpr uintptr_t cIsFreedBody = uintptr_t(1);
  233. /// Amount of bits to shift to get an index to the next freed body
  234. static constexpr uint cFreedBodyIndexShift = 1;
  235. /// Index of first entry in mBodies that is unused
  236. uintptr_t mBodyIDFreeListStart = cBodyIDFreeListEnd;
  237. /// Protects mBodies array (but not the bodies it points to), mNumBodies and mBodyIDFreeListStart
  238. mutable Mutex mBodiesMutex;
  239. /// An array of mutexes protecting the bodies in the mBodies array
  240. using BodyMutexes = MutexArray<SharedMutex>;
  241. mutable BodyMutexes mBodyMutexes;
  242. /// List of next sequence number for a body ID
  243. Array<uint8> mBodySequenceNumbers;
  244. /// Mutex that protects the mActiveBodies array
  245. mutable Mutex mActiveBodiesMutex;
  246. /// List of all active dynamic bodies (size is equal to max amount of bodies)
  247. BodyID * mActiveBodies[cBodyTypeCount] = { };
  248. /// How many bodies there are in the list of active bodies
  249. atomic<uint32> mNumActiveBodies[cBodyTypeCount] = { };
  250. /// How many of the active bodies have continuous collision detection enabled
  251. uint32 mNumActiveCCDBodies = 0;
  252. /// Mutex that protects the mBodiesCacheInvalid array
  253. mutable Mutex mBodiesCacheInvalidMutex;
  254. /// List of all bodies that should have their cache invalidated
  255. BodyIDVector mBodiesCacheInvalid;
  256. /// Listener that is notified whenever a body is activated/deactivated
  257. BodyActivationListener * mActivationListener = nullptr;
  258. /// Cached broadphase layer interface
  259. const BroadPhaseLayerInterface *mBroadPhaseLayerInterface = nullptr;
  260. #ifdef JPH_ENABLE_ASSERTS
  261. static bool sGetOverrideAllowActivation();
  262. static void sSetOverrideAllowActivation(bool inValue);
  263. static bool sGetOverrideAllowDeactivation();
  264. static void sSetOverrideAllowDeactivation(bool inValue);
  265. /// Debug system that tries to limit changes to active bodies during the PhysicsSystem::Update()
  266. bool mActiveBodiesLocked = false;
  267. #endif
  268. };
  269. JPH_NAMESPACE_END