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 and its rotational part as an unit quaternion (in 3D) or a unit complex number (in 2D). Both are combined into an isometry.

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. */
let collider = ColliderBuilder::ball(0.5)
.translation(vector![1.0, 2.0])
.rotation(0.4)
// Set both translation and rotation at once.
.position(Isometry::new(vector![1.0, 2.0], 0.4))
.build();
/* Set the collider position after the collider creation. */
let collider = collider_set.get_mut(collider_handle).unwrap();
collider.set_translation(vector![1.0, 2.0]);
collider.set_rotation(UnitComplex::new(0.4));
// Set both the translation and rotation at once.
collider.set_position(Isometry::new(vector![1.0, 2.0], 0.4));
assert_eq!(*collider.translation(), vector![1.0, 2.0]);
assert_eq!(collider.rotation().angle(), 0.4);

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 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:

let rigid_body = RigidBodyBuilder::dynamic().build();
let rigid_body_handle = rigid_body_set.insert(rigid_body);
let collider = ColliderBuilder::ball(0.5)
.translation(vector![1.0, 2.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.
collider_set.insert_with_parent(collider, rigid_body_handle, &mut rigid_body_set);
/* Set the collider position wrt. its parent after the collider creation. */
let collider = collider_set.get_mut(collider_handle).unwrap();
collider.set_position_wrt_parent(Isometry::translation(1.0, 2.0));
assert_eq!(
collider.position_wrt_parent().unwrap().translation.vector,
vector![1.0, 2.0]
);