collider_position
The position of a collider represents its location (translation) in 2D or 3D world-space as well as its orientation (rotation).
Transform component.Pose type).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).Vec3) and its rotational part as an unit quaternion (Rotation3). Both are combined into a pose (the Isometry3 type).
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:
- Example 2D
- Example 3D
/* 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. */
let collider = ColliderBuilder::ball(0.5)
.translation(Vector::new(1.0, 2.0, 3.0))
.rotation(Vector::new(0.1, 0.2, 0.4))
// Set both translation and rotation at once.
.position(Pose::new(
Vector::new(1.0, 2.0, 3.0),
Vector::new(0.1, 0.2, 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, 3.0));
collider.set_rotation(Rotation::from_scaled_axis(Vector::new(0.1, 0.2, 0.4)));
// Set both the translation and rotation at once.
collider.set_position(Pose::new(
Vector::new(1.0, 2.0, 3.0),
Vector::new(0.1, 0.2, 0.4),
));
assert_eq!(collider.translation(), Vector::new(1.0, 2.0, 3.0));
assert_eq!(
collider.rotation().to_scaled_axis(),
Vector::new(0.1, 0.2, 0.4)
);
- Example 2D
- Example 3D
/* 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. */
commands
.spawn(Collider::cuboid(0.5, 0.5, 0.5))
.insert(Transform::from_xyz(1.0, 2.0, 3.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;
}
}
- Example 2D
- Example 3D
/* 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. */
let colliderDesc = RAPIER.ColliderDesc.ball(0.5)
.setTranslation(1.0, 2.0, 3.0)
.setRotation({ w: 1.0, x: 0.0, y: 0.0, z: 0.0 });
let collider = world.createCollider(colliderDesc);
/* Set the collider position after the collider creation. */
collider.setTranslation({ x: 1.0, y: 2.0, z: 3.0 });
collider.setRotation({ w: 1.0, x: 0.0, y: 0.0, z: 0.0 });
- Example 2D
- Example 3D
/* 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. */
R3ColliderDesc collider = r3BallColliderDesc(0.5);
collider.position.translation = r3Vector(1.0, 2.0, 3.0);
collider.position.rotation = r3RotationFromAxisAngle(r3Vector(0.0, 0.0, 1.0), 0.4);
// Set both translation and rotation at once.
collider.position =
r3Pose(r3Vector(1.0, 2.0, 3.0), r3RotationFromAxisAngle(r3Vector(0.0, 0.0, 1.0), 0.4));
/* Set the collider position after the collider creation. */
R3Rotation rotation = r3RotationFromAxisAngle(r3Vector(0.0, 0.0, 1.0), 0.4);
r3Collider_SetTranslation(collider_handle, r3Vector(1.0, 2.0, 3.0));
r3Collider_SetRotation(collider_handle, rotation);
// Set both the translation and rotation at once.
r3Collider_SetPosition(collider_handle, r3Pose(r3Vector(1.0, 2.0, 3.0), rotation));
R3Vector translation = r3Collider_Translation(collider_handle);
assert(translation.x == 1.0 && translation.y == 2.0 && translation.z == 3.0);
# 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.
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):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: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):- Example 2D
- Example 3D
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)
);
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, 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.
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, 3.0));
assert_eq!(
collider.position_wrt_parent().unwrap().translation,
Vector::new(1.0, 2.0, 3.0)
);
- Example 2D
- Example 3D
// 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));
});
// 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)
.with_children(|children| {
children
.spawn(Collider::cuboid(0.5, 0.5, 0.5))
.insert(Transform::from_xyz(1.0, 2.0, 0.0));
});
- Example 2D
- Example 3D
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 });
let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic();
let rigidBody = world.createRigidBody(rigidBodyDesc);
let colliderDesc = RAPIER.ColliderDesc.ball(0.5)
.setTranslation(1.0, 2.0, 3.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, z: 3.0 });
- Example 2D
- Example 3D
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);
R3RigidBodyDesc rigid_body = r3DynamicRigidBodyDesc();
R3RigidBodyHandle rigid_body_handle = r3InsertRigidBody(world, &rigid_body);
R3ColliderDesc collider = r3BallColliderDesc(0.5);
collider.position.translation = r3Vector(1.0, 2.0, 3.0);
// Attach the collider to the rigid-body. The description's position is
// the collider's position wrt. the rigid-body.
R3ColliderHandle collider_handle = r3InsertCollider(rigid_body_handle, &collider);
/* Set the collider position wrt. its parent after the collider creation. */
r3Collider_SetPositionWrtParent(collider_handle, r3TranslationPose(r3Vector(1.0, 2.0, 3.0)));
R3Vector translation = r3Collider_PositionWrtParent(collider_handle).translation;
assert(translation.x == 1.0 && translation.y == 2.0 && translation.z == 3.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)