Rust Game Physics Engines: PhysX, Rapier, XPBD & Others
Tags: rust, gamedev, bevy
Published: 2024-04-03 09:14:35

---
title: "Rust Game Physics Engines: PhysX, Rapier, XPBD & Others"
published: "true"
description: "Rust Game Physics Engines ♚ a look at choices for Rust game development 2D & 3D physics engines including PhysX, Rapier and XPBD 🎱."
tags: "rust, gamedev, bevy"
canonical_url: "https://rodneylab.com/rust-game-physics-engines/"
cover_image: "https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obpbprj99nquasrvu1el.png"
---

## 🦀 Rust Game Dev Physics Engines

Continuing the series, in this post, we see the choices for Rust game physics engines.  As with an ECS and, to some extent, game engine, not every game will need a physics engine.  We approach the post from the perspective that you have already decided you want a physics engine for your project, and are evaluating which physics engine might be a good fit for your game.

We focus on engines that are still maintained, and enjoy at least moderate use.  Also, if you are curious about how physics engines work, we also see a tutorial, which you might follow to learn what is happening under the engine casing.  If this is what you are looking for then let&rsquo;s press on!
</div>

## 🤔 Rust Game Engine Choices

### Bevy

Bevy has no official physics engine, though some of the physics engines we look at have Bevy-specific versions.  Others were designed for use with Bevy, but work with other game engines.  Bevy XPBD, for example, is designed for use with Bevy, though you can use it with other game engines, much like <a href="https://rodneylab.com/rust-entity-component-systems/#rust-ecs-bevy-ecs">you can use the Bevy ECS with other game engines</a>.

### Other Game Engines

Some other Rust game engines ship with their own physics engine.  <a href="https://fyrox.rs/">Fyrox, for example, has advanced 2D/3D physics</a>, supporting rigid bodies, joints, ray casting and more.  <a href="http://localhost:3001/rust-for-gaming/#rust-for-gaming-using-godots-gdextension-">Godot too, which has community-led Rust bindings</a> also has an in-built physics engine as well as a <a href="https://github.com/godot-jolt/godot-jolt">Godot-native extension using the Jolt physics engine</a>. In fact, which is reported to be more performant than the official physics engine.

![Rust Game Physics Engines: screen capture shows a shower of squares in a 2D animation falling from an origin top and centre of the window.  The squares have uniform rotation and stack on a flat surface, below the source.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0wu0gf1jtnngbqyv1rp.png)

## 🌎 Resources for Learning more about Rust Game Dev Engines

Here are some of the resources I used to research this post.  Hopefully you will find them useful for more background.

- <a href="https://www.youtube.com/watch?v=-RY98R-qd1k&pp=ygUQYmV2eSBnYW1lIG1lZXR1cA%3D%3D">Joona Aalto talk at Bevy Game Dev Meetup</a> The State of Physics in Bevy
- <a href="https://johanhelsing.studio/posts/bevy-xpbd">Johan Helsing Bevy Physics Engine Tutorial</a> this tutorial is thoughtfully developed with excellent explanations of decisions made.  It is based on an older version of Bevy, and you will need a little work to have to work with the latest Bevy version.
- <a href="https://www.youtube.com/watch?v=oPuSvdBGrpE">10 Minute Physics</a> General Introduction to Game Physics by Matthias Müller, a Researcher at NVIDIA.  The series has JavaScript demos, which you can run in a web browser.  There is a <a href="https://www.youtube.com/watch?v=jrociOAYqxA">gentle introduction to Extended Position-Based Dynamic</a> (**XPBD**) in the Getting Ready to Simulate the world with XPBD (Simple and Powerful Simulations with XPBD) video.
- <a href="https://matthias-research.github.io/pages/publications/XPBD.pdf">Research paper on XPBD</a>, from NVIDIA.

## Rust Physics Engine Libraries
Here, we look at game development physics engines for use with Rust.

### Rust Game Physics Engine: Bevy XPBD
**2D & 3D XPBD-based physics engine written for Bevy**

