Rigid-bodies
The real-time simulation of rigid-bodies subjected to forces and contacts is the main feature of a physics engine for video-games, robotics, or animation. Rigid-bodies are typically used to simulate the dynamics of non-deformable solids as well as to integrate the trajectory of solids which velocities are controlled by the user (e.g. moving platforms). On the other hand, rigid-bodies are not enough to simulate, e.g., cars, ragdolls, or robotic systems, as those use-cases require adding restrictions on the relative motion between their parts using joints.
Note that rigid-bodies are only responsible for the dynamics and kinematics of the solid. Colliders can be attached to a rigid-body to specify its shape and enable collision-detection. A rigid-body without collider attached to it will not be affected by contacts (because there is no shape to compute contact against).
Creation and insertion
A rigid-body is created by a RigidBodyBuilder
structure that is based on the builder pattern. Then it needs
to be inserted into the RigidBodySet
that will be processed by the physics-pipeline or query-pipeline.
The following example shows several setters that can be called to customize the rigid-body being built. The input values are just random so using this example as-is will not lead to a useful result.
- Example 2D
- Example 3D
use rapier2d::prelude::*;
// The set that will contain our rigid-bodies.
let mut rigid_body_set = RigidBodySet::new();
// Builder for a fixed rigid-body.
let _ = RigidBodyBuilder::fixed();
// Builder for a dynamic rigid-body.
let _ = RigidBodyBuilder::dynamic();
// Builder for a kinematic rigid-body controlled at the velocity level.
let _ = RigidBodyBuilder::kinematic_velocity_based();
// Builder for a kinematic rigid-body controlled at the position level.
let _ = RigidBodyBuilder::kinematic_position_based();
// Builder for a body with a status specified by an enum.
let rigid_body = RigidBodyBuilder::new(RigidBodyType::Dynamic)
// The rigid body translation.
// Default: zero vector.
.translation(vector![0.0, 5.0])
// The rigid body rotation.
// Default: no rotation.
.rotation(5.0)
// The rigid body position. Will override `.translation(...)` and `.rotation(...)`.
// Default: the identity isometry.
.position(Isometry::new(vector![1.0, 2.0], 0.4))
// The linear velocity of this body.
// Default: zero velocity.
.linvel(vector![1.0, 2.0])
// The angular velocity of this body.
// Default: zero velocity.
.angvel(2.0)
// The scaling factor applied to the gravity affecting the rigid-body.
// Default: 1.0
.gravity_scale(0.5)
// Whether or not this body can sleep.
// Default: true
.can_sleep(true)
// Whether or not CCD is enabled for this rigid-body.
// Default: false
.ccd_enabled(false)
// All done, actually build the rigid-body.
.build();
// Insert the rigid-body into the set.
let rigid_body_handle = rigid_body_set.insert(rigid_body);
use rapier3d::prelude::*;
// The set that will contain our rigid-bodies.
let mut rigid_body_set = RigidBodySet::new();
// Builder for a fixed rigid-body.
let _ = RigidBodyBuilder::fixed();
// Builder for a dynamic rigid-body.
let _ = RigidBodyBuilder::dynamic();
// Builder for a kinematic rigid-body controlled at the velocity level.
let _ = RigidBodyBuilder::kinematic_velocity_based();
// Builder for a kinematic rigid-body controlled at the position level.
let _ = RigidBodyBuilder::kinematic_position_based();
// Builder for a body with a status specified by an enum.
let rigid_body = RigidBodyBuilder::new(RigidBodyType::Dynamic)
// The rigid body translation.
// Default: zero vector.
.translation(vector![0.0, 5.0, 1.0])
// The rigid body rotation.
// Default: no rotation.
.rotation(vector![0.0, 0.0, 5.0])
// The rigid body position. Will override `.translation(...)` and `.rotation(...)`.
// Default: the identity isometry.
.position(Isometry::new(
vector![1.0, 3.0, 2.0],
vector![0.0, 0.0, 0.4],
))
// The linear velocity of this body.
// Default: zero velocity.
.linvel(vector![1.0, 3.0, 4.0])
// The angular velocity of this body.
// Default: zero velocity.
.angvel(vector![3.0, 0.0, 1.0])
// The scaling factor applied to the gravity affecting the rigid-body.
// Default: 1.0
.gravity_scale(0.5)
// Whether or not this body can sleep.
// Default: true
.can_sleep(true)
// Whether or not CCD is enabled for this rigid-body.
// Default: false
.ccd_enabled(false)
// All done, actually build the rigid-body.
.build();
// Insert the rigid-body into the set.
let rigid_body_handle = rigid_body_set.insert(rigid_body);
All the properties are optional. The only calls that are required are RigidBodyBuilder::new(status)
,
RigidBodyBuilder::fixed()
, RigidBodyBuilder::dynamic()
, RigidBodyBuilder::kinematic_velocity_based()
,
or RigidBodyBuilder::kinematic_position_based()
, to
initialize the builder, and .build()
to actually build the rigid-body.
Typically, the inertia and center of mass are automatically set to the inertia and center of mass resulting from the shapes of the colliders attached to the rigid-body. But they can also be set manually.
Rigid-body type
There are four types of rigid-bodies, identified by the RigidBodyType
enumeration:
RigidBodyType::Dynamic
: Indicates that the body is affected by external forces and contacts.RigidBodyType::Fixed
: Indicates the body cannot move. It acts as if it has an infinite mass and will not be affected by any force. It will continue to collide with dynamic bodies but not with fixed nor with kinematic bodies. This is typically used for the ground or for temporarily freezing a body.RigidBodyType::KinematicPositionBased
: Indicates that the body position must not be altered by the physics engine. The user is free to set its next position and the body velocity will be deduced at each update accordingly to ensure a realistic behavior of dynamic bodies in contact with it. This is typically used for moving platforms, elevators, etc.RigidBodyType::KinematicVelocityBased
: Indicates that the body velocity must not be altered by the physics engine. The user is free to set its velocity and the next body position will be deduced at each update accordingly to ensure a realistic behavior of dynamic bodies in contact with it. This is typically used for moving platforms, elevators, etc.
Both position-based and velocity-based kinematic bodies are mostly the same. Choosing between both is mostly a matter of preference between position-based control and velocity-based control.
The whole point of kinematic bodies is to let the user have total control over their trajectory. This means that kinematic bodies will simply ignore any contact force and go through walls and the ground. In other words: if you tell the kinematic to go somewhere, it will go there, no questions asked.
Taking obstacles into account needs to be done manually either by using scene queries to detect nearby obstacles, or by using the built-in character controller.
Position
The position of a rigid-body represents its location (translation) in 2D or 3D world-space, as well as its orientation (rotation). Its translational part is represented as a vector and its rotational part as an unit quaternion (in 3D) or a unit complex number (in 2D). Both are combined into an isometry.
The position of a rigid-body can be set when creating it. It can also be set after its creation as illustrated below.
Directly changing the position of a rigid-body is equivalent to teleporting it: this is a not a physically realistic action! Teleporting a dynamic or kinematic bodies may result in odd behaviors especially if it teleports into a space occupied by other objects. For dynamic bodies, forces, impulses, or velocity modification should be preferred. For kinematic bodies, see the discussion after the examples below.
- Example 2D
- Example 3D
/* Set the position when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
// The rigid body translation.
// Default: zero vector.
.translation(vector![0.0, 5.0])
// The rigid body rotation.
// Default: no rotation.
.rotation(5.0)
// The rigid body position. Will override `.translation(...)` and `.rotation(...)`.
// Default: the identity isometry.
.position(Isometry::new(vector![1.0, 2.0], 0.4))
// All done, actually build the rigid-body.
.build();
use nalgebra::UnitComplex;
/* Set the position after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
// The `true` argument makes sure the rigid-body is awake.
rigid_body.set_translation(vector![0.0, 5.0], true);
rigid_body.set_rotation(UnitComplex::new(0.2), true);
assert_eq!(*rigid_body.translation(), vector![0.0, 5.0]);
assert_eq!(rigid_body.rotation().angle(), 0.2);
rigid_body.set_position(Isometry::new(vector![1.0, 2.0], 0.4), true);
assert_eq!(
*rigid_body.position(),
Isometry::new(vector![1.0, 2.0], 0.4)
);
/* Set the position when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
// The rigid body translation.
// Default: zero vector.
.translation(vector![0.0, 5.0, 1.0])
// The rigid body rotation.
// Default: no rotation.
.rotation(vector![0.2, 0.0, 0.0])
// The rigid body position. Will override `.translation(...)` and `.rotation(...)`.
// Default: the identity isometry.
.position(Isometry::new(
vector![1.0, 2.0, 3.0],
vector![0.2, 0.0, 0.0],
))
// All done, actually build the rigid-body.
.build();
/* Set the position after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
// The `true` argument makes sure the rigid-body is awake.
rigid_body.set_translation(vector![0.0, 5.0, 1.0], true);
rigid_body.set_rotation(Rotation::from_scaled_axis(vector![0.2, 0.0, 0.0]), true);
assert_eq!(*rigid_body.translation(), vector![0.0, 5.0, 1.0]);
assert_eq!(rigid_body.rotation().scaled_axis(), vector![0.2, 0.0, 0.0]);
rigid_body.set_position(
Isometry::new(vector![1.0, 2.0, 3.0], vector![0.0, 0.4, 0.0]),
true,
);
assert_eq!(
*rigid_body.position(),
Isometry::new(vector![1.0, 2.0, 3.0], vector![0.0, 0.4, 0.0])
);
In order to move a dynamic rigid-body it is strongly discouraged to set its position directly as it may results in weird behaviors: it's as if the rigid-body teleports itself, which is a non-physical behavior. For dynamic bodies, it is recommended to either set its velocity or to apply forces or impulses.
For velocity-based kinematic bodies, it is recommended to set its velocity instead of setting its position directly. For position-based kinematic bodies, it is recommended to use the special methods:
RigidBody::set_next_kinematic_rotation
RigidBody::set_next_kinematic_translation
These methods will let the physics pipeline compute the fictitious velocity of the position-based kinematic body for more realistic interactions with other rigid-bodies. These methods won't immediately modify the position of the kinematic body itself. The position of the kinematic body will be automatically set to these values during the next physics pipeline update.
Velocity
The velocity of a dynamic rigid-body controls how fast it is moving in time. The velocity is applied at the center-of-mass of the rigid-body, and is composed of two independent parts:
- The linear velocity is specified as a vector representing the direction and magnitude of the movement.
- In 3D, the angular velocity is given as a vector representing the rotation
axis multiplied by the rotation angular speed in
rad/s
(axis-angle representation). In 2D, the angular velocity is given as a real representing the angular speed inrad/s
.
The velocity is only relevant to dynamic rigid-bodies. It has no effect on fixed rigid-bodies, and the velocity of kinematic rigid-bodies are automatically computed at each timestep based on their next kinematic positions.
The velocity of a rigid-body is automatically updated by the physics pipeline after taking forces, contacts, and joints into account. It can be set when the rigid-body is created or after its creation:
- Example 2D
- Example 3D
/* Set the velocities when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
// The linear velocity of this body.
// Default: zero velocity.
.linvel(vector![1.0, 3.0])
// The angular velocity of this body.
// Default: zero velocity.
.angvel(3.0)
// All done, actually build the rigid-body.
.build();
/* Set the velocities after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
// The `true` argument makes sure the rigid-body is awake.
rigid_body.set_linvel(vector![1.0, 3.0], true);
rigid_body.set_angvel(3.0, true);
assert_eq!(*rigid_body.linvel(), vector![1.0, 3.0]);
assert_eq!(rigid_body.angvel(), 3.0);
/* Set the velocities when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
// The linear velocity of this body.
// Default: zero velocity.
.linvel(vector![1.0, 3.0, 4.0])
// The angular velocity of this body.
// Default: zero velocity.
.angvel(vector![3.0, 0.0, 0.0])
// All done, actually build the rigid-body.
.build();
/* Set the velocities after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
// The `true` argument makes sure the rigid-body is awake.
rigid_body.set_linvel(vector![1.0, 3.0, 4.0], true);
rigid_body.set_angvel(vector![3.0, 0.0, 0.0], true);
assert_eq!(*rigid_body.linvel(), vector![1.0, 3.0, 4.0]);
assert_eq!(*rigid_body.angvel(), vector![3.0, 0.0, 0.0]);
Alternatively, the velocity of a dynamic rigid-body can be altered indirectly by applying a force or an impulse.
Gravity
Gravity is such a common force that it is implemented as a special case (even if it could easily be implemented
by the user using force application). The gravity is given as an argument to the
PhysicsPipeline::step
method and can be modified at will by simply modifying that argument. Note however that a change of gravity won't automatically wake-up the
sleeping bodies so keep in mind that you may want to wake them up manually before a gravity change.
Because fixed and kinematic bodies are immune to forces, they are not affected by gravity.
A rigid-body with no mass will not be affected by gravity either. So if your rigid-body doesn't fall when you expected it to, make sure it has a mass set explicitly, or has at least one collider with non-zero density attached to it.
It is possible to change the way gravity affects a specific rigid-body by setting the rigid-body's gravity scale
to a value other than 1.0
. The magnitude of the gravity applied to this body will be multiplied by this scaling
factor. Therefore, a gravity scale set to 0.0
will disable gravity for the rigid-body whereas a gravity scale set to
2.0
will make it twice as strong. A negative value will flip the direction of the gravity for this rigid-body.
This gravity scale factor can be set when the rigid-body is created or after its creation:
/* Set the gravity scale when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
// Divide by 2 the strength of gravity for this rigid-body.
.gravity_scale(0.5)
.build();
/* Set the gravity scale after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
// The `true` argument makes sure the rigid-body is awake.
rigid_body.set_gravity_scale(0.5, true);
assert_eq!(rigid_body.gravity_scale(), 0.5);
Forces and impulses
In addition to gravity, it is possible to add custom forces (or torques) or apply impulses (or torque impulses) to dynamic rigid-bodies in order to make them move in specific ways. Forces affect the rigid-body's acceleration whereas impulses affect the rigid-body's velocity. They are both based on the familiar equations:
- Forces: the acceleration change is equal to the force divided by the mass:
- Impulses: the velocity change is equal to the impulse divided by the mass:
Forces can be added, and impulses can be applied, to a rigid-body after it has been created. Added forces are persistent across simulation steps, and can be cleared manually.
- Example 2D
- Example 3D
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
// The `true` argument makes sure the rigid-body is awake.
rigid_body.reset_forces(true); // Reset the forces to zero.
rigid_body.reset_torques(true); // Reset the torques to zero.
rigid_body.add_force(vector![0.0, 1000.0], true);
rigid_body.add_torque(100.0, true);
rigid_body.add_force_at_point(vector![0.0, 1000.0], point![1.0, 2.0], true);
rigid_body.apply_impulse(vector![0.0, 1000.0], true);
rigid_body.apply_torque_impulse(100.0, true);
rigid_body.apply_impulse_at_point(vector![0.0, 1000.0], point![1.0, 2.0], true);
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
// The `true` argument makes sure the rigid-body is awake.
rigid_body.reset_forces(true); // Reset the forces to zero.
rigid_body.reset_torques(true); // Reset the torques to zero.
rigid_body.add_force(vector![0.0, 1000.0, 0.0], true);
rigid_body.add_torque(vector![100.0, 0.0, 0.0], true);
rigid_body.add_force_at_point(vector![0.0, 1000.0, 0.0], point![1.0, 2.0, 3.0], true);
rigid_body.apply_impulse(vector![0.0, 1000.0, 0.0], true);
rigid_body.apply_torque_impulse(vector![100.0, 0.0, 0.0], true);
rigid_body.apply_impulse_at_point(vector![0.0, 1000.0, 0.0], point![1.0, 2.0, 3.0], true);
Keep in mind that a dynamic rigid-body with a zero mass won't be affected by a linear force/impulse, and a rigid-body with a zero angular inertia won't be affected by torques/torque impulses. So if your force doesn't appear to do anything, make sure that:
-
The rigid-body is dynamic.
-
It is strong enough to make the rigid-body move (try a very large value and see if it does something).
-
The rigid-body has a non-zero mass or angular inertia either because they were set explicitly, or because they were computed automatically from colliders with non-zero densities.
-
The rigid-body is awake (by waking it up manually or setting the last
wake_up
parameter totrue
).
Mass properties
The mass properties of a rigid-body is composed of three parts:
- The mass which determines the resistance of the rigid-body wrt. linear movements. A high mass implies that larger forces are needed to make the rigid-body translate.
- The angular inertia determines the resistance of the rigid-body wrt. the angular movements. A high angular inertia implies that larger torques are needed to make the rigid-body rotate.
- The center-of-mass determines relative to what points torques are applied to the rigid-body.
Zero is a special value for masses and angular inertia. A mass equal to zero is interpreted as an infinite mass. An angular inertia equal to zero is interpreted as an infinite angular inertia. Therefore, a rigid-body with a mass equal to zero will not be affected by any force, and a rigid-body with an angular inertia equal to zero will not be affected by any torque.
Computing the mass and angular-inertia can often be difficult because they depend on the geometric shape of the object being simulated. This is why they are automatically computed by Rapier when a collider is attached to the rigid-body: the collider add its own mass and angular-inertia contribution (computed based on the collider's shape and density) to the rigid-body it is attached to:
let rigid_body = RigidBodyBuilder::dynamic().build();
let rigid_body_handle = rigid_body_set.insert(rigid_body);
// The default density is 1.0, we are setting 2.0 for this example.
let collider = ColliderBuilder::ball(1.0).density(2.0).build();
// When the collider is attached, the rigid-body's mass and angular
// inertia is automatically updated to take the collider into account.
collider_set.insert_with_parent(collider, rigid_body_handle, &mut rigid_body_set);
Alternatively, it is possible to set the mass properties of a rigid-body when it is created. Keep in mind that this won't prevent the colliders' contributions to be added to these values. So make sure to set the attached colliders' densities to zero if you want your explicit values to be the final mass-properties values.
- Example 2D
- Example 3D
/* Set the mass-properties when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
.additional_mass(0.5)
// Sets both the mass and angular inertia at once.
.additional_mass_properties(MassProperties::new(point![0.0, 1.0], 0.5, 0.3))
.build();
/* Set the mass-properties after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
// The `true` argument makes sure the rigid-body is awake.
rigid_body
.set_additional_mass_properties(MassProperties::new(point![0.0, 1.0], 0.5, 0.3), true);
/* Set the mass-properties when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
.additional_mass(0.5)
// Sets both the mass and angular inertia at once.
.additional_mass_properties(MassProperties::new(
point![0.0, 1.0, 0.0],
0.5,
vector![0.3, 0.2, 0.1],
))
.build();
/* Set the mass-properties after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
// The `true` argument makes sure the rigid-body is awake.
rigid_body.set_additional_mass_properties(
MassProperties::new(point![0.0, 1.0, 0.0], 0.5, vector![0.3, 0.2, 0.1]),
true,
);
Locking translations/rotations
It is sometimes useful to prevent a rigid-body from rotating or translating. One typical use-case for locking rotations is to prevent a player modeled as a dynamic rigid-body from tilting. These kind of degree-of-freedom restrictions could be achieved by joints, but locking translations/rotations of a single rigid-body wrt. the cartesian coordinate axes can be done in a much more efficient and numerically stable way. That's why rigid-bodies have dedicated methods for this.
- Example 2D
- Example 3D
/* Lock translations/rotations when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
.lock_translations() // prevent translations along along all axes.
.lock_rotations() // prevent rotations.
.build();
/* Lock translations/rotations after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
// The last `true` argument makes sure the rigid-body is awake.
rigid_body.lock_translations(true, true);
rigid_body.lock_rotations(true, true);
/* Lock translations/rotations when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
.lock_translations() // prevent translations along along all axes.
.lock_rotations() // prevent rotations along all axes.
.enabled_rotations(true, false, false) // only enable rotations along the X axis.
.build();
/* Lock translations/rotations after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
// The last `true` argument makes sure the rigid-body is awake.
rigid_body.lock_translations(true, true);
rigid_body.lock_rotations(true, true);
rigid_body.set_enabled_rotations(true, false, false, true);
Damping
Damping lets you slow down a rigid-body automatically. This can be used to achieve a wide variety of effects like
fake air friction. Each rigid-body is given a linear damping coefficient (affecting its linear velocity) and an
angular damping coefficient (affecting its angular velocity). Larger values of the damping coefficients lead to
a stronger slow-downs. Their default values are 0.0
(no damping at all).
This damping coefficients can be set when the rigid-body is created or after its creation:
/* Set the damping coefficients when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
.linear_damping(0.5)
.angular_damping(1.0)
.build();
/* Set the damping coefficients after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
rigid_body.set_linear_damping(0.5);
rigid_body.set_angular_damping(1.0);
assert_eq!(rigid_body.linear_damping(), 0.5);
assert_eq!(rigid_body.angular_damping(), 1.0);
Dominance
Dominance is a non-realistic, but sometimes useful, feature. It can be used to make one rigid-body immune to forces originating from contacts with some other bodies. For example this can be used to model a player represented as a dynamic rigid-body that cannot be "pushed back" by any, or some, other dynamic rigid-bodies part of the environment.
Each rigid-body is part of a dominance group in [-127; 127]
(the default group is 0
). If the colliders from two rigid-bodies
are in contact, the one with the highest dominance will act as if it has an infinite mass, making it immune to the
contact forces the other body would apply on it. If both bodies are part of the same dominance group, then their
contacts will work in the usual way (both are affected by opposite forces with the same magnitude).
For example, if a dynamic body A
is in the dominance group 10
, and a dynamic body B
in the dominance group -20
, then
a contact between a collider attached to A
and a collider attached B
will result in A
remaining immobile and B
being
pushed by A
(independently from their mass).
A non-dynamic rigid-body is always considered as being part of a dominance group greater than any dynamic rigid-body. This means that dynamic/fixed and dynamic/kinematic contacts will continue to work normally, independently from the dominance group they were given by the user.
The dominance group can be set when the rigid-body is created or after its creation:
/* Set the dominance group when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic().dominance_group(10).build();
/* Set the dominance group after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
rigid_body.set_dominance_group(10);
assert_eq!(rigid_body.dominance_group(), 10);
Continuous collision detection
Continuous Collision Detection (CCD) is used to make sure that fast-moving objects don't miss any contacts (a problem
usually called tunneling). This is done by using motion-clamping, i.e., each fast-moving rigid-body with CCD enabled
will be stopped at the time where their first contact happen, taking their continuous motion into account. This will result
in some "time loss" for that rigid-body. This loss of time can be reduced by increasing the maximum number of CCD
substeps executed (the default being 1) in the IntegrationParameters
(by changing the IntegrationParameters::max_ccd_substeps
field).
Rapier implements nonlinear CCD, meaning that it takes into account both the angular and translational motion of the rigid-body.
CCD takes action only if the CCD-enabled rigid-body is moving fast relative to another rigid-body. Therefore it is useless to enable CCD on fixed rigid-bodies and rigid-bodies that are expected to move slowly.
By default, CCD is disabled for all the rigid-bodies because it requires additional computations. It can be enabled when creating a rigid-body or after its creation:
/* Enable CCD when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic().ccd_enabled(true).build();
/* Enable CCD after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
rigid_body.enable_ccd(true);
assert_eq!(rigid_body.is_ccd_enabled(), true);
Sleeping
When a dynamic rigid-body doesn't move (or moves very slowly) during a few seconds, it will be marked as sleeping by the physics pipeline. Rigid-bodies marked as sleeping are no longer simulated by the physics engine until they are woken up. That way the physics engine doesn't waste any computational resources simulating objects that don't actually move. They are woken up automatically whenever another non-sleeping rigid-body starts interacting with them (either with a joint, or with one of its attached colliders generating contacts).
However, a sleeping rigid-body won't respond to any user action. This is why it is possible to wake-up the rigid-body
manually with RigidBody::wake_up
. Some rigid-body methods take an additional wake_up
boolean argument that, if
true, ensures that the rigid-body wakes up before the action takes place. For example:
RigidBody::add_force(force, true)
will wake-up the rigid-body before adding the force.ImpulseJointSet::remove(..., true)
will wake-up the two rigid-bodies attached by the removed joints.ColliderSet::remove(..., true)
will wake-up the rigid-body the removed collider is attached to.
Unless you want to achieve special effects, it is recommended to always set the wake_up
argument to true
.
One example of case where setting the argument of wake_up
to false
makes sense is to simulate a custom
constant gravity with RigidBody::add_force(force, false)
. This will result in the force being added
to the rigid-body, but will allow the rigid-body to fall asleep if it reaches a dynamic equilibrium.
User-data
Each rigid-body can be given a user-defined data of type u128
. This integer can have any value
and is never used/modified by the physics-engine. This can for example be useful to add some custom data for
custom contact filtering/modification.
This user-data can be set when the rigid-body is created or after its creation:
/* Set the user-data when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic().user_data(42).build();
/* Set the user-data after the rigid-body creation. */
let rigid_body = rigid_body_set.get_mut(rigid_body_handle).unwrap();
rigid_body.user_data = 42;
assert_eq!(rigid_body.user_data, 42);