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

#include <PhysicsSystem.h>

Inheritance diagram for PhysicsSystem:
NonCopyable

Classes

class  BodiesToSleep
 

Public Types

using BodyStats = BodyManager::BodyStats
 Helper struct that counts the number of bodies of each type. More...
 

Public Member Functions

JPH_OVERRIDE_NEW_DELETE PhysicsSystem ()
 Constructor / Destructor. More...
 
 ~PhysicsSystem ()
 
void Init (uint inMaxBodies, uint inNumBodyMutexes, uint inMaxBodyPairs, uint inMaxContactConstraints, const BroadPhaseLayerInterface &inBroadPhaseLayerInterface, const ObjectVsBroadPhaseLayerFilter &inObjectVsBroadPhaseLayerFilter, const ObjectLayerPairFilter &inObjectLayerPairFilter)
 
void SetBodyActivationListener (BodyActivationListener *inListener)
 Listener that is notified whenever a body is activated/deactivated. More...
 
BodyActivationListenerGetBodyActivationListener () const
 
void SetContactListener (ContactListener *inListener)
 Listener that is notified whenever a contact point between two bodies is added/updated/removed. More...
 
ContactListenerGetContactListener () const
 
void SetSoftBodyContactListener (SoftBodyContactListener *inListener)
 Listener that is notified whenever a contact point between a soft body and another body. More...
 
SoftBodyContactListenerGetSoftBodyContactListener () const
 
void SetCombineFriction (ContactConstraintManager::CombineFunction inCombineFriction)
 
ContactConstraintManager::CombineFunction GetCombineFriction () const
 
void SetCombineRestitution (ContactConstraintManager::CombineFunction inCombineRestition)
 
ContactConstraintManager::CombineFunction GetCombineRestitution () const
 
void SetPhysicsSettings (const PhysicsSettings &inSettings)
 Control the main constants of the physics simulation. More...
 
const PhysicsSettingsGetPhysicsSettings () const
 
const BodyInterfaceGetBodyInterface () const
 Access to the body interface. This interface allows to to create / remove bodies and to change their properties. More...
 
BodyInterfaceGetBodyInterface ()
 
const BodyInterfaceGetBodyInterfaceNoLock () const
 Version that does not lock the bodies, use with great care! More...
 
BodyInterfaceGetBodyInterfaceNoLock ()
 Version that does not lock the bodies, use with great care! More...
 
const BroadPhaseQueryGetBroadPhaseQuery () const
 Access to the broadphase interface that allows coarse collision queries. More...
 
const NarrowPhaseQueryGetNarrowPhaseQuery () const
 Interface that allows fine collision queries against first the broad phase and then the narrow phase. More...
 
const NarrowPhaseQueryGetNarrowPhaseQueryNoLock () const
 Version that does not lock the bodies, use with great care! More...
 
void AddConstraint (Constraint *inConstraint)
 Add constraint to the world. More...
 
void RemoveConstraint (Constraint *inConstraint)
 Remove constraint from the world. More...
 
void AddConstraints (Constraint **inConstraints, int inNumber)
 Batch add constraints. Note that the inConstraints array is allowed to have nullptrs, these will be ignored. More...
 
void RemoveConstraints (Constraint **inConstraints, int inNumber)
 Batch remove constraints. Note that the inConstraints array is allowed to have nullptrs, these will be ignored. More...
 
Constraints GetConstraints () const
 Get a list of all constraints. More...
 
void OptimizeBroadPhase ()
 Optimize the broadphase, needed only if you've added many bodies prior to calling Update() for the first time. More...
 
void AddStepListener (PhysicsStepListener *inListener)
 Adds a new step listener. More...
 
void RemoveStepListener (PhysicsStepListener *inListener)
 Removes a step listener. More...
 
EPhysicsUpdateError Update (float inDeltaTime, int inCollisionSteps, TempAllocator *inTempAllocator, JobSystem *inJobSystem)
 
