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 flags for this.

The locked axes are given by a LockedAxes value combining, with the | operator, the TRANSLATION_LOCKED_X, TRANSLATION_LOCKED_Y, TRANSLATION_LOCKED_Z, ROTATION_LOCKED_X, ROTATION_LOCKED_Y, and ROTATION_LOCKED_Z flags (or TRANSLATION_LOCKED and ROTATION_LOCKED to lock all the translations or all the rotations at once). It is set with RigidBodyBuilder.locked_axes when the rigid-body is created, or with the RigidBody.locked_axes property afterwards. Alternatively, the enabled_translations and enabled_rotations builder methods and properties take a tuple of three booleans indicating, for each axis, if the corresponding translation or rotation is allowed.

# Lock translations/rotations when the rigid-body is created.
rigid_body = (
rp.RigidBody.dynamic()
# Prevent translations along all axes, and rotations around all axes.
.locked_axes(rp.LockedAxes.TRANSLATION_LOCKED | rp.LockedAxes.ROTATION_LOCKED)
# Only enable rotations around the X axis.
.enabled_rotations((True, False, False))
.build()
)
# Lock translations/rotations after the rigid-body creation.
rigid_body = world.rigid_bodies[rigid_body_handle]
# Setting these properties automatically wakes the rigid-body up.
rigid_body.locked_axes = rp.LockedAxes.TRANSLATION_LOCKED | rp.LockedAxes.ROTATION_LOCKED
# Only enable rotations around the X axis.
rigid_body.enabled_rotations = (True, False, False)