collider_mass_properties
The mass properties of a rigid-body is computed as the sum of the mass-properties manually set by the user for the rigid-body, plus the mass-properties of the colliders attached to it. There are two ways to define the mass-properties of a collider:
- The easiest, automatic, way: by giving the collider a non-zero density (the default density is 1.0) or a non-zero mass. This will make sure the other mass-properties like the angular inertia tensor are computed automatically from the collider's shape.
- The manual way: by giving an explicit mass and angular inertia to the collider.
It is recommended to use the density-based or mass-based approaches as it will ensure the automatically-computed mass-properties are coherent with the geometric shape. Wrong mass-properties (especially the angular inertia part and center-of-mass location) may lead to odd behaviors. The manual approach is usually useful when modeling real-world objects for which you already know the real-world mass, center-of-mass, and angular inertia tensor.
The mass-properties of a collider can only be set when the collider is created:
- Example 2D
- Example 3D
let rigid_body = RigidBodyBuilder::dynamic().build();
let rigid_body_handle = rigid_body_set.insert(rigid_body);
// First option: by setting the density of the collider (or we could just leave
// its default value 1.0).
let collider = ColliderBuilder::cuboid(1.0, 2.0).density(2.0).build();
// Second option: by setting the mass of the collider.
let collider = ColliderBuilder::cuboid(1.0, 2.0).mass(0.8).build();
// Third option: by setting the mass-properties explicitly.
let collider = ColliderBuilder::cuboid(1.0, 2.0)
.mass_properties(MassProperties::new(point![0.0, 1.0], 0.5, 0.3))
.build();
// When the collider is attached, the rigid-body's mass and angular
// inertia is automatically updated to take the collider into account.
collider_set.insert_with_parent(collider, rigid_body_handle, &mut rigid_body_set);
let rigid_body = RigidBodyBuilder::dynamic().build();
let rigid_body_handle = rigid_body_set.insert(rigid_body);
// First option: by setting the density of the collider (or we could just leave
// its default value 1.0).
let collider = ColliderBuilder::cuboid(1.0, 2.0, 3.0).density(2.0).build();
// Second option: by setting the mass of the collider.
let collider = ColliderBuilder::cuboid(1.0, 2.0, 3.0).mass(0.8).build();
// Third option: by setting the mass-properties explicitly.
let collider = ColliderBuilder::cuboid(1.0, 2.0, 3.0)
.mass_properties(MassProperties::new(
point![0.0, 1.0, 0.0],
0.5,
vector![0.3, 0.2, 0.1],
))
.build();
// When the collider is attached, the rigid-body's mass and angular
// inertia is automatically updated to take the collider into account.
let collider_handle =
collider_set.insert_with_parent(collider, rigid_body_handle, &mut rigid_body_set);
- Example 2D
- Example 3D
// First option: by setting the density of the collider (or we could just leave
// its default value 1.0).
let collider_mprops = ColliderMassProperties::Density(2.0);
// Second option: by setting the mass of the collider.
let collider_mprops = ColliderMassProperties::Mass(0.8);
// Third option: by setting the mass-properties explicitly.
let collider_mprops = ColliderMassProperties::MassProperties(MassProperties {
local_center_of_mass: Vec2::new(0.0, 1.0),
mass: 0.5,
principal_inertia: 0.3,
});
// When the collider is attached, the rigid-body's mass and angular
// inertia will be automatically updated to take the collider into account.
commands
.spawn(RigidBody::Dynamic)
.insert(Collider::ball(0.5))
.insert(collider_mprops);
// First option: by setting the density of the collider (or we could just leave
// its default value 1.0).
let collider_mprops = ColliderMassProperties::Density(2.0);
// Second option: by setting the mass of the collider.
let collider_mprops = ColliderMassProperties::Mass(0.8);
// Third option: by setting the mass-properties explicitly.
let collider_mprops = ColliderMassProperties::MassProperties(MassProperties {
local_center_of_mass: Vec3::new(0.0, 1.0, 2.0),
mass: 0.5,
principal_inertia_local_frame: Quat::IDENTITY,
principal_inertia: Vec3::new(0.3, 0.4, 0.5),
});
// When the collider is attached, the rigid-body's mass and angular
// inertia will be automatically updated to take the collider into account.
commands
.spawn(RigidBody::Dynamic)
.insert(Collider::ball(0.5))
.insert(collider_mprops);
- Example 2D
- Example 3D
let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic();
let rigidBody = world.createRigidBody(rigidBodyDesc);
// First option: by setting the density of the collider (or we could just leave
// its default value 1.0).
let colliderDesc = RAPIER.ColliderDesc.cuboid(1.0, 2.0)
.setDensity(2.0);
// Second option: by setting the mass of the collider.
let colliderDesc2 = RAPIER.ColliderDesc.cuboid(1.0, 2.0)
.setMass(0.8);
// Third option: by setting the mass-properties explicitly.
let colliderDesc3 = RAPIER.ColliderDesc.cuboid(1.0, 2.0)
.setMassProperties(0.5, { x: 0.0, y: 1.0 }, 0.3);
// When the collider is attached, the rigid-body's mass and angular
// inertia is automatically updated to take the collider into account.
let collider = world.createCollider(colliderDesc, rigidBody);
let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic();
let rigidBody = world.createRigidBody(rigidBodyDesc);
// First option: by setting the density of the collider (or we could just leave
// its default value 1.0).
let colliderDesc = RAPIER.ColliderDesc.cuboid(1.0, 2.0, 1.0)
.setDensity(2.0);
// Second option: by setting the mass of the collider.
let colliderDesc2 = RAPIER.ColliderDesc.cuboid(1.0, 2.0, 1.0)
.setMass(0.8);
// Third option: by setting the mass-properties explicitly.
let colliderDesc3 = RAPIER.ColliderDesc.cuboid(1.0, 2.0, 1.0)
.setMassProperties(0.5, { x: 0.0, y: 1.0, z: 0.0 }, { x: 0.3, y: 0.2, z: 0.1 }, { w: 1.0, x: 0.0, y: 0.0, z: 0.0 });
// When the collider is attached, the rigid-body's mass and angular
// inertia is automatically updated to take the collider into account.
let collider = world.createCollider(colliderDesc, rigidBody);