rigid_body_ccd
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 looking for collisions along the shapes motion: a rigid-body that moved fast during the timestep casts its colliders from their previous position to their new one, and its position is clamped to the first impact found this way. Its velocities are left untouched and the solver is responsible for preventing penetrations. Since the trajectory is clamped to some intermediate location along its path this technique is commonly called motion clamping.
Rapier applies CCD in two ways:
- Every fast-moving dynamic rigid-body is swept against the fixed colliders and the soft-bodies of the scene. This is automatic and doesn't need to be enabled: this prevents a falling crate from going through the floor, or a projectile from going through a wall, etc. Note however that this is not enabled fol dynamic rigid-bodies with mesh-like shapes (triangle meshes, polylines, heightfields) as that would be too computationally expensive.
- A dynamic rigid-body with CCD enabled (aka. a bullet) is swept against the kinematic and dynamic bodies as well. This is more expensive, therefore it is disabled by default and should be reserved to the objects that must not tunnel through moving obstacles. Note that two CCD-enabled objects might still tunel since the CCD resolution does currently not take both continuous motions into account simultaneously.
CCD takes action only if the rigid-body is moving fast relative to another collider. Therefore it is useless to enable it on fixed rigid-bodies and on rigid-bodies that are expected to move slowly.
The CCD feature, including the automatic sweeping of the fast dynamic bodies, can be fully disabled by setting the
maximum number of CCD substeps to zero in the
IntegrationParameters
(the R3IntegrationParameters.maxCcdSubsteps
field, whose default is 1). Larger values let a body resolve several successive impacts within a single timestep, at
the cost of additional sweeps.
It can also be modified directly with r3SetMaxCcdSubsteps.
Per-object CCD can be enabled when creating a rigid-body or after its creation:
/* Enable CCD when the rigid-body is created. */
R2RigidBodyDesc rigid_body = r2DynamicRigidBodyDesc();
rigid_body.ccdEnabled = 1;
/* Enable CCD after the rigid-body creation. */
r2RigidBody_SetCcdEnabled(rigid_body_handle, 1);
assert(r2RigidBody_IsCcdEnabled(rigid_body_handle));
Keep in mind that r3RigidBody_IsCcdEnabled only tells if CCD was enabled for this rigid-body. Whether a rigid-body
is currently moving fast enough for CCD to take action (which is also the case of fast dynamic bodies without CCD
enabled, since they are swept against the fixed colliders) is given by r3RigidBody_IsCcdActive.