Jolt Physics
A multi core friendly Game Physics Engine
|
#include <PhysicsSystem.h>
Classes | |
class | BodiesToSleep |
Public Types | |
using | BodyStats = BodyManager::BodyStats |
Helper struct that counts the number of bodies of each type. | |
Public Member Functions | |
JPH_OVERRIDE_NEW_DELETE | PhysicsSystem () |
Constructor / Destructor. | |
~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. | |
BodyActivationListener * | GetBodyActivationListener () const |
void | SetContactListener (ContactListener *inListener) |
ContactListener * | GetContactListener () const |
void | SetSoftBodyContactListener (SoftBodyContactListener *inListener) |
Listener that is notified whenever a contact point between a soft body and another body. | |
SoftBodyContactListener * | GetSoftBodyContactListener () const |
void | SetCombineFriction (ContactConstraintManager::CombineFunction inCombineFriction) |
ContactConstraintManager::CombineFunction | GetCombineFriction () const |
void | SetCombineRestitution (ContactConstraintManager::CombineFunction inCombineRestitution) |
ContactConstraintManager::CombineFunction | GetCombineRestitution () const |
void | SetSimShapeFilter (const SimShapeFilter *inShapeFilter) |
const SimShapeFilter * | GetSimShapeFilter () const |
void | SetPhysicsSettings (const PhysicsSettings &inSettings) |
Control the main constants of the physics simulation. | |
const PhysicsSettings & | GetPhysicsSettings () const |
const BodyInterface & | GetBodyInterface () const |
Access to the body interface. This interface allows to to create / remove bodies and to change their properties. | |
BodyInterface & | GetBodyInterface () |
const BodyInterface & | GetBodyInterfaceNoLock () const |
Version that does not lock the bodies, use with great care! | |
BodyInterface & | GetBodyInterfaceNoLock () |
Version that does not lock the bodies, use with great care! | |
const BroadPhaseQuery & | GetBroadPhaseQuery () const |
Access to the broadphase interface that allows coarse collision queries. | |
const NarrowPhaseQuery & | GetNarrowPhaseQuery () const |
Interface that allows fine collision queries against first the broad phase and then the narrow phase. | |
const NarrowPhaseQuery & | GetNarrowPhaseQueryNoLock () const |
Version that does not lock the bodies, use with great care! | |
void | AddConstraint (Constraint *inConstraint) |
Add constraint to the world. | |
void | RemoveConstraint (Constraint *inConstraint) |
Remove constraint from the world. | |
void | AddConstraints (Constraint **inConstraints, int inNumber) |
Batch add constraints. | |
void | RemoveConstraints (Constraint **inConstraints, int inNumber) |
Batch remove constraints. | |
Constraints | GetConstraints () const |
Get a list of all constraints. | |
void | OptimizeBroadPhase () |
void | AddStepListener (PhysicsStepListener *inListener) |
Adds a new step listener. | |
void | RemoveStepListener (PhysicsStepListener *inListener) |
Removes a step listener. | |
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. | |
bool | RestoreState (StateRecorder &inStream, const StateRecorderFilter *inFilter=nullptr) |
Restoring state for replay. Returns false if failed. | |
void | SaveBodyState (const Body &inBody, StateRecorder &inStream) const |
Saving state of a single body. | |
void | RestoreBodyState (Body &ioBody, StateRecorder &inStream) |
Restoring state of a single body. | |
void | DrawBodies (const BodyManager::DrawSettings &inSettings, DebugRenderer *inRenderer, const BodyDrawFilter *inBodyFilter=nullptr) |
Draw the state of the bodies (debugging purposes) | |
void | DrawConstraints (DebugRenderer *inRenderer) |
Draw the constraints only (debugging purposes) | |
void | DrawConstraintLimits (DebugRenderer *inRenderer) |
Draw the constraint limits only (debugging purposes) | |
void | DrawConstraintReferenceFrame (DebugRenderer *inRenderer) |
Draw the constraint reference frames only (debugging purposes) | |
void | SetGravity (Vec3Arg inGravity) |
Set gravity value. | |
Vec3 | GetGravity () const |
const BodyLockInterfaceNoLock & | GetBodyLockInterfaceNoLock () const |
Returns a locking interface that won't actually lock the body. Use with great care! | |
const BodyLockInterfaceLocking & | GetBodyLockInterface () const |
Returns a locking interface that locks the body so other threads cannot modify it. | |
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. | |
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. | |
uint | GetNumBodies () const |
Gets the current amount of bodies that are in the body manager. | |
uint32 | GetNumActiveBodies (EBodyType inType) const |
Gets the current amount of active bodies that are in the body manager. | |
uint | GetMaxBodies () const |
Get the maximum amount of bodies that this physics system supports. | |
BodyStats | GetBodyStats () const |
Get stats about the bodies in the body manager (slow, iterates through all bodies) | |
void | GetBodies (BodyIDVector &outBodyIDs) const |
void | GetActiveBodies (EBodyType inType, BodyIDVector &outBodyIDs) const |
const BodyID * | GetActiveBodiesUnsafe (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. | |
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. | |
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.
Helper struct that counts the number of bodies of each type.
|
inline |
Constructor / Destructor.
PhysicsSystem::~PhysicsSystem | ( | ) |
|
inline |
Add constraint to the world.
|
inline |
Batch add constraints.
void PhysicsSystem::AddStepListener | ( | PhysicsStepListener * | inListener | ) |
Adds a new step listener.
|
inline |
Draw the state of the bodies (debugging purposes)
|
inline |
Draw the constraint limits only (debugging purposes)
|
inline |
Draw the constraint reference frames only (debugging purposes)
|
inline |
Draw the constraints only (debugging purposes)
|
inline |
Get copy of the list of active bodies under protection of a lock.
inType | The type of bodies to get |
outBodyIDs | On return, this will contain the list of BodyIDs |
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.
|
inline |
Get copy of the list of all bodies under protection of a lock.
outBodyIDs | On return, this will contain the list of BodyIDs |
|
inline |
|
inline |
|
inline |
Access to the body interface. This interface allows to to create / remove bodies and to change their properties.
|
inline |
Version that does not lock the bodies, use with great care!
|
inline |
Version that does not lock the bodies, use with great care!
|
inline |
Returns a locking interface that locks the body so other threads cannot modify it.
|
inline |
Returns a locking interface that won't actually lock the body. Use with great care!
|
inline |
Get stats about the bodies in the body manager (slow, iterates through all bodies)
|
inline |
Get the bounding box of all bodies in the physics system.
|
inline |
Access to the broadphase interface that allows coarse collision queries.
|
inline |
|
inline |
|
inline |
Get a list of all constraints.
|
inline |
|
inline |
Get an broadphase layer filter that uses the default pair filter and a specified object layer to determine if broadphase layers collide.
|
inline |
Get an object layer filter that uses the default pair filter and a specified layer to determine if layers collide.
|
inline |
|
inline |
Get the maximum amount of bodies that this physics system supports.
|
inline |
Interface that allows fine collision queries against first the broad phase and then the narrow phase.
|
inline |
Version that does not lock the bodies, use with great care!
Gets the current amount of active bodies that are in the body manager.
|
inline |
Gets the current amount of bodies that are in the body manager.
|
inline |
|
inline |
|
inline |
void PhysicsSystem::Init | ( | uint | inMaxBodies, |
uint | inNumBodyMutexes, | ||
uint | inMaxBodyPairs, | ||
uint | inMaxContactConstraints, | ||
const BroadPhaseLayerInterface & | inBroadPhaseLayerInterface, | ||
const ObjectVsBroadPhaseLayerFilter & | inObjectVsBroadPhaseLayerFilter, | ||
const ObjectLayerPairFilter & | inObjectLayerPairFilter | ||
) |
Initialize the system.
inMaxBodies | Maximum number of bodies to support. |
inNumBodyMutexes | Number of body mutexes to use. Should be a power of 2 in the range [1, 64], use 0 to auto detect. |
inMaxBodyPairs | Maximum 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. |
inMaxContactConstraints | Maximum amount of contact constraints to process (anything else will fall through the world). |
inBroadPhaseLayerInterface | Information 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. |
inObjectVsBroadPhaseLayerFilter | Filter 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. |
inObjectLayerPairFilter | Filter 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. |
void PhysicsSystem::OptimizeBroadPhase | ( | ) |
Optimize the broadphase, needed only if you've added many bodies prior to calling Update() for the first time. Don't call this every frame as PhysicsSystem::Update spreads out the same work over multiple frames. If you add many bodies through BodyInterface::AddBodiesPrepare/AddBodiesFinalize and if the bodies in a batch are in a roughly unoccupied space (e.g. a new level section) then a call to OptimizeBroadPhase is also not needed as batch adding creates an efficient bounding volume hierarchy. Don't call this function while bodies are being modified from another thread or use the locking BodyInterface to modify bodies.
|
inline |
Remove constraint from the world.
|
inline |
Batch remove constraints.
void PhysicsSystem::RemoveStepListener | ( | PhysicsStepListener * | inListener | ) |
Removes a step listener.
void PhysicsSystem::RestoreBodyState | ( | Body & | ioBody, |
StateRecorder & | inStream | ||
) |
Restoring state of a single body.
bool PhysicsSystem::RestoreState | ( | StateRecorder & | inStream, |
const StateRecorderFilter * | inFilter = nullptr |
||
) |
Restoring state for replay. Returns false if failed.
void PhysicsSystem::SaveBodyState | ( | const Body & | inBody, |
StateRecorder & | inStream | ||
) | const |
Saving state of a single body.
void PhysicsSystem::SaveState | ( | StateRecorder & | inStream, |
EStateRecorderState | inState = EStateRecorderState::All , |
||
const StateRecorderFilter * | inFilter = nullptr |
||
) | const |
Saving state for replay.
|
inline |
Listener that is notified whenever a body is activated/deactivated.
|
inline |
Set the function that combines the friction of two bodies and returns it Default method is the geometric mean: sqrt(friction1 * friction2).
|
inline |
Set the function that combines the restitution of two bodies and returns it Default method is max(restitution1, restitution1)
|
inline |
Listener that is notified whenever a contact point between two bodies is added/updated/removed. You can't change contact listener during PhysicsSystem::Update but it can be changed at any other time.
|
inline |
Set gravity value.
|
inline |
Control the main constants of the physics simulation.
|
inline |
Set/get the shape filter that will be used during simulation. This can be used to exclude shapes within a body from colliding with each other. E.g. if you have a high detail and a low detail collision model, you can attach them to the same body in a StaticCompoundShape and use the ShapeFilter to exclude the high detail collision model when simulating and exclude the low detail collision model when casting rays. Note that in this case you would need to pass the inverse of inShapeFilter to the CastRay function. Pass a nullptr to disable the shape filter. The PhysicsSystem does not own the ShapeFilter, make sure it stays alive during the lifetime of the PhysicsSystem.
|
inline |
Listener that is notified whenever a contact point between a soft body and another body.
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. The temp allocator is used, for example, to store the list of bodies that are in contact, how they form islands together and data to solve the contacts between bodies. At the end of the Update call, all allocated memory will have been freed.
|
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:
|
static |
Draw debug info for objects that perform continuous collision detection through the linear cast motion quality.