This engine was created by extending the Johan Helsing tutorial mentioned above.  XPBD is a different approach to the impulse and velocity-based solvers used in other engines.  XPBD can produce stable simulations, though the algorithm design can lead to issues modelling material that deform.  The XPBD algorithm has substeps, with multiple iterations of the algorithm within a frame.  This substep technique has been combined successfully with impulse-based solvers in other engines.

Bevy XPBD has a modular design, letting you swap out the default collision system for <a href="https://docs.omniverse.nvidia.com/extensions/latest/ext_physics/rigid-bodies.html#physx-short-sdf-collision">Signed Distance fields</a>, for example.

<a href="https://github.com/Jondolf/bevy_xpbd/issues/346">Bevy XPBD will move away from the XPBD solver in coming months</a>, which is worth bearing in mind if you are considering using it for your project.

#### Bevy XPBD Features
- Collision detection using Parry;
- constraints and joints;
- ray casting and shape casting; and
- a modular design.

#### Not currently included
At the time of writing,  the following features have not yet been integrated: continuous collision detection (CCD), articulations (multi-body joints), soft bodies (cloth); and fluid simulation.

#### Learn more about Bevy XPBD here
- GitHub repo: <a href="https://github.com/Jondolf/bevy_xpbd">Jondolf/bevy_xpbd</a>
- Rust crates: <a href="https://docs.rs/bevy_xpbd_2d/latest/bevy_xpbd_2d/">bevy_xpbd_2d</a> & <a href="https://docs.rs/bevy_xpbd_3d/latest/bevy_xpbd_3d/">bevy_xpbd_3d</a>
- Code examples: <a href="https://github.com/Jondolf/bevy_xpbd/blob/main/crates/bevy_xpbd_2d/examples">Bevy XPBD 2D Examples</a>, <a href="https://github.com/Jondolf/bevy_xpbd/blob/main/crates/bevy_xpbd_3d/examples">Bevy XPBD 3D Examples</a>


### 🎮 Rust Game Physics Engine: physx

**Rust binding for NVIDIA PhysX**

PhysX is a mature physics engine used in Unreal Engine and Unity 3D, written in C++.  Embark Studios originally wrote this Rust wrapper to bring the maturity (more than 20 years old) offered by PhysX to Rust games.

PhysX is fully featured and has been battle tested many times in released games over the years.  Both PhysX and physx are open-source.

#### Learn more about physx here

- GitHub repo: <a href="https://github.com/EmbarkStudios/physx-rs">EmbarkStudios/physx-rs</a>
- Rust crate <a href="https://docs.rs/physx/latest/physx/">physx</a>
- Code example: <a href="https://github.com/EmbarkStudios/physx-rs/blob/main/physx/examples/ball_physx.rs">physx ball physics example</a>
- NVIDIA PhysX GitHub repo: <a href="https://github.com/NVIDIA-Omniverse/PhysX">NVIDIA-Omniverse/PhysX</a>

### 🎮 Rust Game Physics Engine: rapier

**2D and 3D physics engine for Rust**

Rapier supercedes the earlier **nphysics** engine and is the most mature pure-Rust offering. It is fully cross-platform with web support.  There are 2D, 3D and f32 and high precision f64 crates, which are all free and open-source.

#### rapier Features

- Web platform support;
- joint constraints; and
- collisions and intersections.

For fluid simulation see Salva (below), also from the Dimforge organization, which couples with rapier.

**Learn more about rapier here**:
- GitHub repo: <a href="https://github.com/dimforge/rapier">dimforge/rapier</a>
- Rust crates <a href="https://docs.rs/rapier2d/latest/rapier2d/">rapier2d</a>, <a href="https://docs.rs/rapier2d-f64/latest/rapier2d_f64/">rapier2d_f64</a>, <a href="https://docs.rs/rapier3d/latest/rapier3d/">rapier3d</a> & <a href="https://docs.rs/rapier3d-f64/latest/rapier3d_f64/">rapier3d_f64</a>.
- User Guide: <a href="https://www.rapier.rs/docs/">rapier User Guide</a>
- Getting Started: <a href="https://rapier.rs/docs/user_guides/rust/getting_started">Getting started with rapier Rust</a>
- Code examples: <a href="https://github.com/dimforge/rapier">see examples2d, examples3d-f64 and examples3d directories</a>

