RigidBody

interface RigidBody

A physics structure which simulates dynamics - velocity, forces, friction, etc. - when attached to a PhysicsSpace. A body may have Colliders attached to it, which provide the physical collision shape used in contact response.

Body type

A body may have one of several RigidBodyTypes, which can change after construction:

  • RigidBodyType.FIXED - the body is not moved (position or velocity) by the physics engine

  • RigidBodyType.DYNAMIC - the body's position and velocity is managed by the physics engine, so it can be pushed by other colliders. The user can also specify forces to apply manually via Mut.applyForce and related methods.

  • RigidBodyType.KINEMATIC - the body's position and velocity is managed by the physics engine, but the user manually sets a desired target position via Mut.moveTo, and the engine calculates the required velocity to move it there.

Note that not all operations can be applied to all body types - for example, Mut.applyForce on a RigidBodyType.FIXED does not make sense. In these cases, the API will simply fail silently, rather than throw an exception. It is your responsibility to make sure your bodies are of the correct type before you call methods on them.

Continuous collision detection (CCD)

Since a PhysicsSpace uses a discrete time-stepping approach, collisions may get missed if a body is moving too fast. This is known as "tunneling", and it allows a body to effectively move through another body without any collisions being detected. In this case, setting the fast-moving body to have CCD enabled (using Mut.isCcdEnabled) will allow it to detect collisions via a "sweep" between its old and new position, and find collisions in between. If there is a collision, it will be teleported back to just before it collides that body, effectively preventing tunneling. This works for both the linear and angular velocities.

To check if a body is currently being simulated with CCD, use isCcdActive.

Sleeping

A body may not always be moving, so it would be a waste of resources to simulate its movement if it has not moved much in the past few physics steps. This process is called sleeping, and a body is automatically woken up if another body collides with it, or if woken up by the user.

Damping

To simulate phenomena like air resistance, it would be too computationally expensive to actually simulate fluid dynamics. Instead, we use a simple factor which the body's velocities are multiplied by on every time step. This effectively slows the body down more the faster that it moves. There are two damping factors:

Inheritors

Types

Link copied to clipboard
interface Mut : RigidBody

Mutable interface for a RigidBody.

Link copied to clipboard

Mutable owned interface for a RigidBody.

Properties

Link copied to clipboard
abstract val angularDamping: Double

The angular damping factor (see RigidBody).

Link copied to clipboard
abstract val angularVelocity: DVec3

How fast and in what direction the body is moving around its angular axes, in radians/second. This is represented as an axis-angle vector, where the magnitude is the angle.

Link copied to clipboard
abstract val appliedForce: DVec3

How much force has been applied by the user in this simulation step.

Link copied to clipboard
abstract val appliedTorque: DVec3

How much torque has been applied by the user in this simulation step.

Link copied to clipboard

The handles to colliders attached to this body.

Link copied to clipboard
abstract val gravityScale: Double

The factor by which gravity is applied to this body. Default of 1.

Link copied to clipboard
abstract val isCcdActive: Boolean

If this body is currently being simulated with CCD (see RigidBody).

Link copied to clipboard
abstract val isCcdEnabled: Boolean

If this body has CCD enabled (see RigidBody).

Link copied to clipboard
abstract val isSleeping: Boolean

If this body is currently sleeping (see RigidBody).

Link copied to clipboard
abstract val linearDamping: Double

The linear damping factor (see RigidBody).

Link copied to clipboard
abstract val linearVelocity: DVec3

How fast and in what direction the body is moving linearly, in meters/second.

Link copied to clipboard
abstract val position: DIso3

The absolute position of this body in the world.

Link copied to clipboard
abstract val type: RigidBodyType

The body type (see RigidBody).

Functions

Link copied to clipboard
abstract fun kineticEnergy(): Double

Calculates the kinetic energy of this body.