Class PhysicsRigidBody

All Implemented Interfaces:
Comparable<NativePhysicsObject>
Direct Known Subclasses:
PhysicsVehicle

public class PhysicsRigidBody extends PhysicsBody
A collision object to simulate a rigid body, based on Bullet's btRigidBody.
  • Field Details

    • logger2

      public static final Logger logger2
      message logger for this class
    • mass

      protected float mass
      JVM copy of the mass (>0) of a dynamic body, or 0 for a static body
  • Constructor Details

    • PhysicsRigidBody

      public PhysicsRigidBody(CollisionShape shape)
      Instantiate a responsive, dynamic body with mass=1 and the specified CollisionShape. The new body is not added to any PhysicsSpace.
      Parameters:
      shape - the desired shape (not null, alias created)
    • PhysicsRigidBody

      public PhysicsRigidBody(CollisionShape shape, float mass)
      Instantiate a responsive dynamic or static body with the specified CollisionShape and mass. The new body is not added to any PhysicsSpace.
      Parameters:
      shape - the desired shape (not null, alias created)
      mass - if 0, a static body is created; otherwise a dynamic body is created (≥0, default=1)
  • Method Details

    • activate

      public void activate()
      Reactivate the body if it has been deactivated due to lack of motion.
    • applyCentralForce

      public void applyCentralForce(Vector3f force)
      Apply a force to the body's center of mass. Effective on the next simulation step, then cleared.

      To apply an impulse, use applyCentralImpulse(com.jme3.math.Vector3f).

      Parameters:
      force - the force vector (mass times distance per second squared in physics-space coordinates, not null, finite, unaffected)
    • applyCentralImpulse

      public void applyCentralImpulse(Vector3f impulse)
      Apply an impulse to the body's center of mass.
      Parameters:
      impulse - the impulse vector (mass times distance per second in physics-space coordinates, not null, unaffected)
    • applyForce

      public void applyForce(Vector3f force, Vector3f offset)
      Apply a force to the body. Effective on the next simulation step, then cleared.

      To apply an impulse, use applyImpulse(com.jme3.math.Vector3f, com.jme3.math.Vector3f).

      Parameters:
      force - the force vector (mass times distance per second squared in physics-space coordinates, not null, finite, unaffected)
      offset - the location to apply the force (relative to the body's center in physics-space coordinates, not null, finite, unaffected)
    • applyImpulse

      public void applyImpulse(Vector3f impulse, Vector3f offset)
      Apply an off-center impulse to the body.
      Parameters:
      impulse - the impulse vector (mass times distance per second in physics-space coordinates, not null, unaffected)
      offset - where to apply the impulse (relative to the body's center of mass in physics-space coordinates, not null, unaffected)
    • applyTorque

      public void applyTorque(Vector3f torque)
      Apply a torque to the body. Effective on the next simulation step, then cleared.

      To apply a torque impulse, use applyTorqueImpulse(com.jme3.math.Vector3f).

      Parameters:
      torque - the torque vector (mass times distance squared per second squared in physics-space coordinates, not null, finite, unaffected)
    • applyTorqueImpulse

      public void applyTorqueImpulse(Vector3f torqueImpulse)
      Apply a torque impulse to the body.
      Parameters:
      torqueImpulse - the torque impulse vector (mass times physics-space units squared per second in physics-space coordinates, not null, unaffected)
    • clearForces

      public void clearForces()
      Clear all forces and torques acting on the body, including gravity.
    • getAngularDamping

      public float getAngularDamping()
      Return the body's angular damping.
      Returns:
      the damping fraction (≥0, ≤1)
    • getAngularFactor

      public Vector3f getAngularFactor(Vector3f storeResult)
      Copy the body's angular factors.
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      the angular factor for each axis (either storeResult or a new vector, not null)
    • getAngularSleepingThreshold

      public float getAngularSleepingThreshold()
      Return the body's angular-motion sleeping threshold. Note that "sleeping" is synonym for "deactivation".
      Returns:
      the angular-motion threshold (in radians per second, ≥0)
    • getAngularVelocity

      public Vector3f getAngularVelocity(Vector3f storeResult)
      Copy the body's angular velocity. The body must be in dynamic mode.
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      an angular-velocity vector (radians per second in physics-space coordinates, either storeResult or a new vector, not null)
    • getAngularVelocityDp

      public Vec3d getAngularVelocityDp(Vec3d storeResult)
      Copy the body's angular velocity. The body must be in dynamic mode.
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      an angular-velocity vector (radians per second in physics-space coordinates, either storeResult or a new vector, not null)
    • getAngularVelocityLocal

      public Vector3f getAngularVelocityLocal(Vector3f storeResult)
      Calculate the body's angular velocity in its local coordinates. The body must be in dynamic mode.
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      a velocity vector (radians per second in local coordinates, either storeResult or a new vector, not null)
    • getGravityDp

      public Vec3d getGravityDp(Vec3d storeResult)
      Copy the body's gravitational acceleration.
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      an acceleration vector in physics-space coordinates (either storeResult or a new vector, not null)
    • getInverseInertiaLocal

      public Vector3f getInverseInertiaLocal(Vector3f storeResult)
      Copy the principal (diagonal) elements of the inverse inertia tensor in the body's local coordinates.
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      a vector (either storeResult or a new vector, not null)
    • getInverseInertiaWorld

      public Matrix3f getInverseInertiaWorld(Matrix3f storeResult)
      Compute the inverse inertia tensor in physics-space coordinates.
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      a matrix (either storeResult or a new matrix, not null)
    • getLinearDamping

      public float getLinearDamping()
      Return the body's linear damping.
      Returns:
      the damping fraction (≥0, ≤1)
    • getLinearFactor

      public Vector3f getLinearFactor(Vector3f storeResult)
      Copy the body's linear factors.
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      the linear factor for each axis (either storeResult or a new vector, not null)
    • getLinearSleepingThreshold

      public float getLinearSleepingThreshold()
      Return the body's linear-motion sleeping threshold. Note that "sleeping" is synonym for "deactivation".
      Returns:
      the linear-motion threshold (in physics-space units per second, ≥0)
    • getLinearVelocity

      public Vector3f getLinearVelocity(Vector3f storeResult)
      Copy the linear velocity of the body's center of mass. The body must be in dynamic mode.
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      a velocity vector (physics-space units per second in physics-space coordinates, either storeResult or a new vector, not null)
    • getLinearVelocityDp

      public Vec3d getLinearVelocityDp(Vec3d storeResult)
      Copy the linear velocity of the body's center of mass. The body must be in dynamic mode.
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      a velocity vector (physics-space units per second in physics-space coordinates, either storeResult or a new vector, not null)
    • getMotionState

      public RigidBodyMotionState getMotionState()
      Access the body's motion state.
      Returns:
      the pre-existing instance (not null)
    • getSquaredSpeed

      public float getSquaredSpeed()
      Calculate the squared speed of the body. The body must be in dynamic mode.
      Returns:
      the squared speed (in physics-space units squared per second squared, ≥0)
    • isDynamic

      public boolean isDynamic()
      Test whether the body is in dynamic mode.
      Returns:
      true if in dynamic mode, otherwise false (static/kinematic mode)
    • isGravityProtected

      public boolean isGravityProtected()
      Test whether the body's gravity can be overwritten by PhysicsSpace.
      Returns:
      false if the body's gravity can be overwritten, otherwise true
    • isKinematic

      public final boolean isKinematic()
      Test whether the body is in kinematic mode.

      In kinematic mode, the body is not influenced by physics but can affect other physics objects. Its kinetic force is calculated based on its movement and weight.

      Returns:
      true if in kinematic mode, otherwise false (dynamic/static mode)
    • kineticEnergy

      public double kineticEnergy()
      Calculate the body's kinetic energy (linear + angular). The body must be in dynamic mode.
      Returns:
      the total kinetic energy (≥0)
    • mechanicalEnergy

      public double mechanicalEnergy()
      Calculate the mechanical energy of the body (kinetic + potential) assuming a uniform gravitational field. The body must be in dynamic mode.
      Returns:
      the total mechanical energy
    • rebuildRigidBody

      public void rebuildRigidBody()
      Rebuild the rigid body with a new native object.
    • setAngularDamping

      public void setAngularDamping(float angularDamping)
      Alter the body's angular damping.
      Parameters:
      angularDamping - the desired angular damping fraction (≥0, ≤1, default=0)
    • setAngularFactor

      public void setAngularFactor(float factor)
      Alter the body's angular factor.
      Parameters:
      factor - the desired angular factor for all axes (not null, unaffected, default=1)
    • setAngularFactor

      public void setAngularFactor(Vector3f factor)
      Alter the body's angular factors, used to scale applied torques.
      Parameters:
      factor - the desired angular factor for each axis (not null, unaffected, default=(1,1,1))
    • setAngularSleepingThreshold

      public void setAngularSleepingThreshold(float threshold)
      Alter the body's angular-motion sleeping threshold. Note that "sleeping" is synonym for "deactivation".
      Parameters:
      threshold - the desired threshold (in radians per second, ≥0, default=1)
    • setAngularVelocity

      public void setAngularVelocity(Vector3f omega)
      Alter the body's angular velocity.
      Parameters:
      omega - the desired angular velocity (radians per second in physics-space coordinates, not null, finite, unaffected)
    • setAngularVelocityDp

      public void setAngularVelocityDp(Vec3d omega)
      Alter the body's angular velocity.
      Parameters:
      omega - the desired angular velocity (radians per second in physics-space coordinates, not null, unaffected)
    • setCollisionShape

      public void setCollisionShape(CollisionShape desiredShape)
      Apply the specified CollisionShape to the body. The body gets rebuilt on the native side.
      Overrides:
      setCollisionShape in class PhysicsCollisionObject
      Parameters:
      desiredShape - the shape to apply (not null, alias created)
    • setContactResponse

      public void setContactResponse(boolean newState)
      Enable/disable the body's contact response.
      Parameters:
      newState - true to respond to contacts, false to ignore them (default=true)
    • setDamping

      public void setDamping(float linearDamping, float angularDamping)
      Alter the body's damping.
      Parameters:
      linearDamping - the desired linear damping fraction (≥0, ≤1, default=0)
      angularDamping - the desired angular damping fraction (≥0, ≤1, default=0)
    • setEnableSleep

      public void setEnableSleep(boolean setting)
      Alter the body's activation state to allow/disallow sleeping. Note that "sleeping" is synonym for "deactivation".
      Parameters:
      setting - true→enable sleeping, false→disable sleeping (default=true)
    • setGravityDp

      public void setGravityDp(Vec3d acceleration)
      Alter the body's gravitational acceleration.

      Invoke this method after adding the body to a PhysicsSpace. Unless protection is set, adding a body to a PhysicsSpace overrides its gravity.

      Parameters:
      acceleration - the desired acceleration vector (in physics-space coordinates, not null, unaffected, default=(0,0,0))
      See Also:
    • setInverseInertiaLocal

      public void setInverseInertiaLocal(Vector3f inverseInertia)
      Alter the principal (diagonal) components of the local inertia tensor in the body's local coordinates.
      Parameters:
      inverseInertia - the desired component values (not null, unaffected)
    • setKinematic

      public void setKinematic(boolean kinematic)
      Put the body into kinematic mode or take it out of kinematic mode.

      In kinematic mode, the body is not influenced by physics but can affect other physics objects. Its kinetic force is calculated based on its mass and motion.

      Parameters:
      kinematic - true→set kinematic mode, false→set dynamic (default=false)
    • setLinearDamping

      public void setLinearDamping(float linearDamping)
      Alter the body's linear damping.
      Parameters:
      linearDamping - the desired linear damping fraction (≥0, ≤1, default=0)
    • setLinearFactor

      public void setLinearFactor(Vector3f factor)
      Alter the body's linear factors.
      Parameters:
      factor - the desired linear factor for each axis (not null, unaffected, default=(1,1,1))
    • setLinearSleepingThreshold

      public void setLinearSleepingThreshold(float threshold)
      Alter the body's linear-motion sleeping threshold. Note that "sleeping" is synonym for "deactivation".
      Parameters:
      threshold - the desired threshold (in physics-space units per second, ≥0, default=0.8)
    • setLinearVelocity

      public void setLinearVelocity(Vector3f velocity)
      Alter the linear velocity of the body's center of mass.
      Parameters:
      velocity - the desired velocity (physics-space units per second in physics-space coordinates, not null, finite, unaffected)
    • setLinearVelocityDp

      public void setLinearVelocityDp(Vec3d velocity)
      Alter the linear velocity of the body's center of mass.
      Parameters:
      velocity - the desired velocity (physics-space units per second in physics-space coordinates, not null, unaffected)
    • setPhysicsLocationDp

      public void setPhysicsLocationDp(Vec3d location)
      Directly relocate the body's center of mass.
      Parameters:
      location - the desired location (in physics-space coordinates, not null, unaffected)
    • setPhysicsRotation

      public void setPhysicsRotation(Matrix3f orientation)
      Directly alter the body's orientation.
      Parameters:
      orientation - the desired orientation (rotation matrix relative to physics-space coordinates, not null, unaffected)
    • setPhysicsRotation

      public void setPhysicsRotation(Quaternion orientation)
      Directly reorient the body.
      Parameters:
      orientation - the desired orientation (relative to physics-space coordinates, not null, not zero, unaffected)
    • setPhysicsRotationDp

      public void setPhysicsRotationDp(Matrix3d orientation)
      Directly reorient the body.
      Parameters:
      orientation - the desired orientation (rotation matrix relative to physics-space coordinates, not null, unaffected)
    • setPhysicsRotationDp

      public void setPhysicsRotationDp(Quatd orientation)
      Directly reorient the body.
      Parameters:
      orientation - the desired orientation (relative to physics-space coordinates, not null, not zero, unaffected)
    • setPhysicsScale

      public void setPhysicsScale(Vector3f newScale)
      Rescale the body. Note that if it has joints, their pivot locations will not be adjusted.
      Parameters:
      newScale - the desired scale factor for each local axis (not null, no negative component, unaffected, default=(1,1,1))
    • setPhysicsTransform

      public void setPhysicsTransform(Transform transform)
      Directly alter the body's transform, including the scale of its shape. If the body has joints, their pivot points will not be adjusted for scale changes.
      Parameters:
      transform - the desired transform (relative to physics-space coordinates, not null, unaffected)
    • setProtectGravity

      public void setProtectGravity(boolean newState)
      Alter whether the body's gravity should be overwritten if the body gets added to a PhysicsSpace or the gravity of the PhysicsSpace changes.
      Parameters:
      newState - true to preserve the body's gravity, false to allow it to be overwritten (default=false)
    • setSleepingThresholds

      public void setSleepingThresholds(float linear, float angular)
      Alter the body's sleeping thresholds. Note that "sleeping" is synonym for "deactivation".

      These thresholds influence whether the body will be deactivated to save resources. Low values keep the body active when it barely moves.

      Parameters:
      linear - the desired linear threshold (in physics-space units per second, ≥0, default=0.8)
      angular - the desired angular threshold (in radians per second, ≥0, default=1)
    • totalAppliedForce

      public Vector3f totalAppliedForce(Vector3f storeResult)
      Determine the total force applied to the body (excluding contact forces, damping, and gravity).
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      the total force (either storeResult or a new vector, mass times physics-space units per second squared in physics-space coordinates)
    • totalAppliedTorque

      public Vector3f totalAppliedTorque(Vector3f storeResult)
      Determine the total torque applied to the body (excluding contact forces and damping).
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      the total torque (either storeResult or a new vector, mass times physics-space units squared per second squared in physics-space coordinates)
    • postRebuild

      protected void postRebuild()
      For use by subclasses.
    • preRebuild

      protected void preRebuild()
      For use by subclasses.
    • getGravity

      public Vector3f getGravity(Vector3f storeResult)
      Copy the body's gravitational acceleration.
      Specified by:
      getGravity in class PhysicsBody
      Parameters:
      storeResult - storage for the result (modified if not null)
      Returns:
      an acceleration vector in physics-space coordinates (either storeResult or a new vector, not null)
    • getMass

      public float getMass()
      Return the body's mass.
      Specified by:
      getMass in class PhysicsBody
      Returns:
      the mass (>0) or zero for a static body
    • setGravity

      public void setGravity(Vector3f acceleration)
      Alter the body's gravitational acceleration.

      Invoke this method after adding the body to a PhysicsSpace. Unless protection is set, adding a body to a PhysicsSpace overrides its gravity.

      Specified by:
      setGravity in class PhysicsBody
      Parameters:
      acceleration - the desired acceleration vector (in physics-space coordinates, not null, finite, unaffected, default=(0,0,0))
      See Also:
    • setMass

      public void setMass(float mass)
      Alter the body's mass. Static bodies have mass=0. For dynamic bodies, it is best to keep the mass on the order of 1.
      Specified by:
      setMass in class PhysicsBody
      Parameters:
      mass - the desired mass (≥0, default=1)
    • setPhysicsLocation

      public void setPhysicsLocation(Vector3f location)
      Directly relocate the body's center of mass.
      Specified by:
      setPhysicsLocation in class PhysicsBody
      Parameters:
      location - the desired location (in physics-space coordinates, not null, finite, unaffected)