Why use this library not bullet or box 2d?

Hi and thank you for the library. I wonder why i would take this library over the use of bullet or box 2d, which looks to have more features and to be more fast. Does nphysics have more features or speed than bullet or box 2d maybe?


Well, yes, as of today nphysics is slower and as less features than bullet and box2d. But that’s just a matter of time, nphysics is very young while, e.g., bullet is more than 10 years-old! Actually, my first goal for the next couple of years is to make nphysics at least as good as those two (perhaps not including GPU acceleration because Rust does not have a good ecosystem for GPU programming at the moment). Then I intend to make it more flexible by allowing to switch between several simulation kinds (from fully kinematic to fully dynamic), with different levels of accuracy (from video-game to quasi-realistic). And one of the cool things with nphysics is that once this is done, you will get a 2D and a 3D engine simultaneously (the work will not have to be done twice).

One benefit of using nphysics right now instead of bullet/box2d is a smoother interaction with the rest of the Rust ecosystem. To use bullet or box2d you’d have to write bindings and ending up with a good rust-idiomatic API will probably be quite hard (for example bullet relies a lot on inheritance and global functions for example). Also, in the long run, you will not be able to extend (in Rust) neither box2d nor bullet with, e.g., you own collision shapes or your own dynamics solver. To do that you would have to write your extensions in C++ and somehow wrap them in Rust to be able to pass them to your bindings.

So, yeah, featurewise and performancewise nphysics is not extremely competitive yet, but it will only get better!

What would need to be done to get nphysics to run on the gpu as well as be as performant, or better then bullet or physx? I would love for a super fast physics engine to exist in rust and would like to help contribute some of my skills. Also, since it can swap between 3d and 2d physics, how hard would it be to add support for 1d and 4d?

edit: oh crap…just realized this thread was a year old, not a few days :confused:

Sorry for the late reply, I’ve been very busy those weeks.

The current design of nphysics has not been made with GPU in mind so I guess some architectural work is to be done to make it run on GPU. Also, we would first need to improve the current (or create new) rust tools for writing/running OpenCL kernels. It has been some time since I checked the ocl crate but when I did a couple of years ago, it was still buggy and non-ergonomic. Perhaps it is ready now? Also, being able to write OpenCL kernels in Rust would be extremely useful for this kind of undertaking. There has been some attempts of DSL or to compile rust code on the GPU, but nothing more recent AFAIK.

Regarding CPU performance, the first step would be to benchmark typical simulations on all three engines, and improve each part of nphysics accordingly. I am currently working on improving performances of nalgebra (which physics uses). nphysics relies on similar algorithms as Bullet, so we should be able to at least be as performant without too much effort.

Adding support for 1D should be quite straightforward. We just have to somehow disable rotations for 1D scenarios, then most of the existing code should work fine. Of course, we may then want to do some optimizations, e.g., regarding collision detection on 1D shapes which is straightforward.

Adding support for 4d should be much harder. We would first have implement 4D rotations on nalgebra. Then, after formulating kinetic and potential energy in 4-space, we have to see how Euler-Lagrange equations of motion unroll for 4D dynamics. Perhaps some additional inertial terms appear, and higher-order tensors may become necessary (e.g. for the rotational inertia tensor?), I don’t know yet. Some academic work exist for n-dimensional rigid body dynamics, but I never dug into it.

(Also, note that this year I focus mostly on nalgebra and ncollide so that they provide solid tools for future works on nphysics.)