Introduction
Rapier is a set of 2D and 3D physics simulation libraries written in Rust with first-class WebAssembly support. It provides rigid body dynamics, collision detection, and joint constraints suitable for games, robotics simulations, and interactive applications.
What Rapier Does
- Simulates rigid body dynamics with gravity, forces, and impulses
- Detects collisions between convex shapes, trimeshes, and heightfields
- Supports joints and constraints including revolute, prismatic, and fixed types
- Provides continuous collision detection (CCD) to prevent tunneling
- Exposes a deterministic simulation mode for lockstep multiplayer
Architecture Overview
Rapier is built on the nalgebra linear algebra library and uses an island-based constraint solver with SIMD acceleration. The collision detection pipeline implements a broad-phase sweep-and-prune algorithm followed by narrow-phase GJK/EPA queries. The engine compiles natively to Rust targets and to WebAssembly via wasm-bindgen, sharing the same core simulation code across platforms.
Self-Hosting & Configuration
- Add
rapier2dorrapier3das a Cargo dependency for Rust projects - Use the
@dimforge/rapier2dorrapier3dnpm packages for JavaScript/WASM - Configure gravity, timestep, and solver iterations via
IntegrationParameters - Enable CCD on fast-moving bodies to prevent objects passing through walls
- Use debug rendering with Bevy or other engines via the
rapier-debug-renderfeature
Key Features
- Cross-platform via native Rust and WebAssembly compilation
- Deterministic simulation for reproducible physics across clients
- Bevy integration through the
bevy_rapierplugin for game development - Event system for collision start/stop and contact force reporting
- Serialization support for saving and restoring simulation state
Comparison with Similar Tools
- Box2D — mature 2D engine but C++ only, no built-in Rust or WASM support
- Bullet — feature-rich 3D engine but heavier and more complex API
- PhysX — high-performance but proprietary NVIDIA SDK
- Cannon.js — JavaScript-native physics but slower than WASM-based Rapier
- Avian (formerly heron) — Bevy-specific wrapper but Rapier powers it underneath
FAQ
Q: Can Rapier handle soft bodies or fluids? A: No. Rapier focuses on rigid body simulation. For soft bodies, consider complementary libraries.
Q: Is the WASM build slower than native Rust? A: WASM performance is typically 1.5-3x slower than native, but still fast enough for real-time game physics in the browser.
Q: Does Rapier support multithreading?
A: Yes. The parallel feature enables rayon-based multithreaded simulation on native targets.
Q: What game engines integrate with Rapier? A: Bevy (via bevy_rapier), Godot (via godot-rapier), and any engine that can call Rust or WASM code.