PhysicsSystem

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.

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

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

Link copied to clipboard

Listener that is notified whenever a body is activated/deactivated

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

Get the bounding box of all bodies in the physics system. Deprecated: Use GetBroadPhaseQuery().GetBounds() instead.

Link copied to clipboard

Return the BroadPhaseLayerInterface that this class has been initialized with

Link copied to clipboard

Access to the broadphase interface that allows coarse collision queries

Link copied to clipboard

Get a list of all constraints

Link copied to clipboard

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.

Link copied to clipboard

Set gravity value

Link copied to clipboard

Get the maximum amount of bodies that this physics system supports

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

Object layer filter that decides if two objects can collide, this was passed to the Init function.

Link copied to clipboard

Broadphase layer filter that decides if two objects can collide, this was passed to the Init function.

Link copied to clipboard

Control the main constants of the physics simulation

Link copied to clipboard

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.

Link copied to clipboard

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

Functions

Link copied to clipboard
fun addConstraint(constraint: Constraint?)

Add constraint to the world

Link copied to clipboard
fun addConstraints(constraints: List<Constraint>, number: Int)

Batch add constraints.

Link copied to clipboard

Adds a new step listener

Link copied to clipboard
open override fun close()
Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard

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

Link copied to clipboard
fun init(maxBodies: Int, numBodyMutexes: Int, maxBodyPairs: Int, maxContactConstraints: Int, broadPhaseLayerInterface: BroadPhaseLayerInterface, objectVsBroadPhaseLayerFilter: ObjectVsBroadPhaseLayerFilter, objectLayerPairFilter: ObjectLayerPairFilter)

Initialize the system.

Link copied to clipboard

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.

Link copied to clipboard
fun removeConstraint(constraint: Constraint?)

Remove constraint from the world

Link copied to clipboard
fun removeConstraints(constraints: List<Constraint>, number: Int)

Batch remove constraints.

Link copied to clipboard

Removes a step listener

Link copied to clipboard
fun restoreBodyState(body: Body, stream: StateRecorder)

Restoring state of a single body.

Link copied to clipboard

Restoring state for replay. Returns false if failed.

Link copied to clipboard
fun saveBodyState(body: Body, stream: StateRecorder)

Saving state of a single body.

Link copied to clipboard

Saving state for replay

Link copied to clipboard

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.

Link copied to clipboard
fun update(deltaTime: Float, collisionSteps: Int, tempAllocator: TempAllocator?, jobSystem: JobSystem?): EPhysicsUpdateError

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.

Link copied to clipboard
fun wereBodiesInContact(body1ID: BodyID, body2ID: BodyID): Boolean

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: