Physics System
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.
C++ JPH::PhysicsSystem.
Properties
Listener that is notified whenever a body is activated/deactivated
Returns a locking interface that locks the body so other threads cannot modify it.
Returns a locking interface that won't actually lock the body. Use with great care!
Return the BroadPhaseLayerInterface that this class has been initialized with
Access to the broadphase interface that allows coarse collision queries
Get a list of all constraints
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.
Interface that allows fine collision queries against first the broad phase and then the narrow phase.
Object layer filter that decides if two objects can collide, this was passed to the Init function.
Broadphase layer filter that decides if two objects can collide, this was passed to the Init function.
Control the main constants of the physics simulation
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.
Listener that is notified whenever a contact point between a soft body and another body
Functions
Add constraint to the world
Batch add constraints.
Adds a new step listener
Get copy of the list of active bodies under protection of a lock.
Get an broadphase layer filter that uses the default pair filter and a specified object layer to determine if broadphase layers collide
Get an object layer filter that uses the default pair filter and a specified layer to determine if layers collide
Gets the current amount of active bodies that are in the body manager
Initialize the system.
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.
Remove constraint from the world
Batch remove constraints.
Removes a step listener
Restoring state of a single body.
Restoring state for replay. Returns false if failed.
Saving state of a single body.
Saving state for replay
Advanced use only: Set the function that will be used to collide two bodies during simulation. This function is expected to eventually call CollideShapeCollector::AddHit all contact points between the shapes of body 1 and 2 in their given transforms.
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.
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: