Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Body/BodyManager.h
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Physics/Body/Body.h>7#include <Jolt/Core/Mutex.h>8#include <Jolt/Core/MutexArray.h>910JPH_NAMESPACE_BEGIN1112// Classes13class BodyCreationSettings;14class SoftBodyCreationSettings;15class BodyActivationListener;16class StateRecorderFilter;17struct PhysicsSettings;18#ifdef JPH_DEBUG_RENDERER19class DebugRenderer;20class BodyDrawFilter;21#endif // JPH_DEBUG_RENDERER2223#ifdef JPH_DEBUG_RENDERER2425/// Defines how to color soft body constraints26enum class ESoftBodyConstraintColor27{28ConstraintType, /// Draw different types of constraints in different colors29ConstraintGroup, /// Draw constraints in the same group in the same color, non-parallel group will be red30ConstraintOrder, /// Draw constraints in the same group in the same color, non-parallel group will be red, and order within each group will be indicated with gradient31};3233#endif // JPH_DEBUG_RENDERER3435/// Array of bodies36using BodyVector = Array<Body *>;3738/// Array of body ID's39using BodyIDVector = Array<BodyID>;4041/// Class that contains all bodies42class JPH_EXPORT BodyManager : public NonCopyable43{44public:45JPH_OVERRIDE_NEW_DELETE4647/// Destructor48~BodyManager();4950/// Initialize the manager51void Init(uint inMaxBodies, uint inNumBodyMutexes, const BroadPhaseLayerInterface &inLayerInterface);5253/// Gets the current amount of bodies that are in the body manager54uint GetNumBodies() const;5556/// Gets the max bodies that we can support57uint GetMaxBodies() const { return uint(mBodies.capacity()); }5859/// Helper struct that counts the number of bodies of each type60struct BodyStats61{62uint mNumBodies = 0; ///< Total number of bodies in the body manager63uint mMaxBodies = 0; ///< Max allowed number of bodies in the body manager (as configured in Init(...))6465uint mNumBodiesStatic = 0; ///< Number of static bodies6667uint mNumBodiesDynamic = 0; ///< Number of dynamic bodies68uint mNumActiveBodiesDynamic = 0; ///< Number of dynamic bodies that are currently active6970uint mNumBodiesKinematic = 0; ///< Number of kinematic bodies71uint mNumActiveBodiesKinematic = 0; ///< Number of kinematic bodies that are currently active7273uint mNumSoftBodies = 0; ///< Number of soft bodies74uint mNumActiveSoftBodies = 0; ///< Number of soft bodies that are currently active75};7677/// Get stats about the bodies in the body manager (slow, iterates through all bodies)78BodyStats GetBodyStats() const;7980/// Create a body using creation settings. The returned body will not be part of the body manager yet.81Body * AllocateBody(const BodyCreationSettings &inBodyCreationSettings) const;8283/// Create a soft body using creation settings. The returned body will not be part of the body manager yet.84Body * AllocateSoftBody(const SoftBodyCreationSettings &inSoftBodyCreationSettings) const;8586/// Free a body that has not been added to the body manager yet (if it has, use DestroyBodies).87void FreeBody(Body *inBody) const;8889/// Add a body to the body manager, assigning it the next available ID. Returns false if no more IDs are available.90bool AddBody(Body *ioBody);9192/// Add a body to the body manager, assigning it a custom ID. Returns false if the ID is not valid.93bool AddBodyWithCustomID(Body *ioBody, const BodyID &inBodyID);9495/// Remove a list of bodies from the body manager96void RemoveBodies(const BodyID *inBodyIDs, int inNumber, Body **outBodies);9798/// Remove a set of bodies from the body manager and frees them.99void DestroyBodies(const BodyID *inBodyIDs, int inNumber);100101/// Activate a list of bodies.102/// This function should only be called when an exclusive lock for the bodies are held.103void ActivateBodies(const BodyID *inBodyIDs, int inNumber);104105/// Deactivate a list of bodies.106/// This function should only be called when an exclusive lock for the bodies are held.107void DeactivateBodies(const BodyID *inBodyIDs, int inNumber);108109/// Update the motion quality for a body110void SetMotionQuality(Body &ioBody, EMotionQuality inMotionQuality);111112/// Get copy of the list of active bodies under protection of a lock.113void GetActiveBodies(EBodyType inType, BodyIDVector &outBodyIDs) const;114115/// Get the list of active bodies. Note: Not thread safe. The active bodies list can change at any moment.116const BodyID * GetActiveBodiesUnsafe(EBodyType inType) const { return mActiveBodies[int(inType)]; }117118/// Get the number of active bodies.119uint32 GetNumActiveBodies(EBodyType inType) const { return mNumActiveBodies[int(inType)].load(memory_order_acquire); }120121/// Get the number of active bodies that are using continuous collision detection122uint32 GetNumActiveCCDBodies() const { return mNumActiveCCDBodies; }123124/// Listener that is notified whenever a body is activated/deactivated125void SetBodyActivationListener(BodyActivationListener *inListener);126BodyActivationListener * GetBodyActivationListener() const { return mActivationListener; }127128/// 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.129static inline bool sIsValidBodyPointer(const Body *inBody) { return (uintptr_t(inBody) & cIsFreedBody) == 0; }130131/// Get all bodies. Note that this can contain invalid body pointers, call sIsValidBodyPointer to check.132const BodyVector & GetBodies() const { return mBodies; }133134/// Get all bodies. Note that this can contain invalid body pointers, call sIsValidBodyPointer to check.135BodyVector & GetBodies() { return mBodies; }136137/// Get all body IDs under the protection of a lock138void GetBodyIDs(BodyIDVector &outBodies) const;139140/// Access a body (not protected by lock)141const Body & GetBody(const BodyID &inID) const { return *mBodies[inID.GetIndex()]; }142143/// Access a body (not protected by lock)144Body & GetBody(const BodyID &inID) { return *mBodies[inID.GetIndex()]; }145146/// Access a body, will return a nullptr if the body ID is no longer valid (not protected by lock)147const Body * TryGetBody(const BodyID &inID) const148{149uint32 idx = inID.GetIndex();150if (idx >= mBodies.size())151return nullptr;152153const Body *body = mBodies[idx];154if (sIsValidBodyPointer(body) && body->GetID() == inID)155return body;156157return nullptr;158}159160/// Access a body, will return a nullptr if the body ID is no longer valid (not protected by lock)161Body * TryGetBody(const BodyID &inID)162{163uint32 idx = inID.GetIndex();164if (idx >= mBodies.size())165return nullptr;166167Body *body = mBodies[idx];168if (sIsValidBodyPointer(body) && body->GetID() == inID)169return body;170171return nullptr;172}173174/// Access the mutex for a single body175SharedMutex & GetMutexForBody(const BodyID &inID) const { return mBodyMutexes.GetMutexByObjectIndex(inID.GetIndex()); }176177/// 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.178using MutexMask = uint64;179180///@name Batch body mutex access (do not use directly)181///@{182MutexMask GetAllBodiesMutexMask() const { return mBodyMutexes.GetNumMutexes() == sizeof(MutexMask) * 8? ~MutexMask(0) : (MutexMask(1) << mBodyMutexes.GetNumMutexes()) - 1; }183MutexMask GetMutexMask(const BodyID *inBodies, int inNumber) const;184void LockRead(MutexMask inMutexMask) const;185void UnlockRead(MutexMask inMutexMask) const;186void LockWrite(MutexMask inMutexMask) const;187void UnlockWrite(MutexMask inMutexMask) const;188///@}189190/// Lock all bodies. This should only be done during PhysicsSystem::Update().191void LockAllBodies() const;192193/// Unlock all bodies. This should only be done during PhysicsSystem::Update().194void UnlockAllBodies() const;195196/// Function to update body's layer (should only be called by the BodyInterface since it also requires updating the broadphase)197inline void SetBodyObjectLayerInternal(Body &ioBody, ObjectLayer inLayer) const { ioBody.mObjectLayer = inLayer; ioBody.mBroadPhaseLayer = mBroadPhaseLayerInterface->GetBroadPhaseLayer(inLayer); }198199/// 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.200void InvalidateContactCacheForBody(Body &ioBody);201202/// Reset the Body::EFlags::InvalidateContactCache flag for all bodies. All contact pairs in the contact cache will now by valid again.203void ValidateContactCacheForAllBodies();204205/// Saving state for replay206void SaveState(StateRecorder &inStream, const StateRecorderFilter *inFilter) const;207208/// Restoring state for replay. Returns false if failed.209bool RestoreState(StateRecorder &inStream);210211/// Save the state of a single body for replay212void SaveBodyState(const Body &inBody, StateRecorder &inStream) const;213214/// Save the state of a single body for replay215void RestoreBodyState(Body &inBody, StateRecorder &inStream);216217#ifdef JPH_DEBUG_RENDERER218enum class EShapeColor219{220InstanceColor, ///< Random color per instance221ShapeTypeColor, ///< Convex = green, scaled = yellow, compound = orange, mesh = red222MotionTypeColor, ///< Static = grey, keyframed = green, dynamic = random color per instance223SleepColor, ///< Static = grey, keyframed = green, dynamic = yellow, sleeping = red224IslandColor, ///< Static = grey, active = random color per island, sleeping = light grey225MaterialColor, ///< Color as defined by the PhysicsMaterial of the shape226};227228/// Draw settings229struct DrawSettings230{231bool mDrawGetSupportFunction = false; ///< Draw the GetSupport() function, used for convex collision detection232bool mDrawSupportDirection = false; ///< When drawing the support function, also draw which direction mapped to a specific support point233bool mDrawGetSupportingFace = false; ///< Draw the faces that were found colliding during collision detection234bool mDrawShape = true; ///< Draw the shapes of all bodies235bool mDrawShapeWireframe = false; ///< When mDrawShape is true and this is true, the shapes will be drawn in wireframe instead of solid.236EShapeColor mDrawShapeColor = EShapeColor::MotionTypeColor; ///< Coloring scheme to use for shapes237bool mDrawBoundingBox = false; ///< Draw a bounding box per body238bool mDrawCenterOfMassTransform = false; ///< Draw the center of mass for each body239bool mDrawWorldTransform = false; ///< Draw the world transform (which can be different than the center of mass) for each body240bool mDrawVelocity = false; ///< Draw the velocity vector for each body241bool mDrawMassAndInertia = false; ///< Draw the mass and inertia (as the box equivalent) for each body242bool mDrawSleepStats = false; ///< Draw stats regarding the sleeping algorithm of each body243bool mDrawSoftBodyVertices = false; ///< Draw the vertices of soft bodies244bool mDrawSoftBodyVertexVelocities = false; ///< Draw the velocities of the vertices of soft bodies245bool mDrawSoftBodyEdgeConstraints = false; ///< Draw the edge constraints of soft bodies246bool mDrawSoftBodyBendConstraints = false; ///< Draw the bend constraints of soft bodies247bool mDrawSoftBodyVolumeConstraints = false; ///< Draw the volume constraints of soft bodies248bool mDrawSoftBodySkinConstraints = false; ///< Draw the skin constraints of soft bodies249bool mDrawSoftBodyLRAConstraints = false; ///< Draw the LRA constraints of soft bodies250bool mDrawSoftBodyPredictedBounds = false; ///< Draw the predicted bounds of soft bodies251ESoftBodyConstraintColor mDrawSoftBodyConstraintColor = ESoftBodyConstraintColor::ConstraintType; ///< Coloring scheme to use for soft body constraints252};253254/// Draw the state of the bodies (debugging purposes)255void Draw(const DrawSettings &inSettings, const PhysicsSettings &inPhysicsSettings, DebugRenderer *inRenderer, const BodyDrawFilter *inBodyFilter = nullptr);256#endif // JPH_DEBUG_RENDERER257258#ifdef JPH_ENABLE_ASSERTS259/// Lock the active body list, asserts when Activate/DeactivateBody is called.260void SetActiveBodiesLocked(bool inLocked) { mActiveBodiesLocked = inLocked; }261262/// Per thread override of the locked state, to be used by the PhysicsSystem only!263class GrantActiveBodiesAccess264{265public:266inline GrantActiveBodiesAccess(bool inAllowActivation, bool inAllowDeactivation)267{268JPH_ASSERT(!sGetOverrideAllowActivation());269sSetOverrideAllowActivation(inAllowActivation);270271JPH_ASSERT(!sGetOverrideAllowDeactivation());272sSetOverrideAllowDeactivation(inAllowDeactivation);273}274275inline ~GrantActiveBodiesAccess()276{277sSetOverrideAllowActivation(false);278sSetOverrideAllowDeactivation(false);279}280};281#endif282283#ifdef JPH_DEBUG284/// Validate if the cached bounding boxes are correct for all active bodies285void ValidateActiveBodyBounds();286#endif // JPH_DEBUG287288private:289/// Increment and get the sequence number of the body290#ifdef JPH_COMPILER_CLANG291__attribute__((no_sanitize("implicit-conversion"))) // We intentionally overflow the uint8 sequence number292#endif293inline uint8 GetNextSequenceNumber(int inBodyIndex) { return ++mBodySequenceNumbers[inBodyIndex]; }294295/// Add a single body to mActiveBodies, note doesn't lock the active body mutex!296inline void AddBodyToActiveBodies(Body &ioBody);297298/// Remove a single body from mActiveBodies, note doesn't lock the active body mutex!299inline void RemoveBodyFromActiveBodies(Body &ioBody);300301/// Helper function to remove a body from the manager302JPH_INLINE Body * RemoveBodyInternal(const BodyID &inBodyID);303304/// Helper function to delete a body (which could actually be a BodyWithMotionProperties)305inline static void sDeleteBody(Body *inBody);306307#if defined(JPH_DEBUG) && defined(JPH_ENABLE_ASSERTS)308/// Function to check that the free list is not corrupted309void ValidateFreeList() const;310#endif // defined(JPH_DEBUG) && _defined(JPH_ENABLE_ASSERTS)311312/// 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.313BodyVector mBodies;314315/// Current number of allocated bodies316uint mNumBodies = 0;317318/// Indicates that there are no more freed body IDs319static constexpr uintptr_t cBodyIDFreeListEnd = ~uintptr_t(0);320321/// 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.322static constexpr uintptr_t cIsFreedBody = uintptr_t(1);323324/// Amount of bits to shift to get an index to the next freed body325static constexpr uint cFreedBodyIndexShift = 1;326327/// Index of first entry in mBodies that is unused328uintptr_t mBodyIDFreeListStart = cBodyIDFreeListEnd;329330/// Protects mBodies array (but not the bodies it points to), mNumBodies and mBodyIDFreeListStart331mutable Mutex mBodiesMutex;332333/// An array of mutexes protecting the bodies in the mBodies array334using BodyMutexes = MutexArray<SharedMutex>;335mutable BodyMutexes mBodyMutexes;336337/// List of next sequence number for a body ID338Array<uint8> mBodySequenceNumbers;339340/// Mutex that protects the mActiveBodies array341mutable Mutex mActiveBodiesMutex;342343/// List of all active dynamic bodies (size is equal to max amount of bodies)344BodyID * mActiveBodies[cBodyTypeCount] = { };345346/// How many bodies there are in the list of active bodies347atomic<uint32> mNumActiveBodies[cBodyTypeCount] = { };348349/// How many of the active bodies have continuous collision detection enabled350uint32 mNumActiveCCDBodies = 0;351352/// Mutex that protects the mBodiesCacheInvalid array353mutable Mutex mBodiesCacheInvalidMutex;354355/// List of all bodies that should have their cache invalidated356BodyIDVector mBodiesCacheInvalid;357358/// Listener that is notified whenever a body is activated/deactivated359BodyActivationListener * mActivationListener = nullptr;360361/// Cached broadphase layer interface362const BroadPhaseLayerInterface *mBroadPhaseLayerInterface = nullptr;363364#ifdef JPH_ENABLE_ASSERTS365static bool sGetOverrideAllowActivation();366static void sSetOverrideAllowActivation(bool inValue);367368static bool sGetOverrideAllowDeactivation();369static void sSetOverrideAllowDeactivation(bool inValue);370371/// Debug system that tries to limit changes to active bodies during the PhysicsSystem::Update()372bool mActiveBodiesLocked = false;373#endif374};375376JPH_NAMESPACE_END377378379