Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
BodyManager Class Reference

Class that contains all bodies. More...

#include <BodyManager.h>

Inheritance diagram for BodyManager:
NonCopyable

Classes

struct  BodyStats
 Helper struct that counts the number of bodies of each type. More...
 
struct  DrawSettings
 Draw settings. More...
 

Public Types

enum class  EShapeColor {
  InstanceColor , ShapeTypeColor , MotionTypeColor , SleepColor ,
  IslandColor , MaterialColor
}
 
using MutexMask = uint64
 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.
 

Public Member Functions

JPH_OVERRIDE_NEW_DELETE ~BodyManager ()
 Destructor.
 
void Init (uint inMaxBodies, uint inNumBodyMutexes, const BroadPhaseLayerInterface &inLayerInterface)
 Initialize the manager.
 
uint GetNumBodies () const
 Gets the current amount of bodies that are in the body manager.
 
uint GetMaxBodies () const
 Gets the max bodies that we can support.
 
BodyStats GetBodyStats () const
 Get stats about the bodies in the body manager (slow, iterates through all bodies)
 
BodyAllocateBody (const BodyCreationSettings &inBodyCreationSettings) const
 Create a body using creation settings. The returned body will not be part of the body manager yet.
 
BodyAllocateSoftBody (const SoftBodyCreationSettings &inSoftBodyCreationSettings) const
 Create a soft body using creation settings. The returned body will not be part of the body manager yet.
 
void FreeBody (Body *inBody) const
 Free a body that has not been added to the body manager yet (if it has, use DestroyBodies).
 
bool AddBody (Body *ioBody)
 Add a body to the body manager, assigning it the next available ID. Returns false if no more IDs are available.
 
bool AddBodyWithCustomID (Body *ioBody, const BodyID &inBodyID)
 Add a body to the body manager, assigning it a custom ID. Returns false if the ID is not valid.
 
void RemoveBodies (const BodyID *inBodyIDs, int inNumber, Body **outBodies)
 Remove a list of bodies from the body manager.
 
void DestroyBodies (const BodyID *inBodyIDs, int inNumber)
 Remove a set of bodies from the body manager and frees them.
 
void ActivateBodies (const BodyID *inBodyIDs, int inNumber)
 
void DeactivateBodies (const BodyID *inBodyIDs, int inNumber)
 
void SetMotionQuality (Body &ioBody, EMotionQuality inMotionQuality)
 Update the motion quality for a body.
 
void GetActiveBodies (EBodyType inType, BodyIDVector &outBodyIDs) const
 Get copy of the list of active bodies under protection of a lock.
 
const BodyIDGetActiveBodiesUnsafe (EBodyType inType) const
 Get the list of active bodies. Note: Not thread safe. The active bodies list can change at any moment.
 
uint32 GetNumActiveBodies (EBodyType inType) const
 Get the number of active bodies.
 
uint32 GetNumActiveCCDBodies () const
 Get the number of active bodies that are using continuous collision detection.
 
void SetBodyActivationListener (BodyActivationListener *inListener)
 Listener that is notified whenever a body is activated/deactivated.
 
BodyActivationListenerGetBodyActivationListener () const
 
const BodyVectorGetBodies () const
 Get all bodies. Note that this can contain invalid body pointers, call sIsValidBodyPointer to check.
 
BodyVectorGetBodies ()
 Get all bodies. Note that this can contain invalid body pointers, call sIsValidBodyPointer to check.
 
void GetBodyIDs (BodyIDVector &outBodies) const
 Get all body IDs under the protection of a lock.
 
const BodyGetBody (const BodyID &inID) const
 Access a body (not protected by lock)
 
BodyGetBody (const BodyID &inID)
 Access a body (not protected by lock)
 
const BodyTryGetBody (const BodyID &inID) const
 Access a body, will return a nullptr if the body ID is no longer valid (not protected by lock)
 
BodyTryGetBody (const BodyID &inID)
 Access a body, will return a nullptr if the body ID is no longer valid (not protected by lock)
 
SharedMutexGetMutexForBody (const BodyID &inID) const
 Access the mutex for a single body.
 
void LockAllBodies () const
 Lock all bodies. This should only be done during PhysicsSystem::Update().
 
void UnlockAllBodies () const
 Unlock all bodies. This should only be done during PhysicsSystem::Update().
 
void SetBodyObjectLayerInternal (Body &ioBody, ObjectLayer inLayer) const
 Function to update body's layer (should only be called by the BodyInterface since it also requires updating the broadphase)
 
void InvalidateContactCacheForBody (Body &ioBody)
 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.
 
void ValidateContactCacheForAllBodies ()
 Reset the Body::EFlags::InvalidateContactCache flag for all bodies. All contact pairs in the contact cache will now by valid again.
 
void SaveState (StateRecorder &inStream, const StateRecorderFilter *inFilter) const
 Saving state for replay.
 
