🎊🎊🎊 Happy new year 2023 everyone! 🎊🎊🎊
This article summarizes the most significant additions made in 2022 to our open-source crates for linear-algebra and physics simulation that we develop for the Rust community. We also present our main objectives for 2023.
Rapier: 2D and 3D physics
Rapier is a 2D and 3D physics engine for games, robotics, and animation written in Rust. Our
goal for 2022 was to
work on the missing high-level features of Rapier. This month we released the version
0.17 of Rapier with significant feature additions since the version
0.12 released in January 2022:
- The addition of a kinematic character controller.
- The addition of a dynamic vehicle controller.
- The addition of a generic debug-renderer.
- The exploration of destructible voxel-based physics.
- The significant improvement of the ergonomics of bevy-rapier.
- The fix of the internal edges problem in 3D: flat meshes no longer cause unexpected bumps when something slides or rolls on it.
Kinematic character controller
We added a kinematic character controller to Rapier
(as well as
bevy-rapier and the
JS bindings for rapier). The character
controller was designed mostly with platformers in mind, supporting the following operations:
- Slide on uneven terrains.
- Interaction with dynamic bodies.
- Climb stairs automatically.
- Automatically snap the body to the floor when going downstairs.
- Prevent sliding up slopes that are too steep.
- Prevent sliding down slopes that are not steep enough.
- Interactions with moving platforms.
- Report information about the obstacles it hit on its path.
Dynamic vehicle controller
A dynamic vehicle controller was also a very popular feature request. We ported to Rust the vehicle controller from the Bullet physics engine, and integrated it to Rapier. This vehicle controller is based on ray-casting (one ray per wheel) to detect the ground. It simulates wheel friction, suspension, and braking.
Dynamic vehicle controllers are only usable with the main Rapier crates (
rapier2d, rapier3d, rapier2d-f64, rapier3d-f64). It is not integrated yet into
bevy-rapier and it is not usable yet with the JS bindings either.
We added a debug-renderer to let the user see the scene as is actually seen by the physics engine, in order to easily spot common mistakes on objects dimentions and placements relative to their game assets.
Despite its name, Rapier’s debug-renderer doesn’t actually draw anything to the screen directly (except
bevy-rapier which actually draws lines using Bevy’s rendering capabilities).
Instead, the user is responsible for providing a method to draw lines using their favorite graphics engine. The
simply returns the lines that need to be rendered. This makes it very easy to integrate to any game engine for quick physics debugging!
In JS, the lines to draw can be obtained with
World.debugRender() which returns the vertex buffer and color
buffer to render. See rapier.js#119 for integration examples with THREE.js,
PIXI, and PlayCanvas.
Exploring destructible voxel-based physics
We explored the feasibility of destructible voxel physics with Rapier. This requires three parts:
- A new
Voxelsshape is composed of several grid-aligned voxels, where each voxel is represented as a pseudo-sphere (a sphere with cube-like corners along faces were the adjacent voxel exists). Using pseudo-spheres makes the collision-detection more efficient than using actual cubes.
- Collision-detection (also implemented in
parry) with these new
Voxelsshape, and, in particular, between two
- The definition of a
rapierresponsible for calculating approximate stress values within a single
Voxelsshape in order to detected potential fracture locations.
This first exploration resulted in the following proof-of-concept: a room collapsing under its own weight. Each new colored body appearing is a fractured piece that separates from the main body:
Bevy-rapier: ergonomics improvements
bevy-rapier3d crates provide plugins to use
rapier easily with the
bevy game engine.
In April 2022 we released a complete rewrite of this plugin to make the API significantly more "bevy-friendly":
- Colliders and rigid-bodies can be configured through multiple components.
- Collider and rigid-body positioning operates through the familiar
- Mesh colliders can be created automatically after a
Meshasset is done loading, using the
- All the components and query functions now rely
glamtypes instead of
nalgebratypes are only needed when calling into the raw Rapier objects (which can be used for niche operations that are not exposed explicitly by the plugin yet).
- A wireframe debug-renderer can be enabled with the new
These changes were strongly inspired by the heron crate which
had a way nicer API than
bevy-rapier before this rewrite
Game highlight: Ultimechs
In September 2022, our sponsor Resolution Games launched the exciting free-to-play multiplayer VR game: Ultimechs. Based on Unity, Ultimechs relies on our Rapier physics engine for physics. You can read more about Resolution Games’ use of Rust alongside Unity in their May 20 Insight.
Sparkl: exploring MPM physics simulation
The Sparkl project is entirely sponsored by Foresight Mining Software Corporation which currently leverages it in production for industrial applications.
We silently released in December 2022 our open-source MPM (Material Point Method) physics simulation libraries:
sparkl3d. In their current form, they supports 2D and 3D particle-based deformable physics simulation
with various materials, including sand, snow, elasticity and fracture. They do
not support two-way coupling with
rapier yet, meaning that Rapier colliders can be used
to define the ground using boxes or heightfields, but interaction with dynamic bodies isn’t
Sparkl runs on CUDA-enabled GPUs only (running on multiple GPUs is supported). All the CUDA kernels are written 100% in Rust thanks to the Rust CUDA Project.
nalgebra is a general-purpose linear-algebra library for Rust. It supports low-dimensional, high-dimensional, sparse matrices, as well as geometric transformations (rotations, isometries, etc.)
In 2022, most of our efforts went in Rapier rather than nalgebra itself. Most new features were added by the community, including:
- Matrix slices were renamed as matrix views to avoid confusion with rust slices (by Andlon #1178).
- Enabling the
rayonfeature of nalgebra will enable the
Matrix::par_column_iter_mutmethods. As their names suggest, these are rayon-based parallel iterators on the matrix’s columns (by geo-ant #1165).
- Generalized eigenvalues problem resolution, generalized eigenvalues/eigenvector calculations, and QZ decomposition have been added to
nalgebra-lapack(by metric-space #1067 and geoeo #1106).
- Performance of the product of two sparse matrices in
nalgebra-sparsehas been improved significantly (by smr97 #1081).
Check out the Changelog for details on the other additions.
What’s next in 2023
Because we focused on features during the past two years, we will take a different route for 2023. In its current form, Rapier looks complete enough feature-wise to fit most game use-cases. So our main objective will be to work on bug fixes, user-experience improvements, and documentation. This quality improvement effort will be focused mainly on Rapier, and include for example:
- Addressing most bugs reported on GitHub in 2022 and before, as well as complaints that are frequently reported on Discord (for example, related to some limitations of the character-controller when it hits vertical walls).
- Reduce risks of internal panics in Rapier (for example when a rigid-body has a
- Make the debugging experience nicer with the JS bindings by detecting potential problems (and raising an exception) before reaching the WASM module.
In order to get a better awareness of Rapier’s ergonomics issues and bugs, we will be working on a small physics sandbox app. It is currently closed-source, but will be open-sourced once it is more fleshed-out and usable. Think of something similar Phun/Algodoo but written in Rust, based on Rapier, compatible with WASM (i.e. runs on the browser), and handling both 2D and 3D (in two different apps. sharing the same codebase).
Writing this physics sandbox will also be the opportunity to explore the scaling large-scale distributed physics simulation. This exploratory work is sponsored by Futurewei.
We can’t thank enough:
- Foresight Mining Software Corporation for sponsoring Sparkl, all our CUDA-related efforts on nalgebra and parry, many of the new geometric primitives in Parry.
- Futurewei for sponsoring the ongoing exploration of distributed physics simulation.
- Fragcolor and Resolution Games for being our Gold sponsors for a whole year.
Thanks to all the former, current and new supporters through GitHub sponsors! This helps us tremendously to sustain our Free and Open-Source work.
This help is greatly appreciated and allows us to continue working on our open-source projects. Finally, a huge thanks to the whole community and code contributors!