Class PhysicsSystem

All Implemented Interfaces:
ConstJoltPhysicsObject, AutoCloseable, Comparable<JoltPhysicsObject>

public class PhysicsSystem extends NonCopyable
Perform simulation on a collection of physics objects. Bodies are added by way of a separate interface.
  • Constructor Details

    • PhysicsSystem

      public PhysicsSystem()
      Instantiate an uninitialized physics system.
  • Method Details

    • addConstraint

      public void addConstraint(Constraint constraint)
      Add the specified constraint to the system.
      Parameters:
      constraint - the constraint to add (not null)
    • addConstraint

      public void addConstraint(TwoBodyConstraintRef constraintRef)
      Add the specified constraint to the system.
      Parameters:
      constraintRef - a counted reference to the constraint to add (not null)
    • addStepListener

      public void addStepListener(PhysicsStepListener listener)
      Add the specified step listener to the system.

      Step listeners are limited in what actions they can perform.

      Parameters:
      listener - the listener to add (not null, alias created)
    • destroyAllBodies

      public int destroyAllBodies()
      Remove and destroy all bodies in the system.
      Returns:
      the number of bodies destroyed (≥0)
    • drawBodies

      public void drawBodies(BodyManagerDrawSettings settings, DebugRenderer renderer)
      Render the bodies in the system, for debugging purposes.
      Parameters:
      settings - the settings to use (not null)
      renderer - the renderer to use (not null)
    • drawConstraints

      public void drawConstraints(DebugRenderer renderer)
      Render all constraints in the system, for debugging purposes.
      Parameters:
      renderer - the renderer to use (not null)
    • drawConstraintLimits

      public void drawConstraintLimits(DebugRenderer renderer)
      Render the limits of all constraints in the system, for debugging purposes.
      Parameters:
      renderer - the renderer to use (not null)
    • drawConstraintReferenceFrame

      public void drawConstraintReferenceFrame(DebugRenderer renderer)
      Render the reference frames of all constraints in the system, for debugging purposes.
      Parameters:
      renderer - the renderer to use (not null)
    • containsConstraint

      public boolean containsConstraint(ConstConstraint constraint)
      Test whether the system contains the specified constraint. The system is unaffected.
      Parameters:
      constraint - the constraint to search for (not null, unaffected)
      Returns:
      true if found, otherwise false
    • find

      public static PhysicsSystem find(long systemVa)
      Find a pre-existing system given its virtual address.
      Parameters:
      systemVa - the address to search for
      Returns:
      the pre-existing object, or null if not found
    • getActiveBodies

      public void getActiveBodies(EBodyType bodyType, BodyIdVector storeResult)
      Enumerate all bodies of the specified type to the specified vector.
      Parameters:
      bodyType - (not null)
      storeResult - storage for the result (not null)
    • getBodies

      public void getBodies(BodyIdVector storeResult)
      Enumerate all bodies to the specified variable-length vector. The system is unaffected.
      Parameters:
      storeResult - storage for the result (not null)
    • getBodyActivationListener

      public BodyActivationListener getBodyActivationListener()
      Access the system's BodyActivationListener.
      Returns:
      a new JVM object with the pre-existing native object assigned, or null if none
    • getBodyInterface

      public BodyInterface getBodyInterface()
      Access the system's BodyInterface.
      Returns:
      the pre-existing JVM object (not null)
    • getBodyInterfaceNoLock

      public BodyInterface getBodyInterfaceNoLock()
      Access a version of the system's BodyInterface that does not use locks.
      Returns:
      a new JVM object with the pre-existing native object assigned
    • getBodyLockInterface

      public BodyLockInterfaceLocking getBodyLockInterface()
      Access the system's BodyLockInterfaceLocking.
      Returns:
      a new JVM object with the pre-existing native object assigned
    • getBodyLockInterfaceNoLock

      public BodyLockInterfaceNoLock getBodyLockInterfaceNoLock()
      Access the system's BodyLockInterfaceNoLock.
      Returns:
      a new JVM object with the pre-existing native object assigned
    • getBounds

      public AaBox getBounds()
      Return a bounding box that contains all the bodies in the system. The system is unaffected.
      Returns:
      a new box
    • getBroadPhaseLayerInterface

      public ConstBroadPhaseLayerInterface getBroadPhaseLayerInterface()
      Access the (application-provided) interface for mapping object layers to broadphase layers.
      Returns:
      the pre-existing instance, or null if none
    • getBroadPhaseQuery

      public BroadPhaseQuery getBroadPhaseQuery()
      Access the system's interface for coarse collision queries.
      Returns:
      the pre-existing JVM object, or null if the system hasn't been initialized yet
    • getCombineFriction

      public CombineFunction getCombineFriction()
      Access the combining function for friction.
      Returns:
      a new JVM object with the pre-existing native object assigned
    • getCombineRestitution

      public CombineFunction getCombineRestitution()
      Access the combining function for restitutions.
      Returns:
      a new JVM object with the pre-existing native object assigned
    • getConstraints

      public Constraints getConstraints()
      Enumerate all constraints in the system. The system is unaffected.
      Returns:
      a new object
    • getContactListener

      public ContactListener getContactListener()
      Access the (application-provided) contact listener.
      Returns:
      the pre-existing instance, or null if none
    • getDefaultBroadPhaseLayerFilter

      public DefaultBroadPhaseLayerFilter getDefaultBroadPhaseLayerFilter(int objectLayer)
      Generate a broad-phase layer filter using the current pair filter and the specified layer index.
      Parameters:
      objectLayer - the index of the object layer to use
      Returns:
      a new filter
    • getDefaultLayerFilter

      public DefaultObjectLayerFilter getDefaultLayerFilter(int objectLayer)
      Generate an object layer filter using the current pair filter and the specified layer index.
      Parameters:
      objectLayer - the index of the object layer to use
      Returns:
      a new filter
    • getGravity

      public Vec3 getGravity()
      Copy the gravity vector. The system is unaffected.
      Returns:
      a new acceleration vector (meters per second squared in system coordinates)
    • getMaxBodies

      public int getMaxBodies()
      Return the maximum number of bodies the system supports. The system is unaffected.
      Returns:
      the count (≥0)
    • getNarrowPhaseQuery

      public NarrowPhaseQuery getNarrowPhaseQuery()
      Access the system's interface for fine collision queries.
      Returns:
      the pre-existing JVM object (not null)
    • getNarrowPhaseQueryNoLock

      public NarrowPhaseQuery getNarrowPhaseQueryNoLock()
      Access a version of the system's NarrowPhaseQuery that does not lock the bodies.
      Returns:
      the pre-existing JVM object (not null)
    • getNumActiveBodies

      public int getNumActiveBodies(EBodyType bodyType)
      Count how many active bodies of the specified type there are in the body manager. The system is unaffected.
      Parameters:
      bodyType - which type of body to count (not null)
      Returns:
      the count (≥0, ≤maxBodies)
    • getNumBodies

      public int getNumBodies()
      Count how many bodies there are in the body manager. The system is unaffected.
      Returns:
      the count (≥0, ≤maxBodies)
    • getOvbFilter

      public ConstObjectVsBroadPhaseLayerFilter getOvbFilter()
      Access the (application-provided) interface for testing whether an object can collide with a broad-phase layer.
      Returns:
      the pre-existing instance, or null if none
    • getOvoFilter

      public ConstObjectLayerPairFilter getOvoFilter()
      Access the (application-provided) interface for testing whether 2 objects can collide, based on their object layers.
      Returns:
      the pre-existing instance, or null if none
    • getPhysicsSettings

      public PhysicsSettings getPhysicsSettings()
      Copy the system's settings. The system is unaffected.
      Returns:
      a new JVM object with a new native object assigned
      See Also:
    • getSoftBodyContactListener

      public SoftBodyContactListener getSoftBodyContactListener()
      Access the (application-provided) soft-body contact listener.
      Returns:
      the pre-existing instance, or null if none
    • init

      public void init(int maxBodies, int numBodyMutexes, int maxBodyPairs, int maxContactConstraints, ConstBroadPhaseLayerInterface map, ConstObjectVsBroadPhaseLayerFilter ovbFilter, ConstObjectLayerPairFilter ovoFilter)
      Initialize the system with the specified limits.
      Parameters:
      maxBodies - the desired maximum number of rigid bodies that can be added
      numBodyMutexes - the desired number of mutexes to allocate, or 0 for the default number
      maxBodyPairs - the desired maximum number of body pairs that can be queued at a time
      maxContactConstraints - the desired capacity of the contact-constraint buffer
      map - (not null, alias created)
      ovbFilter - (not null, alias created)
      ovoFilter - (not null, alias created)
    • optimizeBroadPhase

      public void optimizeBroadPhase()
      Improve the performance of future collision detections.
    • removeAllBodies

      public int removeAllBodies()
      Remove all bodies from the system, but don't destroy them.
      Returns:
      the number of bodies removed (≥0)
      See Also:
    • removeAllConstraints

      public int removeAllConstraints()
      Remove all constraints from the system.
      Returns:
      the number of constraints removed (≥0)
    • removeConstraint

      public void removeConstraint(Constraint constraint)
      Remove the specified constraint from the system.
      Parameters:
      constraint - the constraint to remove (not null)
    • removeStepListener

      public void removeStepListener(PhysicsStepListener listener)
      Remove the specified step listener from the system.
      Parameters:
      listener - the listener to remove (not null)
    • restoreState

      public boolean restoreState(StateRecorder recorder)
      Restore the system's state from the specified recorder, for replay.
      Parameters:
      recorder - where to read the state from (not null)
      Returns:
      true if successful, otherwise false
    • saveState

      public void saveState(StateRecorder recorder)
      Save the system's state to be replayed later.
      Parameters:
      recorder - where to save the state (not null)
    • saveState

      public void saveState(StateRecorder recorder, int bitmask)
      Save the aspects of the system's state to be replayed later.
      Parameters:
      recorder - where to save the state (not null)
      bitmask - which aspects of the simulation to save
    • saveState

      public void saveState(StateRecorder recorder, int bitmask, StateRecorderFilter filter)
      Save aspects of the system's state to be replayed later.
      Parameters:
      recorder - where to save the state (not null)
      bitmask - which aspects of the simulation to save
      filter - select which parts to save (unaffected) or null
    • setBodyActivationListener

      public void setBodyActivationListener(BodyActivationListener listener)
      Replace the system's BodyActivationListener.
      Parameters:
      listener - the desired listener
    • setCombineFriction

      public void setCombineFriction(CombineFunction function)
      Replace the combining function for friction.
      Parameters:
      function - the desired function (not null, default=geometricMean)
    • setCombineRestitution

      public void setCombineRestitution(CombineFunction function)
      Replace the combining function for restitutions.
      Parameters:
      function - the desired function (not null, default=max)
    • setContactListener

      public void setContactListener(ContactListener listener)
      Replace the system's contact listener.
      Parameters:
      listener - the desired listener
    • setGravity

      public void setGravity(float x, float y, float z)
      Alter the system's gravity vector.
      Parameters:
      x - the X component of the desired acceleration vector (in system coordinates)
      y - the Y component of the desired acceleration vector (in system coordinates)
      z - the Z component of the desired acceleration vector (in system coordinates)
    • setGravity

      public void setGravity(Vec3Arg gravity)
      Alter the system's gravity vector.
      Parameters:
      gravity - the desired acceleration vector (in system coordinates, not null, unaffected, default=(0,-9.81,0))
    • setPhysicsSettings

      public void setPhysicsSettings(PhysicsSettings settings)
      Copy the specified settings to the system.
      Parameters:
      settings - the desired settings (not null, unaffected)
      See Also:
    • setSoftBodyContactListener

      public void setSoftBodyContactListener(SoftBodyContactListener listener)
      Replace the system's soft-body contact listener.
      Parameters:
      listener - the desired listener
    • update

      public int update(float deltaTime, int collisionSteps, TempAllocator tempAllocator, JobSystem jobSystem)
      Advance the simulation by the specified amount.
      Parameters:
      deltaTime - the total time to advance (in seconds)
      collisionSteps - the number of simulation steps to perform
      tempAllocator - the allocator to use (not null)
      jobSystem - the job system to use (not null)
      Returns:
      a bitmask of error conditions, or-ed together
      See Also: