OpenGL is a reasonably abstract API for doing 3D graphics.
In the past I did an example of OpenGL using GLUT.
However GLUT is a bit outdated now and a more modern alternative is GLFW.
The example still uses GLEW to setup the OpenGL extensions.
This example is minimal and only uses a vertex shader and a fragment shader to get started with OpenGL.
For an example using tesselation and geometry shaders as well, see my short introduction to OpenGL.
Note that it is important to add code for retrieving error messages (as I have done below) in order to be able to do development of the shaders.
As in my old example, the code draws a coloured triangle on the screen.
The example uses the widely supported OpenGL version 3.1 (which has the version tag 130).
You can download, compile, and run the example as follows:
Any feedback, comments, and suggestions are welcome.
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 tested a few base cases of rigid body physics with the Jolt Physics library.
Installing Jolt
Jolt Physics is a C++ library built using CMake.
To compile with double precision, I changed the cmake call in 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.
This is a small example on how to implement an interpreter using Clojure and the Instaparse library.
Dependencies
First we create a deps.edn file to get Rich Hickey’s Clojure, Mark Engelberg’s Instaparse, the Midje test suite by Brian Marick, and my modified version of Max Miorim’s midje-runner:
Initial setup
Next we create a test suite with an initial test in test/clj_calculator/t_core.clj:
You can run the test suite as follows which should give an error.
Next we create a module with the parser in src/clj_calculator/core.clj:
We also need to create an initial grammar in resources/clj_calculator/calculator.bnf defining a minimal grammar and a regular expression for parsing an integer:
At this point the first test should pass.
Ignoring whitespace
Next we add a test to ignore whitespace.
The test should fail with unexpected input.
The grammar needs to be modified to pass this test:
Note the use of ‘<’ and ‘>’ to omit the parsed whitespace from the parse tree.
Parsing expressions
Next we can add tests for sum, difference, or product of two numbers:
The grammar now becomes:
Transforming syntax trees
Instaparse comes with a useful transformation function for recursively transforming the abstract syntax tree we obtained from parsing.
First we write and run a failing test for transforming a string to an integer:
To pass the test we implement a calculator function which transforms the syntax tree.
Initially it only needs to deal with the nonterminal symbols START and NUMBER:
Performing calculations
Obviously we can use the transformation function to also perform the calculations.
Here are the tests for the three possible operations of the parse tree.
The implementation using the Instaparse transformation function is quite elegant:
Recursive Grammar
The next test is about implementing an expression with two operations.
A naive implementation using a blind EXPR nonterminal symbol passes the test:
However there is a problem with this grammar: It is ambiguous.
The following failing test shows that the parser could generate two different parse trees:
When parsing small strings, this might not be a problem.
However if you use an ambiguous grammar to parse a large file with a syntax error near the end, the resulting combinatorial explosion leads to a long processing time before the parser can return the syntax error.
The good thing is, that Instaparse uses the GLL parsing algorithm, i.e. it can handle a left-recursive grammar to resolve the ambiguity:
This grammar is not ambiguous any more and will pass above test.
Grouping using brackets
We might want to use brackets to group expressions and influence the order expressions are applied:
The following grammar implements this:
A final consideration is operator precedence of multiplication over addition and subtraction.
I leave this as an exercise for the interested reader ;)
Main function
Now we only need a main function to be able to use the calculator program.