Skip to main content

collider_position

The position of a collider represents its location (translation) in 2D or 3D world-space as well as its orientation (rotation).

Its translational part is represented as a vector (Vec3) and its rotational part as an unit quaternion (Rotation3). Both are combined into a pose (the Isometry3 type).

warning

Please read carefully the paragraph after the next example. It explains how the collider position (and the action of setting this position) behaves differently when it is attached to a rigid-body.

It is possible to set this position when the collider is created or after its creation:

# Set the collider position when the collider is created.
collider = (
rp.Collider.ball(0.5)
.translation((1.0, 2.0, 3.0))
.rotation((0.1, 0.2, 0.4))
# Set both translation and rotation at once.
.position(rp.Isometry3((1.0, 2.0, 3.0), rp.Rotation3.from_scaled_axis((0.1, 0.2, 0.4))))
.build()
)
# Set the collider position after the collider creation.
collider = world.colliders[collider_handle]
collider.translation = (1.0, 2.0, 3.0)
collider.rotation = rp.Rotation3.from_scaled_axis((0.1, 0.2, 0.4))
# Set both the translation and rotation at once.
collider.position = rp.Isometry3((1.0, 2.0, 3.0), rp.Rotation3.from_scaled_axis((0.1, 0.2, 0.4)))
assert collider.translation == (1.0, 2.0, 3.0)
assert (collider.rotation.scaled_axis - (0.1, 0.2, 0.4)).norm() < 1.0e-6

If a collider is attached to a rigid-body, its position is automatically updated by the physics pipeline when a rigid-body is moved by the physics pipeline. If a change to the rigid-body position is made by the user then the collider position will be updated during the next timestep.

Therefore, directly setting the position of a collider attached to a rigid-body (by assigning its position, translation, or rotation property) will have no lasting effect. Instead, it is possible to set the position of the collider relative to the rigid-body it is attached to: this is the position given to its builder, which can be modified after its creation by assigning its position_wrt_parent property (or only its translation or rotation part, with the translation_wrt_parent or rotation_wrt_parent property):

rigid_body_handle = world.add_body(rp.RigidBody.dynamic())
collider = rp.Collider.ball(0.5).translation((1.0, 2.0, 3.0)).build()
# Attach the collider to the rigid-body. The collider's position wrt. the rigid-body
# is automatically set to the collider current position when this method is called.
attached_collider_handle = world.add_collider(collider, parent=rigid_body_handle)
# Set the collider position wrt. its parent after the collider creation.
collider = world.colliders[attached_collider_handle]
collider.position_wrt_parent = rp.Isometry3.from_translation(1.0, 2.0, 3.0)
assert collider.position_wrt_parent.translation == (1.0, 2.0, 3.0)