void SaveState (StateRecorder &inStream, EStateRecorderState inState=EStateRecorderState::All, const StateRecorderFilter *inFilter=nullptr) 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
 Saving state of a single body. More...
 
void RestoreBodyState (Body &ioBody, StateRecorder &inStream)
 Restoring state of a single body. More...
 
void DrawBodies (const BodyManager::DrawSettings &inSettings, DebugRenderer *inRenderer, const BodyDrawFilter *inBodyFilter=nullptr)
 Draw the state of the bodies (debugging purposes) More...
 
void DrawConstraints (DebugRenderer *inRenderer)
 Draw the constraints only (debugging purposes) More...
 
void DrawConstraintLimits (DebugRenderer *inRenderer)
 Draw the constraint limits only (debugging purposes) More...
 
void DrawConstraintReferenceFrame (DebugRenderer *inRenderer)
 Draw the constraint reference frames only (debugging purposes) More...
 
void SetGravity (Vec3Arg inGravity)
 Set gravity value. More...
 
Vec3 GetGravity () const
 
const BodyLockInterfaceNoLockGetBodyLockInterfaceNoLock () const
 Returns a locking interface that won't actually lock the body. Use with great care! More...
 
const BodyLockInterfaceLockingGetBodyLockInterface () const
 Returns a locking interface that locks the body so other threads cannot modify it. More...
 
DefaultBroadPhaseLayerFilter GetDefaultBroadPhaseLayerFilter (ObjectLayer inLayer) const
 Get an broadphase layer filter that uses the default pair filter and a specified object layer to determine if broadphase layers collide. More...
 
DefaultObjectLayerFilter GetDefaultLayerFilter (ObjectLayer inLayer) const
 Get an object layer filter that uses the default pair filter and a specified layer to determine if layers collide. More...
 
uint GetNumBodies () const
 Gets the current amount of bodies that are in the body manager. More...
 
uint32 GetNumActiveBodies (EBodyType inType) const
 Gets the current amount of active bodies that are in the body manager. More...
 
uint GetMaxBodies () const
 Get the maximum amount of bodies that this physics system supports. More...
 
BodyStats GetBodyStats () const
 Get stats about the bodies in the body manager (slow, iterates through all bodies) More...
 
void GetBodies (BodyIDVector &outBodyIDs) const
 
void GetActiveBodies (EBodyType inType, BodyIDVector &outBodyIDs) const
 
const BodyIDGetActiveBodiesUnsafe (EBodyType inType) const
 
bool WereBodiesInContact (const BodyID &inBody1ID, const BodyID &inBody2ID) const
 
AABox GetBounds () const
 Get the bounding box of all bodies in the physics system. More...
 
- Public Member Functions inherited from NonCopyable
 NonCopyable ()=default
 
 NonCopyable (const NonCopyable &)=delete
 
void operator= (const NonCopyable &)=delete
 

Static Public Attributes

static bool sDrawMotionQualityLinearCast = false
 Draw debug info for objects that perform continuous collision detection through the linear cast motion quality. More...
 

Detailed Description

The main class for the physics system. It contains all rigid bodies and simulates them.

The main simulation is performed by the Update() call on multiple threads (if the JobSystem is configured to use them). Please refer to the general architecture overview in the Docs folder for more information.

Member Typedef Documentation

◆ BodyStats

Helper struct that counts the number of bodies of each type.

Constructor & Destructor Documentation

◆ PhysicsSystem()

JPH_OVERRIDE_NEW_DELETE PhysicsSystem::PhysicsSystem ( )
inline

Constructor / Destructor.

◆ ~PhysicsSystem()

PhysicsSystem::~PhysicsSystem ( )

Member Function Documentation

◆ AddConstraint()

void PhysicsSystem::AddConstraint ( Constraint inConstraint)
inline

Add constraint to the world.

◆ AddConstraints()

void PhysicsSystem::AddConstraints ( Constraint **  inConstraints,
int  inNumber 
)
inline

