In the past I have experimented with sequential impulses to implement constraints (see
part 1,
part 2,
part 3,
part 4,
part 5,
part 6 of my rigid body physics series).
I tried to integrate Runge-Kutta integration with sequential impulses.
However it was difficult to prevent interpenetration of objects.
Also implementing a vehicle with wheels and suspension, where the weight ratio between the vehicle and the wheels was high, required a high number of iterations to stabilise.
Finally stacking of boxes turned out to be unstable.
In 2022 Jorrit Rouwe released JoltPhysics which is a physics engine for 3D rigid objects also using sequential impulses.
His GDC 2022 talk Architecting Jolt Physics for Horizon Forbidden West refers to Erin Catto’s talk and discusses various performance optimisations developed in Jolt Physics.
In the following I have provided a few Jolt physics example programs to demonstrate some capabilities of the physics engine.
Installing Jolt
Jolt Physics is a C++ library built using CMake.
To compile with double precision, I invoked JoltPhysics/Build/cmake_linux_clang_gcc.sh as follows:
A release build with g++ and installation is done as follows:
Next you can have a look at JoltPhysics/HelloWorld/HelloWorld.cpp which is a simple example of a sphere bouncing on a floor.
The example shows how to implement the required layers and collision filters (e.g. stationary objects cannot collide with each other).
Make sure to define the Trace variable so you get useful warnings if something goes wrong.
Tumbling object in space
In this section we test the tumbling motion of a cuboid in space.
To compile a C++ program using Jolt, you need to use the same preprocessor definitions which were used to compile Jolt.
If you have set up the Trace function, you will get a warning if the preprocessor definitions do not match.
Here is an example Makefile to compile and link a program with the release build of the Jolt library, GLFW, and GLEW.
The core of the example creates a shape of dimension a×b×c and sets the density to 1000.0.
Furthermore the convex radius used for approximating collision shapes needs to be much smaller than the object dimensions.
The limit for the linear velocity is lifted and most importantly the solution for gyroscopic forces is enabled.
Furthermore linear and angular damping are set to zero.
Finally the body is created, added to the physics system, and the angular velocity is set to an interesting value.
The code snippet is shown below:
Here is a video showing the result of the simulation.
As one can see, Jolt is able to simulate a tumbling motion without deterioation.
In this section we test the falling motion of a stack of cuboids.
Three cuboids are created and the initial positions are staggered in the x direction to get a more interesting result.
Using i = 0, 1, 2 the cuboids are created in the following way:
Furthermore a ground shape is created.
Note that for simplicity I only created one layer.
If the ground was composed of multiple convex objects, a static layer should be created and used.
Note that the bodies need to be activated for the simulation to take place.
The simulation is run by repeatedly calling the Update method on the physics system.
The following video shows the result of the simulation.
For a more challenging demo of this type, see the Stable Box Stacking demo video by Jorrit Rouwe.
Double pendulum
The double pendulum is created using the HingeConstraintSettings class.
There are two hinges.
One between the base and the upper arm of the pendulum and one between the upper arm and the lower arm.
The physics library also requires initialisation of a vector normal to the hinge axis.
Another test case is a prismatic joint with a suspension constraint.
The prismatic joint is created using the SliderConstraintSettings class.
The suspension is created using a soft distance constraint.
The code snippet is shown below:
The video shows the result of running this sumulation.
Jolt comes with a specialised implementation for simulating wheeled vehicles (there is also even one for tracked vehicles).
The vehicle API allows placing the wheels and adjusting the suspension minimum and maximum length.
One can set the angular damping of the wheels to zero.
Furthermore there are longitudinal and lateral friction curves of the wheels which I haven’t modified.
Finally there is a vehicle controller object for setting motor, steering angle, brakes, and hand brake.
A vehicle dropping on the ground with horizontal speed is shown in the following video.
Note that the inertia of the wheels was high in this video.
One can correct this by reducing the inertia of the wheels as follows.