rigid_body_velocity
The velocity of a dynamic rigid-body controls how fast it is moving in time. The velocity is applied at the center-of-mass of the rigid-body, and is composed of two independent parts:
- The linear velocity is specified as a vector representing the direction and magnitude of the movement.
- In 3D, the angular velocity is given as a vector representing the rotation
axis multiplied by the rotation angular speed in
rad/s(axis-angle representation). In 2D, the angular velocity is given as a real representing the angular speed inrad/s.
info
The velocity is only relevant to dynamic rigid-bodies. It has no effect on fixed rigid-bodies, and the velocity of kinematic rigid-bodies are automatically computed at each timestep based on their next kinematic positions.
The velocity of a rigid-body is automatically updated by the physics pipeline after taking forces, contacts, and joints into account. It can be set when the rigid-body is created or after its creation:
- Example 2D
- Example 3D
/* Set the velocities when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
// The linear velocity of this body.
// Default: zero velocity.
.linvel(Vector::new(1.0, 3.0))
// The angular velocity of this body.
// Default: zero velocity.
.angvel(3.0)
// All done, actually build the rigid-body.
.build();
/* Set the velocities after the rigid-body creation. */
let rigid_body = &mut world.bodies[rigid_body_handle];
// The `true` argument makes sure the rigid-body is awake.
rigid_body.set_linvel(Vector::new(1.0, 3.0), true);
rigid_body.set_angvel(3.0, true);
assert_eq!(rigid_body.linvel(), Vector::new(1.0, 3.0));
assert_eq!(rigid_body.angvel(), 3.0);
/* Set the velocities when the rigid-body is created. */
let rigid_body = RigidBodyBuilder::dynamic()
// The linear velocity of this body.
// Default: zero velocity.
.linvel(Vector::new(1.0, 3.0, 4.0))
// The angular velocity of this body.
// Default: zero velocity.
.angvel(Vector::new(3.0, 0.0, 0.0))
// All done, actually build the rigid-body.
.build();
/* Set the velocities after the rigid-body creation. */
let rigid_body = &mut world.bodies[rigid_body_handle];
// The `true` argument makes sure the rigid-body is awake.
rigid_body.set_linvel(Vector::new(1.0, 3.0, 4.0), true);
rigid_body.set_angvel(Vector::new(3.0, 0.0, 0.0), true);
assert_eq!(rigid_body.linvel(), Vector::new(1.0, 3.0, 4.0));
assert_eq!(rigid_body.angvel(), Vector::new(3.0, 0.0, 0.0));
- Example 2D
- Example 3D
/* Set the velocities when the rigid-body is created. */
commands.spawn(RigidBody::Dynamic).insert(Velocity {
linear: Vec2::new(0.0, 2.0),
angular: 0.4,
});
/* Set the velocities inside of a system. */
fn modify_body_velocity(mut velocities: Query<&mut Velocity>) {
for mut vel in velocities.iter_mut() {
vel.linear = Vec2::new(0.0, 2.0);
vel.angular = 0.4;
}
}
commands.spawn(RigidBody::Dynamic).insert(Velocity {
linear: Vec3::new(0.0, 2.0, 0.0),
angular: Vec3::new(0.2, 0.4, 0.8),
});
/* Set the velocities inside of a system. */
fn modify_body_velocity(mut velocities: Query<&mut Velocity>) {
for mut vel in velocities.iter_mut() {
vel.linear = Vec3::new(0.0, 2.0, 0.0);
vel.angular = Vec3::new(3.2, 0.4, 0.8);
}
}
- Example 2D
- Example 3D
/* Set the velocities when the rigid-body is created. */
let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic()
// The linear velocity of this body.
// Default: zero velocity.
.setLinvel(1.0, 3.0)
// The angular velocity of this body.
// Default: zero velocity.
.setAngvel(3.0);
let rigidBody = world.createRigidBody(rigidBodyDesc);
/* Set the velocities after the rigid-body creation. */
// The `true` argument makes sure the rigid-body is awake.
rigidBody.setLinvel({ x: 1.0, y: 3.0 }, true);
rigidBody.setAngvel(3.0, true);
/* Set the velocities when the rigid-body is created. */
let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic()
// The linear velocity of this body.
// Default: zero velocity.
.setLinvel(1.0, 3.0, 4.0)
// The angular velocity of this body.
// Default: zero velocity.
.setAngvel({ x: 3.0, y: 0.0, z: 0.0 });
let rigidBody = world.createRigidBody(rigidBodyDesc);
/* Set the velocities after the rigid-body creation. */
// The `true` argument makes sure the rigid-body is awake.
rigidBody.setLinvel({ x: 1.0, y: 3.0, z: 4.0 }, true);
rigidBody.setAngvel({ x: 3.0, y: 0.0, z: 0.0 }, true);
- Example 2D
- Example 3D
/* Set the velocities when the rigid-body is created. */
R2RigidBodyDesc rigid_body = r2DynamicRigidBodyDesc();
// The linear velocity of this body.
// Default: zero velocity.
rigid_body.linvel = r2Vector(1.0, 3.0);
// The angular velocity of this body.
// Default: zero velocity.
rigid_body.angvel = 3.0;
/* Set the velocities after the rigid-body creation. */
// The last `1` argument makes sure the rigid-body is awake.
r2RigidBody_SetLinvel(rigid_body_handle, r2Vector(1.0, 3.0), 1);
r2RigidBody_SetAngvel(rigid_body_handle, 3.0, 1);
R2Vector linvel = r2RigidBody_Linvel(rigid_body_handle);
assert(linvel.x == 1.0 && linvel.y == 3.0);
assert(r2RigidBody_Angvel(rigid_body_handle) == 3.0);
/* Set the velocities when the rigid-body is created. */
R3RigidBodyDesc rigid_body = r3DynamicRigidBodyDesc();
// The linear velocity of this body.
// Default: zero velocity.
rigid_body.linvel = r3Vector(1.0, 3.0, 4.0);
// The angular velocity of this body.
// Default: zero velocity.
rigid_body.angvel = r3Vector(3.0, 0.0, 0.0);
/* Set the velocities after the rigid-body creation. */
// The last `1` argument makes sure the rigid-body is awake.
r3RigidBody_SetLinvel(rigid_body_handle, r3Vector(1.0, 3.0, 4.0), 1);
r3RigidBody_SetAngvel(rigid_body_handle, r3Vector(3.0, 0.0, 0.0), 1);
R3Vector linvel = r3RigidBody_Linvel(rigid_body_handle);
R3AngVector angvel = r3RigidBody_Angvel(rigid_body_handle);
assert(linvel.x == 1.0 && linvel.y == 3.0 && linvel.z == 4.0);
assert(angvel.x == 3.0 && angvel.y == 0.0 && angvel.z == 0.0);
# Set the velocities when the rigid-body is created.
rigid_body = (
rp.RigidBody.dynamic()
# The linear velocity of this body.
# Default: zero velocity.
.linvel((1.0, 3.0, 4.0))
# The angular velocity of this body.
# Default: zero velocity.
.angvel((3.0, 0.0, 0.0))
# All done, actually build the rigid-body.
.build()
)
# Set the velocities after the rigid-body creation.
rigid_body = world.rigid_bodies[rigid_body_handle]
# Setting these properties automatically wakes the rigid-body up.
rigid_body.linvel = (1.0, 3.0, 4.0)
rigid_body.angvel = (3.0, 0.0, 0.0)
assert rigid_body.linvel == rp.Vec3(1.0, 3.0, 4.0)
assert rigid_body.angvel == rp.Vec3(3.0, 0.0, 0.0)
Alternatively, the velocity of a dynamic rigid-body can be altered indirectly by applying a force or an impulse.