Batch add constraints. Note that the inConstraints array is allowed to have nullptrs, these will be ignored.

◆ AddStepListener()

void PhysicsSystem::AddStepListener ( PhysicsStepListener inListener)

Adds a new step listener.

◆ DrawBodies()

void PhysicsSystem::DrawBodies ( const BodyManager::DrawSettings inSettings,
DebugRenderer inRenderer,
const BodyDrawFilter inBodyFilter = nullptr 
)
inline

Draw the state of the bodies (debugging purposes)

◆ DrawConstraintLimits()

void PhysicsSystem::DrawConstraintLimits ( DebugRenderer inRenderer)
inline

Draw the constraint limits only (debugging purposes)

◆ DrawConstraintReferenceFrame()

void PhysicsSystem::DrawConstraintReferenceFrame ( DebugRenderer inRenderer)
inline

Draw the constraint reference frames only (debugging purposes)

◆ DrawConstraints()

void PhysicsSystem::DrawConstraints ( DebugRenderer inRenderer)
inline

Draw the constraints only (debugging purposes)

◆ GetActiveBodies()

void PhysicsSystem::GetActiveBodies ( EBodyType  inType,
BodyIDVector outBodyIDs 
) const
inline

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

Parameters
inTypeThe type of bodies to get
outBodyIDsOn return, this will contain the list of BodyIDs

◆ GetActiveBodiesUnsafe()

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

Get the list of active bodies, use GetNumActiveBodies() to find out how long the list is. Note: Not thread safe. The active bodies list can change at any moment when other threads are doing work. Use GetActiveBodies() if you need a thread safe version.

◆ GetBodies()

void PhysicsSystem::GetBodies ( BodyIDVector outBodyIDs) const
inline

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

Parameters
outBodyIDsOn return, this will contain the list of BodyIDs

◆ GetBodyActivationListener()

BodyActivationListener * PhysicsSystem::GetBodyActivationListener ( ) const
inline

◆ GetBodyInterface() [1/2]

BodyInterface & PhysicsSystem::GetBodyInterface ( )
inline

◆ GetBodyInterface() [2/2]

const BodyInterface & PhysicsSystem::GetBodyInterface ( ) const
inline

Access to the body interface. This interface allows to to create / remove bodies and to change their properties.

◆ GetBodyInterfaceNoLock() [1/2]

BodyInterface & PhysicsSystem::GetBodyInterfaceNoLock ( )
inline

Version that does not lock the bodies, use with great care!

◆ GetBodyInterfaceNoLock() [2/2]

const BodyInterface & PhysicsSystem::GetBodyInterfaceNoLock ( ) const
inline

Version that does not lock the bodies, use with great care!

◆ GetBodyLockInterface()

const BodyLockInterfaceLocking & PhysicsSystem::GetBodyLockInterface ( ) const
inline

Returns a locking interface that locks the body so other threads cannot modify it.

◆ GetBodyLockInterfaceNoLock()

const BodyLockInterfaceNoLock & PhysicsSystem::GetBodyLockInterfaceNoLock ( ) const
inline

Returns a locking interface that won't actually lock the body. Use with great care!

◆ GetBodyStats()

BodyStats PhysicsSystem::GetBodyStats ( ) const
inline

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

◆ GetBounds()

AABox PhysicsSystem::GetBounds ( ) const
inline

Get the bounding box of all bodies in the physics system.

◆ GetBroadPhaseQuery()

const BroadPhaseQuery & PhysicsSystem::GetBroadPhaseQuery ( ) const
inline

Access to the broadphase interface that allows coarse collision queries.

◆ GetCombineFriction()

ContactConstraintManager::CombineFunction PhysicsSystem::GetCombineFriction ( ) const
inline

◆ GetCombineRestitution()

ContactConstraintManager::CombineFunction PhysicsSystem::GetCombineRestitution ( ) const
inline

◆ GetConstraints()

Constraints PhysicsSystem::GetConstraints ( ) const
inline