bool RestoreState (StateRecorder &inStream)
 Restoring state for replay. Returns false if failed.
 
void SaveBodyState (const Body &inBody, StateRecorder &inStream) const
 Save the state of a single body for replay.
 
void RestoreBodyState (Body &inBody, StateRecorder &inStream)
 Save the state of a single body for replay.
 
void Draw (const DrawSettings &inSettings, const PhysicsSettings &inPhysicsSettings, DebugRenderer *inRenderer, const BodyDrawFilter *inBodyFilter=nullptr)
 Draw the state of the bodies (debugging purposes)
 
Batch body mutex access (do not use directly)
MutexMask GetAllBodiesMutexMask () const
 
MutexMask GetMutexMask (const BodyID *inBodies, int inNumber) const
 
void LockRead (MutexMask inMutexMask) const
 
void UnlockRead (MutexMask inMutexMask) const
 
void LockWrite (MutexMask inMutexMask) const
 
void UnlockWrite (MutexMask inMutexMask) const
 
- Public Member Functions inherited from NonCopyable
 NonCopyable ()=default
 
 NonCopyable (const NonCopyable &)=delete
 
void operator= (const NonCopyable &)=delete
 

Static Public Member Functions

static bool sIsValidBodyPointer (const Body *inBody)
 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.
 

Detailed Description

Class that contains all bodies.

Member Typedef Documentation

◆ MutexMask

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.

Member Enumeration Documentation

◆ EShapeColor

enum class BodyManager::EShapeColor
strong
Enumerator
InstanceColor 

Random color per instance.

ShapeTypeColor 

Convex = green, scaled = yellow, compound = orange, mesh = red.

MotionTypeColor 

Static = grey, keyframed = green, dynamic = random color per instance.

SleepColor 

Static = grey, keyframed = green, dynamic = yellow, sleeping = red.

IslandColor 

Static = grey, active = random color per island, sleeping = light grey.

MaterialColor 

Color as defined by the PhysicsMaterial of the shape.

Constructor & Destructor Documentation

◆ ~BodyManager()

BodyManager::~BodyManager ( )

Destructor.

Member Function Documentation

◆ ActivateBodies()

void BodyManager::ActivateBodies ( const BodyID inBodyIDs,
int  inNumber 
)

Activate a list of bodies. This function should only be called when an exclusive lock for the bodies are held.

◆ AddBody()

bool BodyManager::AddBody ( Body ioBody)

Add a body to the body manager, assigning it the next available ID. Returns false if no more IDs are available.

◆ AddBodyWithCustomID()

bool BodyManager::AddBodyWithCustomID ( Body ioBody,
const BodyID inBodyID 
)

Add a body to the body manager, assigning it a custom ID. Returns false if the ID is not valid.

◆ AllocateBody()

Body * BodyManager::AllocateBody ( const BodyCreationSettings inBodyCreationSettings) const

Create a body using creation settings. The returned body will not be part of the body manager yet.

◆ AllocateSoftBody()

Body * BodyManager::AllocateSoftBody ( const SoftBodyCreationSettings inSoftBodyCreationSettings) const

Create a soft body using creation settings. The returned body will not be part of the body manager yet.

◆ DeactivateBodies()

void BodyManager::DeactivateBodies ( const BodyID inBodyIDs,
int  inNumber 
)

Deactivate a list of bodies. This function should only be called when an exclusive lock for the bodies are held.

◆ DestroyBodies()

void BodyManager::DestroyBodies ( const BodyID inBodyIDs,
int  inNumber 
)

Remove a set of bodies from the body manager and frees them.

◆ Draw()

void BodyManager::Draw ( const DrawSettings inSettings,
const PhysicsSettings inPhysicsSettings,
DebugRenderer inRenderer,
const BodyDrawFilter inBodyFilter = nullptr 
)

Draw the state of the bodies (debugging purposes)

◆ FreeBody()

void BodyManager::FreeBody ( Body inBody) const

Free a body that has not been added to the body manager yet (if it has, use DestroyBodies).

◆ GetActiveBodies()

void BodyManager::GetActiveBodies ( EBodyType  inType,
BodyIDVector outBodyIDs 
) const

Get copy of the list of active bodies under protection of a lock.

◆ GetActiveBodiesUnsafe()

const BodyID * BodyManager::GetActiveBodiesUnsafe ( EBodyType  inType) const
inline

Get the list of active bodies. Note: Not thread safe. The active bodies list can change at any moment.

◆ GetAllBodiesMutexMask()

MutexMask BodyManager::GetAllBodiesMutexMask ( ) const
inline

◆ GetBodies() [1/2]

BodyVector & BodyManager::GetBodies ( )
inline

Get all bodies. Note that this can contain invalid body pointers, call sIsValidBodyPointer to check.

◆ GetBodies() [2/2]

const BodyVector & BodyManager::GetBodies ( ) const
inline

Get all bodies. Note that this can contain invalid body pointers, call sIsValidBodyPointer to check.