### 🎮 Rust Game Physics Engine: Salva

**2D & 3D particle-based fluid simulation written in Rust**

Salva offers 2D and 3D fluid simulation and has WASM support.

#### Salva Features

- Multiphase fluids;
- two-way coupling with Rapier; and
- multiple models.

#### Learn more about Salva here

- GitHub repo: <a href="https://github.com/dimforge/salva">dimforge/salva</a>
- Rust crates <a href="https://docs.rs/salva2d/latest/salva2d/">salva2d</a> & <a href="https://docs.rs/salva3d/latest/salva3d/">salva3d</a>.
- Documentation: <a href="https://salva.rs/docs/">Salva docs</a>
- Code examples: <a href="https://github.com/dimforge/salva/tree/master/examples2d">2D examples</a> <a href="https://github.com/dimforge/salva/tree/master/examples3d">3D examples</a>

### 🎮 Rust Game Physics Engine: Wrapped2D

**Rust binding for the Box2D library**

The Box2D engine is the 2D physics engine used in Unity, and was also used in earlier versions of Godot.  It is written in C++.  Wrapped2D offers a Rust binding for Box2D.

**Learn more about wrapped2d here**:
- GitHub repo: <a href="https://github.com/Bastacyclop/rust_box2d">Bastacyclop/rust_box2d</a>
- Rust crate <a href="https://docs.rs/wrapped2d/latest/wrapped2d/">wrapped2d</a>.
- Code examples: <a href="https://github.com/Bastacyclop/rust_box2d/tree/master/testbed/examples">Wrapped2D examples</a>
- Box2D site: <a href="https://box2d.org/">box2d.org</a>
- Box2D GitHub repo: <a href="https://github.com/erincatto/box2d">erincatto/box2d</a>

## 🙌🏽 Rust Game Physics Engines: Wrapping Up

In this Rust game physics engines post, we got an introduction to Rust physics engine line-up.  In particular, we saw:

- an **overview of the pure-Rust and C++ binding physics engines**;
- some **key feature oof the engines**; and
- some **resources for physics engine research**.

I would love to hear from you, if you are also new to Rust game development.  Do you have alternative resources you found useful? Is there a physics engine, or some detail that I missed?

## 🙏🏽 Rust Game Physics Engines: Feedback

If you have found this post useful, see links below for further related content on this site.  Let me know if there are any ways I can improve on it. I hope you will use the code or starter in your own projects. Be sure to share your work on X, giving me a mention, so I can see what you did. Finally, be sure to let me know ideas for other short videos you would like to see. Read on to find ways to get in touch, further below. If you have found this post useful, even though you can only afford even a tiny contribution, please <a aria-label="Support Rodney Lab via Buy me a Coffee" href="https://rodneylab.com/giving/">consider supporting me through Buy me a Coffee</a>.

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via <a href="https://twitter.com/messages/compose?recipient_id=1323579817258831875">@askRodney</a> on X (previously Twitter) and also, join the <a href="https://matrix.to/#/%23rodney:matrix.org">#rodney</a> Element Matrix room. Also, see <a aria-label="Get in touch with Rodney Lab" href="https://rodneylab.com/contact/">further ways to get in touch with Rodney Lab</a>. I post regularly on <a href="https://rodneylab.com/tags/gaming/">Game Dev</a> as well as <a href="https://rodneylab.com/tags/rust/">Rust</a> and <a href="https://rodneylab.com/tags/c++/">C++</a> (among other topics). Also, <a aria-label="Subscribe to the   Rodney Lab newsletter" href="https://newsletter.rodneylab.com/issue/latest-issue">subscribe to the newsletter to keep up-to-date</a> with our latest projects.