Get a list of all constraints.

◆ GetContactListener()

ContactListener * PhysicsSystem::GetContactListener ( ) const
inline

◆ GetDefaultBroadPhaseLayerFilter()

DefaultBroadPhaseLayerFilter PhysicsSystem::GetDefaultBroadPhaseLayerFilter ( ObjectLayer  inLayer) const
inline

Get an broadphase layer filter that uses the default pair filter and a specified object layer to determine if broadphase layers collide.

◆ GetDefaultLayerFilter()

DefaultObjectLayerFilter PhysicsSystem::GetDefaultLayerFilter ( ObjectLayer  inLayer) const
inline

Get an object layer filter that uses the default pair filter and a specified layer to determine if layers collide.

◆ GetGravity()

Vec3 PhysicsSystem::GetGravity ( ) const
inline

◆ GetMaxBodies()

uint PhysicsSystem::GetMaxBodies ( ) const
inline

Get the maximum amount of bodies that this physics system supports.

◆ GetNarrowPhaseQuery()

const NarrowPhaseQuery & PhysicsSystem::GetNarrowPhaseQuery ( ) const
inline

Interface that allows fine collision queries against first the broad phase and then the narrow phase.

◆ GetNarrowPhaseQueryNoLock()

const NarrowPhaseQuery & PhysicsSystem::GetNarrowPhaseQueryNoLock ( ) const
inline

Version that does not lock the bodies, use with great care!

◆ GetNumActiveBodies()

uint32 PhysicsSystem::GetNumActiveBodies ( EBodyType  inType) const
inline

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

◆ GetNumBodies()

uint PhysicsSystem::GetNumBodies ( ) const
inline

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

◆ GetPhysicsSettings()

const PhysicsSettings & PhysicsSystem::GetPhysicsSettings ( ) const
inline

◆ GetSoftBodyContactListener()

SoftBodyContactListener * PhysicsSystem::GetSoftBodyContactListener ( ) const
inline

◆ Init()

void PhysicsSystem::Init ( uint  inMaxBodies,
uint  inNumBodyMutexes,
uint  inMaxBodyPairs,
uint  inMaxContactConstraints,
const BroadPhaseLayerInterface inBroadPhaseLayerInterface,
const ObjectVsBroadPhaseLayerFilter inObjectVsBroadPhaseLayerFilter,
const ObjectLayerPairFilter inObjectLayerPairFilter 
)

Initialize the system.

Parameters
inMaxBodiesMaximum number of bodies to support.
inNumBodyMutexesNumber of body mutexes to use. Should be a power of 2 in the range [1, 64], use 0 to auto detect.
inMaxBodyPairsMaximum amount of body pairs to process (anything else will fall through the world), this number should generally be much higher than the max amount of contact points as there will be lots of bodies close that are not actually touching.
inMaxContactConstraintsMaximum amount of contact constraints to process (anything else will fall through the world).
inBroadPhaseLayerInterfaceInformation on the mapping of object layers to broad phase layers. Since this is a virtual interface, the instance needs to stay alive during the lifetime of the PhysicsSystem.
inObjectVsBroadPhaseLayerFilterFilter callback function that is used to determine if an object layer collides with a broad phase layer. Since this is a virtual interface, the instance needs to stay alive during the lifetime of the PhysicsSystem.
inObjectLayerPairFilterFilter callback function that is used to determine if two object layers collide. Since this is a virtual interface, the instance needs to stay alive during the lifetime of the PhysicsSystem.

◆ OptimizeBroadPhase()

void PhysicsSystem::OptimizeBroadPhase ( )

Optimize the broadphase, needed only if you've added many bodies prior to calling Update() for the first time.

◆ RemoveConstraint()

void PhysicsSystem::RemoveConstraint ( Constraint inConstraint)
inline

Remove constraint from the world.

◆ RemoveConstraints()

void PhysicsSystem::RemoveConstraints ( Constraint **  inConstraints,
int  inNumber 
)
inline