◆ GetBody() [1/2]

Body & BodyManager::GetBody ( const BodyID inID)
inline

Access a body (not protected by lock)

◆ GetBody() [2/2]

const Body & BodyManager::GetBody ( const BodyID inID) const
inline

Access a body (not protected by lock)

◆ GetBodyActivationListener()

BodyActivationListener * BodyManager::GetBodyActivationListener ( ) const
inline

◆ GetBodyIDs()

void BodyManager::GetBodyIDs ( BodyIDVector outBodies) const

Get all body IDs under the protection of a lock.

◆ GetBodyStats()

BodyManager::BodyStats BodyManager::GetBodyStats ( ) const

Get stats about the bodies in the body manager (slow, iterates through all bodies)

◆ GetMaxBodies()

uint BodyManager::GetMaxBodies ( ) const
inline

Gets the max bodies that we can support.

◆ GetMutexForBody()

SharedMutex & BodyManager::GetMutexForBody ( const BodyID inID) const
inline

Access the mutex for a single body.

◆ GetMutexMask()

BodyManager::MutexMask BodyManager::GetMutexMask ( const BodyID inBodies,
int  inNumber 
) const

◆ GetNumActiveBodies()

uint32 BodyManager::GetNumActiveBodies ( EBodyType  inType) const
inline

Get the number of active bodies.

◆ GetNumActiveCCDBodies()

uint32 BodyManager::GetNumActiveCCDBodies ( ) const
inline

Get the number of active bodies that are using continuous collision detection.

◆ GetNumBodies()

uint BodyManager::GetNumBodies ( ) const

Gets the current amount of bodies that are in the body manager.

◆ Init()

void BodyManager::Init ( uint  inMaxBodies,
uint  inNumBodyMutexes,
const BroadPhaseLayerInterface inLayerInterface 
)

Initialize the manager.

◆ InvalidateContactCacheForBody()

void BodyManager::InvalidateContactCacheForBody ( Body ioBody)

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.

◆ LockAllBodies()

void BodyManager::LockAllBodies ( ) const

Lock all bodies. This should only be done during PhysicsSystem::Update().

◆ LockRead()

void BodyManager::LockRead ( MutexMask  inMutexMask) const

◆ LockWrite()

void BodyManager::LockWrite ( MutexMask  inMutexMask) const

◆ RemoveBodies()

void BodyManager::RemoveBodies ( const BodyID inBodyIDs,
int  inNumber,
Body **  outBodies 
)

Remove a list of bodies from the body manager.

◆ RestoreBodyState()

void BodyManager::RestoreBodyState ( Body inBody,
StateRecorder inStream 
)

Save the state of a single body for replay.

◆ RestoreState()

bool BodyManager::RestoreState ( StateRecorder inStream)

Restoring state for replay. Returns false if failed.

◆ SaveBodyState()

void BodyManager::SaveBodyState ( const Body inBody,
StateRecorder inStream 
) const

Save the state of a single body for replay.

◆ SaveState()

void BodyManager::SaveState ( StateRecorder inStream,
const StateRecorderFilter inFilter 
) const

Saving state for replay.

◆ SetBodyActivationListener()

void BodyManager::SetBodyActivationListener ( BodyActivationListener inListener)

Listener that is notified whenever a body is activated/deactivated.

◆ SetBodyObjectLayerInternal()

void BodyManager::SetBodyObjectLayerInternal ( Body ioBody,
ObjectLayer  inLayer 
) const
inline

Function to update body's layer (should only be called by the BodyInterface since it also requires updating the broadphase)

◆ SetMotionQuality()

void BodyManager::SetMotionQuality ( Body ioBody,
EMotionQuality  inMotionQuality 
)

Update the motion quality for a body.

◆ sIsValidBodyPointer()

static bool BodyManager::sIsValidBodyPointer ( const Body inBody)
inlinestatic

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.

◆ TryGetBody() [1/2]

Body * BodyManager::TryGetBody ( const BodyID inID)
inline

Access a body, will return a nullptr if the body ID is no longer valid (not protected by lock)

◆ TryGetBody() [2/2]

const Body * BodyManager::TryGetBody ( const BodyID inID) const
inline

Access a body, will return a nullptr if the body ID is no longer valid (not protected by lock)

◆ UnlockAllBodies()

void BodyManager::UnlockAllBodies ( ) const

Unlock all bodies. This should only be done during PhysicsSystem::Update().

◆ UnlockRead()

void BodyManager::UnlockRead ( MutexMask  inMutexMask) const

◆ UnlockWrite()

void BodyManager::UnlockWrite ( MutexMask  inMutexMask) const

◆ ValidateContactCacheForAllBodies()

void BodyManager::ValidateContactCacheForAllBodies ( )

Reset the Body::EFlags::InvalidateContactCache flag for all bodies. All contact pairs in the contact cache will now by valid again.


The documentation for this class was generated from the following files: