Top Related Projects
Quick Overview
Rapier is a fast and feature-rich 2D and 3D physics engine for Rust. It provides a robust simulation environment for rigid-body dynamics, collision detection, and various constraints, making it suitable for game development, robotics simulations, and other physics-based applications.
Pros
- High performance and efficient simulations
- Cross-platform support (including WebAssembly)
- Comprehensive feature set for both 2D and 3D physics
- Active development and community support
Cons
- Steeper learning curve compared to some other physics engines
- Documentation could be more extensive for advanced features
- Limited built-in rendering capabilities (focuses on physics simulation)
Code Examples
- Creating a rigid body:
use rapier3d::prelude::*;
let rigid_body = RigidBodyBuilder::dynamic()
.translation(vector![0.0, 10.0, 0.0])
.build();
- Adding a collider to a rigid body:
use rapier3d::prelude::*;
let collider = ColliderBuilder::cuboid(1.0, 1.0, 1.0).build();
physics_world.add_collider(collider, rigid_body_handle);
- Applying a force to a rigid body:
use rapier3d::prelude::*;
let force = vector![0.0, 100.0, 0.0];
rigid_body.apply_force(force, true);
- Performing a ray cast:
use rapier3d::prelude::*;
let ray = Ray::new(point![0.0, 0.0, 0.0], vector![1.0, 0.0, 0.0]);
let max_toi = 10.0;
let solid = true;
if let Some((handle, intersection)) = query_pipeline.cast_ray(
&collider_set,
&ray,
max_toi,
solid,
QueryFilter::default(),
) {
println!("Hit object at distance: {}", intersection.toi);
}
Getting Started
To use Rapier in your Rust project, add the following to your Cargo.toml
:
[dependencies]
rapier2d = "0.17.2" # For 2D physics
rapier3d = "0.17.2" # For 3D physics
Then, in your Rust code:
use rapier3d::prelude::*;
fn main() {
let mut rigid_body_set = RigidBodySet::new();
let mut collider_set = ColliderSet::new();
let gravity = vector![0.0, -9.81, 0.0];
let integration_parameters = IntegrationParameters::default();
let mut physics_pipeline = PhysicsPipeline::new();
let mut island_manager = IslandManager::new();
let mut broad_phase = BroadPhase::new();
let mut narrow_phase = NarrowPhase::new();
let mut impulse_joint_set = ImpulseJointSet::new();
let mut multibody_joint_set = MultibodyJointSet::new();
let mut ccd_solver = CCDSolver::new();
// Your physics simulation code here
}
Competitor Comparisons
Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.
Pros of Bullet3
- More mature and widely adopted in industry, with extensive documentation and community support
- Supports a broader range of features, including soft body dynamics and fluid simulation
- Cross-platform compatibility, including support for various game engines and 3D modeling software
Cons of Bullet3
- Written in C++, which may be less accessible for developers primarily working with higher-level languages
- Can be more complex to set up and integrate, especially for simpler projects
- Performance may be slower for certain use cases compared to more specialized libraries
Code Comparison
Bullet3:
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
Rapier:
let mut physics_world = rapier3d::dynamics::World::new();
let collider = ColliderBuilder::cuboid(0.5, 0.5, 0.5).build();
let rigid_body = RigidBodyBuilder::new_dynamic().translation(0.0, 10.0, 0.0).build();
physics_world.add_rigid_body(rigid_body);
physics_world.add_collider(collider, rigid_body_handle);
Box2D is a 2D physics engine for games
Pros of Box2D
- Mature and widely adopted physics engine with a large community and extensive documentation
- Lightweight and efficient, suitable for resource-constrained environments
- Supports a wide range of programming languages through bindings
Cons of Box2D
- Written in C++, which may not be ideal for all projects or developers
- Less frequent updates and slower development pace compared to Rapier
- Limited to 2D physics simulations
Code Comparison
Box2D (C++):
b2Vec2 gravity(0.0f, -10.0f);
b2World world(gravity);
b2BodyDef groundBodyDef;
b2Body* groundBody = world.CreateBody(&groundBodyDef);
Rapier (Rust):
let gravity = Vector2::new(0.0, -9.81);
let mut physics_world = World::new();
let collider = ColliderBuilder::cuboid(100.0, 0.1).build();
physics_world.add_collider(collider, BodyHandle::ground());
Both libraries provide similar functionality for creating a physics world and adding bodies. Box2D uses a more verbose C++ syntax, while Rapier leverages Rust's modern language features for a cleaner API. Rapier's code is generally more concise and type-safe, but Box2D's API may be more familiar to developers with C++ experience.
NVIDIA PhysX SDK
Pros of PhysX
- Mature and widely adopted in the game industry
- Extensive documentation and community support
- Optimized for NVIDIA GPUs, offering hardware acceleration
Cons of PhysX
- Closed-source and proprietary, limiting customization
- Primarily C++ focused, which may not suit all developers
- Can be complex to integrate and use for smaller projects
Code Comparison
PhysX (C++):
PxRigidDynamic* dynamicActor = physics.createRigidDynamic(PxTransform(0,0,0));
PxRigidStatic* staticActor = physics.createRigidStatic(PxTransform(0,0,0));
PxShape* shape = physics.createShape(PxSphereGeometry(1.0f), *material);
dynamicActor->attachShape(*shape);
scene->addActor(*dynamicActor);
Rapier (Rust):
let rigid_body = RigidBodyBuilder::dynamic()
.translation(0.0, 0.0, 0.0)
.build();
let collider = ColliderBuilder::ball(1.0).build();
world.add_rigid_body(rigid_body);
world.add_collider(collider, rigid_body_handle);
PhysX offers a more verbose API with explicit actor and shape creation, while Rapier provides a more concise, builder-pattern approach. PhysX's C++ implementation may be more familiar to game developers, whereas Rapier's Rust code emphasizes safety and modern language features.
2D physics engine for games
Pros of LiquidFun
- Specialized in fluid and soft body simulation, offering unique features for game developers
- Backed by Google, potentially providing more resources and long-term support
- Includes a testbed for easy visualization and experimentation
Cons of LiquidFun
- Less active development and updates compared to Rapier
- Limited to 2D physics simulations
- C++ only, while Rapier supports multiple languages including Rust and JavaScript
Code Comparison
LiquidFun (C++):
b2World world(gravity);
b2BodyDef bodyDef;
b2Body* body = world.CreateBody(&bodyDef);
b2CircleShape circle;
body->CreateFixture(&circle, 1.0f);
Rapier (Rust):
let mut world = World::new();
let collider = ColliderBuilder::ball(0.5).build();
let rigid_body = RigidBodyBuilder::dynamic().build();
world.add_rigid_body(rigid_body);
world.add_collider(collider, rigid_body_handle);
Both libraries offer straightforward APIs for creating physics worlds and bodies. LiquidFun uses a more C-style approach with pointers, while Rapier leverages Rust's ownership model for a safer API. Rapier's code is generally more concise and type-safe compared to LiquidFun's C++ implementation.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
2D and 3D physics engines for the Rust programming language.
What is Rapier?
Rapier is a set of 2D and 3D physics engines for games, animation, and robotics. These crates
are rapier2d
, rapier3d
, rapier2d-f64
, and rapier3d-f64
. They are written with the Rust
programming language, by the Dimforge organization. It is forever free
and open-source!
Getting started
The easiest way to get started with Rapier is to:
- Read the user-guides.
- Play with the examples:
cargo run --release --bin all_examples2
andcargo run --release --bin all_examples3
. Their source code are available on theexamples2d/
andexamples3d/
directory. - Don't hesitate to ask for help on Discord, or by opening an issue on GitHub.
Resources and discussions
- Dimforge: See all the open-source projects we are working on! Follow our announcements on our blog.
- User guide: Learn to use Rapier in your project by reading the official User Guides.
- Discord: Come chat with us, get help, suggest features, on Discord!
- NPM packages: Check out our NPM packages for Rapier, if you need to use it with JavaScript/Typescript.
Please make sure to familiarize yourself with our Code of Conduct and our Contribution Guidelines before contributing or participating in discussions with the community.
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot