Skip to main content

rigid_body_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 methodsflagsmethods for this.

/* 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 and/or rotations when the rigid-body bundle is created. */
commands
.spawn(RigidBody::Dynamic)
.insert(LockedAxes::TRANSLATION_LOCKED);
/* Lock translations and/or rotations inside of a system. */
fn modify_body_locked_flags(mut locked_axes: Query<&mut LockedAxes>) {
for mut locked_axes in locked_axes.iter_mut() {
*locked_axes = LockedAxes::ROTATION_LOCKED;
}
}
/* Lock translations/rotations when the rigid-body is created. */
let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic()
.lockTranslations() // prevent translations along along all axes.
.lockRotations(); // prevent rotations.
let rigidBody = world.createRigidBody(rigidBodyDesc);
/* Lock translations/rotations after the rigid-body creation. */
// The last `true` argument makes sure the rigid-body is awake.
rigidBody.lockTranslations(true, true);
rigidBody.lockRotations(true, true);