Batch remove constraints. Note that the inConstraints array is allowed to have nullptrs, these will be ignored.

◆ RemoveStepListener()

void PhysicsSystem::RemoveStepListener ( PhysicsStepListener inListener)

Removes a step listener.

◆ RestoreBodyState()

void PhysicsSystem::RestoreBodyState ( Body ioBody,
StateRecorder inStream 
)

Restoring state of a single body.

◆ RestoreState()

bool PhysicsSystem::RestoreState ( StateRecorder inStream)

Restoring state for replay. Returns false if failed.

◆ SaveBodyState()

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

Saving state of a single body.

◆ SaveState()

void PhysicsSystem::SaveState ( StateRecorder inStream,
EStateRecorderState  inState = EStateRecorderState::All,
const StateRecorderFilter inFilter = nullptr 
) const

Saving state for replay.

◆ SetBodyActivationListener()

void PhysicsSystem::SetBodyActivationListener ( BodyActivationListener inListener)
inline

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

◆ SetCombineFriction()

void PhysicsSystem::SetCombineFriction ( ContactConstraintManager::CombineFunction  inCombineFriction)
inline

Set the function that combines the friction of two bodies and returns it Default method is the geometric mean: sqrt(friction1 * friction2).

◆ SetCombineRestitution()

void PhysicsSystem::SetCombineRestitution ( ContactConstraintManager::CombineFunction  inCombineRestition)
inline

Set the function that combines the restitution of two bodies and returns it Default method is max(restitution1, restitution1)

◆ SetContactListener()

void PhysicsSystem::SetContactListener ( ContactListener inListener)
inline

Listener that is notified whenever a contact point between two bodies is added/updated/removed.

◆ SetGravity()

void PhysicsSystem::SetGravity ( Vec3Arg  inGravity)
inline

Set gravity value.

◆ SetPhysicsSettings()

void PhysicsSystem::SetPhysicsSettings ( const PhysicsSettings inSettings)
inline

Control the main constants of the physics simulation.

◆ SetSoftBodyContactListener()

void PhysicsSystem::SetSoftBodyContactListener ( SoftBodyContactListener inListener)
inline

Listener that is notified whenever a contact point between a soft body and another body.

◆ Update()

EPhysicsUpdateError PhysicsSystem::Update ( float  inDeltaTime,
int  inCollisionSteps,
TempAllocator inTempAllocator,
JobSystem inJobSystem 
)

Simulate the system. The world steps for a total of inDeltaTime seconds. This is divided in inCollisionSteps iterations. Each iteration consists of collision detection followed by an integration step. This function internally spawns jobs using inJobSystem and waits for them to complete, so no jobs will be running when this function returns.

◆ WereBodiesInContact()

bool PhysicsSystem::WereBodiesInContact ( const BodyID inBody1ID,
const BodyID inBody2ID 
) const
inline

Check if 2 bodies were in contact during the last simulation step. Since contacts are only detected between active bodies, so at least one of the bodies must be active in order for this function to work. It queries the state at the time of the last PhysicsSystem::Update and will return true if the bodies were in contact, even if one of the bodies was moved / removed afterwards. This function can be called from any thread when the PhysicsSystem::Update is not running. During PhysicsSystem::Update this function is only valid during contact callbacks:

  • During the ContactListener::OnContactAdded callback this function can be used to determine if a different contact pair between the bodies was active in the previous simulation step (function returns true) or if this is the first step that the bodies are touching (function returns false).
  • During the ContactListener::OnContactRemoved callback this function can be used to determine if this is the last contact pair between the bodies (function returns false) or if there are other contacts still present (function returns true).

Member Data Documentation

◆ sDrawMotionQualityLinearCast

JPH_NAMESPACE_BEGIN bool PhysicsSystem::sDrawMotionQualityLinearCast = false
static

Draw debug info for objects that perform continuous collision detection through the linear cast motion quality.


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