collider_creation_and_insertion
A collider is created by a ColliderBuilder
structure that is based on the builder pattern. Then it needs
to be inserted into the ColliderSet
that will be processed by the physics-pipeline, collision-pipeline, or
query-pipeline.
info
The following example shows several setters that can be called to customize the collider being built. The input values are just random so using this example as-is will not lead to a useful result.
- Example 2D
- Example 3D
use rapier2d::prelude::*;
use std::f32::consts::PI;
// The set that will contain our colliders.
let mut collider_set = ColliderSet::new();
// Builder for a ball-shaped collider.
let _ = ColliderBuilder::ball(0.5);
// Builder for a cuboid-shaped collider.
let _ = ColliderBuilder::cuboid(0.5, 0.2);
// Builder for a capsule-shaped collider. The capsule principal axis is the `x` coordinate axis.
let _ = ColliderBuilder::capsule_x(0.5, 0.2);
// Builder for a capsule-shaped collider. The capsule principal axis is the `y` coordinate axis.
let _ = ColliderBuilder::capsule_y(0.5, 0.2);
// Builder for a triangle-mesh-shaped collider.
let _ = ColliderBuilder::trimesh(vertices, indices);
// Builder for a heightfield-shaped collider.
let _ = ColliderBuilder::heightfield(heights, scale);
// Builder for a collider with the given shape.
let collider = ColliderBuilder::new(SharedShape::ball(0.5))
// The collider translation wrt. the body it is attached to.
// Default: the zero vector.
.translation(vector![1.0, 2.0])
// The collider rotation wrt. the body it is attached to.
// Default: the identity rotation.
.rotation(PI)
// The collider position wrt. the body it is attached to.
// Default: the identity isometry.
.position(Isometry::new(vector![1.0, 2.0], PI))
// The collider density. If non-zero the collider's mass and angular inertia will be added
// to the inertial properties of the body it is attached to.
// Default: 1.0
.density(1.3)
// The friction coefficient of this collider.
// Default: ColliderBuilder::default_friction() == 0.5
.friction(0.8)
// Whether this collider is a sensor.
// Default: false
.sensor(true)
// All done, actually build the collider.
.build();
// Insert the collider into the set, without attaching it to a rigid-body.
let collider_handle = collider_set.insert(collider.clone());
let mut rigid_body_set = RigidBodySet::new();
let rigid_body_handle = rigid_body_set.insert(RigidBodyBuilder::dynamic().build());
// Or insert the collider into the set and attach it to a rigid-body.
let handle = collider_set.insert_with_parent(collider, rigid_body_handle, &mut rigid_body_set);
use rapier3d::prelude::*;
use std::f32::consts::PI;
// The set that will contain our colliders.
let mut collider_set = ColliderSet::new();
// Builder for a ball-shaped collider.
let _ = ColliderBuilder::ball(0.5);
// Builder for a cuboid-shaped collider.
let _ = ColliderBuilder::cuboid(0.5, 0.2, 0.1);
// Builder for a capsule-shaped collider. The capsule principal axis is the `x` coordinate axis.
let _ = ColliderBuilder::capsule_x(0.5, 0.2);
// Builder for a capsule-shaped collider. The capsule principal axis is the `y` coordinate axis.
let _ = ColliderBuilder::capsule_y(0.5, 0.2);
// Builder for a capsule-shaped collider. The capsule principal axis is the `z` coordinate axis.
let _ = ColliderBuilder::capsule_z(0.5, 0.2);
// Builder for a triangle-mesh-shaped collider.
let _ = ColliderBuilder::trimesh(vertices, indices);
// Builder for a heightfield-shaped collider.
let _ = ColliderBuilder::heightfield(heights, scale);
// Builder for a collider with the given shape.
let collider = ColliderBuilder::new(SharedShape::ball(0.5))
// The collider translation wrt. the body it is attached to.
// Default: the zero vector.
.translation(vector![1.0, 2.0, 3.0])
// The collider rotation wrt. the body it is attached to.
// Default: the identity rotation.
.rotation(vector![0.0, PI, 0.0])
// The collider position wrt. the body it is attached to.
// Default: the identity isometry.
.position(Isometry::new(vector![1.0, 2.0, 3.0], vector![0.0, PI, 0.0]))
// The collider density. If non-zero the collider's mass and angular inertia will be added
// to the inertial properties of the body it is attached to.
// Default: 1.0
.density(1.3)
// The friction coefficient of this collider.
// Default: ColliderBuilder::default_friction() == 0.5
.friction(0.8)
// Whether this collider is a sensor.
// Default: false
.sensor(true)
// All done, actually build the collider.
.build();
// Insert the collider into the set, without attaching to a rigid-body.
let handle = collider_set.insert(collider.clone());
let rigid_body_handle = rigid_body_set.insert(RigidBodyBuilder::dynamic().build());
// Or insert the collider into the set and attach it to a rigid-body.
let handle = collider_set.insert_with_parent(collider, rigid_body_handle, &mut rigid_body_set);