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). Both are combined in a Bevy Transform component. 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 a pose (the Pose type). Its translational part is represented as a vector and its rotational part as an unit quaternion (in 3D) or an angle (in 2D). Its translational part is represented as a vector (R3Vector) and its rotational part (R3Rotation) as an unit quaternion (in 3D) or an angle (in 2D). Both are combined into a pose (the R3Pose type). 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. */
let collider = ColliderBuilder::ball(0.5)
.translation(Vector::new(1.0, 2.0))
.rotation(0.4)
// Set both translation and rotation at once.
.position(Pose::new(Vector::new(1.0, 2.0), 0.4))
.build();
/* Set the collider position after the collider creation. */
let collider = &mut world.colliders[collider_handle];
collider.set_translation(Vector::new(1.0, 2.0));
collider.set_rotation(Rotation::new(0.4));
// Set both the translation and rotation at once.
collider.set_position(Pose::new(Vector::new(1.0, 2.0), 0.4));
assert_eq!(collider.translation(), Vector::new(1.0, 2.0));
assert_eq!(collider.rotation().angle(), 0.4);
/* Set the collider position when the collider is created. */
commands
.spawn(Collider::cuboid(0.5, 0.5))
.insert(Transform::from_xyz(1.0, 2.0, 0.0));
/* Set the collider position inside of a system. */
fn modify_collider_position(mut positions: Query<&mut Transform, With<Collider>>) {
for mut position in positions.iter_mut() {
position.translation.x = 2.0;
}
}
/* Set the collider position when the collider is created. */
let colliderDesc = RAPIER.ColliderDesc.ball(0.5)
.setTranslation(1.0, 2.0)
.setRotation(0.4);
let collider = world.createCollider(colliderDesc);
/* Set the collider position after the collider creation. */
collider.setTranslation({ x: 1.0, y: 2.0 });
collider.setRotation(0.4);
/* Set the collider position when the collider is created. */
R2ColliderDesc collider = r2BallColliderDesc(0.5);
collider.position.translation = r2Vector(1.0, 2.0);
collider.position.rotation = r2Rotation(0.4);
// Set both translation and rotation at once.
collider.position = r2Pose(r2Vector(1.0, 2.0), r2Rotation(0.4));
/* Set the collider position after the collider creation. */
r2Collider_SetTranslation(collider_handle, r2Vector(1.0, 2.0));
r2Collider_SetRotation(collider_handle, r2Rotation(0.4));
// Set both the translation and rotation at once.
r2Collider_SetPosition(collider_handle, r2Pose(r2Vector(1.0, 2.0), r2Rotation(0.4)));
R2Vector translation = r2Collider_Translation(collider_handle);
assert(translation.x == 1.0 && translation.y == 2.0);
assert(fabs(r2Collider_Rotation(collider_handle).angle - 0.4) < 1.0e-6);
# 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, if the collider is attached to a rigid-body through a child entity, setting its Transform will modify the position of the collider relative to the rigid-body it is attached to (if the collider is on the same entity as the rigid-body, its Transform is the one of the rigid-body): 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: 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: Therefore, directly setting the position of a collider attached to a rigid-body (with r3Collider_SetPosition, r3Collider_SetTranslation, or r3Collider_SetRotation) 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 field of its description, which can be modified after its creation with r3Collider_SetPositionWrtParent: 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):
let rigid_body = RigidBodyBuilder::dynamic().build();
let rigid_body_handle = world.insert_body(rigid_body);
let collider = ColliderBuilder::ball(0.5)
.translation(Vector::new(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.
let attached_collider_handle = world.insert_collider(collider, Some(rigid_body_handle));
/* Set the collider position wrt. its parent after the collider creation. */
let collider = &mut world.colliders[attached_collider_handle];
collider.set_position_wrt_parent(Pose::translation(1.0, 2.0));
assert_eq!(
collider.position_wrt_parent().unwrap().translation,
Vector::new(1.0, 2.0)
);
// Attach the collider to the rigid-body. The collider is attached as its
// children, so the collider’s `Transform` components sets its position
// relative to the parent rigid-body.
commands
.spawn((RigidBody::Dynamic, GlobalTransform::default()))
.with_children(|children| {
children
.spawn(Collider::cuboid(0.5, 0.5))
.insert(Transform::from_xyz(1.0, 2.0, 0.0));
});
let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic();
let rigidBody = world.createRigidBody(rigidBodyDesc);
let colliderDesc = RAPIER.ColliderDesc.ball(0.5)
.setTranslation(1.0, 2.0);
// 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.
let collider = world.createCollider(colliderDesc, rigidBody);
/* Set the collider position wrt. its parent after the collider creation. */
collider.setTranslationWrtParent({ x: 1.0, y: 2.0 });
R2RigidBodyDesc rigid_body = r2DynamicRigidBodyDesc();
R2RigidBodyHandle rigid_body_handle = r2InsertRigidBody(world, &rigid_body);
R2ColliderDesc collider = r2BallColliderDesc(0.5);
collider.position.translation = r2Vector(1.0, 2.0);
// Attach the collider to the rigid-body. The description's position is
// the collider's position wrt. the rigid-body.
R2ColliderHandle collider_handle = r2InsertCollider(rigid_body_handle, &collider);
/* Set the collider position wrt. its parent after the collider creation. */
r2Collider_SetPositionWrtParent(collider_handle, r2TranslationPose(r2Vector(1.0, 2.0)));
R2Vector translation = r2Collider_PositionWrtParent(collider_handle).translation;
assert(translation.x == 1.0 && translation.y == 2.0);
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)