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