The Rapier testbed
The Rapier testbed is a small physics sandbox based on the kiss3d renderer. It is designed to easily open a window and render a physics world. It provides basic controls like play/pause, grabbing objects with the mouse, changing simulation settings, etc. This is the application all the demos of Rapier are written with, and it is generally the quickest way of prototyping a scene, or of reproducing a problem before opening an issue.
The testbed is published as the rapier_testbed2d and rapier_testbed3d crates, which share the same version as Rapier
itself.
Example
A scene is one function building a PhysicsWorld, giving it to the viewer, and then stepping it from the rendering
loop. Note that the loop is what owns the simulation: the viewer only draws the state of the world it is given, and
tells whether the user asked for the simulation to keep running or to stop:
async fn bouncing_ball(viewer: &mut TestbedViewer) {
// The scene itself, built like in any other application.
let mut world = PhysicsWorld::new();
world.insert_collider(ColliderBuilder::cuboid(100.0, 0.1, 100.0), None);
world.insert(
RigidBodyBuilder::dynamic().translation(Vector::new(0.0, 10.0, 0.0)),
ColliderBuilder::ball(0.5).restitution(0.7),
);
// Hand the world to the viewer, and place the camera.
viewer.set_world(&mut world);
viewer.look_at(Vec3::new(10.0, 10.0, 10.0), Vec3::ZERO);
// The rendering loop: it ends when the user closes the window or selects another scene.
while viewer.render_frame(&mut world).await {
if viewer.simulating() {
world.step();
}
}
}
The testbed itself is started from the main function of the application, which lists the scenes it can switch
between, and runs the one to display. Note that this function relies on the kiss3d::main attribute, therefore
kiss3d must be a dependency of the application as well:
#[kiss3d::main]
async fn main() {
// The scenes listed in the side panel of the testbed, as (group, name) pairs.
let entries = vec![ExampleEntry::new("Demos", "Bouncing ball")];
let mut viewer = TestbedViewer::new(entries).await;
bouncing_ball(&mut viewer).await;
}
Running the demos
The examples of Rapier are all combined in a single application listing them in a side panel. Running them is the fastest way to get an overview of Rapier’s features and performances.
git clone https://github.com/dimforge/rapier
cd rapier
cargo run --release --bin all_examples2 --features parallel # 2D demos
cargo run --release --bin all_examples3 --features parallel # 3D demos
Always run the testbed (and Rapier in general) in release mode: a debug build is up to 100 times slower.
Parallelism is enabled with --features parallel.