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 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);