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 flags for this.
- Example 2D
- Example 3D
/* 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 and/or rotations when the rigid-body bundle is created. */
commands
.spawn(RigidBody::Dynamic)
.insert(LockedAxes::TRANSLATION_LOCKED | LockedAxes::ROTATION_LOCKED_X);
/* 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;
}
}