Jolt Physics raylib
Trying 3D C++ Game Physics Engine
🏎️ Trying the Jolt Physics Engine with raylib
In this Jolt physics raylib post, we take a look at using the Jolt 3D physics library with raylib. In previous posts, I mentioned how raylib is a fantastic tool for testing. raylib is by no means a full-featured game engine, and so has no in-built physics engine; it is great for putting a game together quickly though, without having to worry about setting up Vulkan or platform specific graphics libraries.
Jolt is one of the newer C++ game physics engines. As mentioned in the recent post on Rust physics engines, many developers work with it in Godot, for the potential stability improvements it brings over the default Godot 4 physics engine. Of course, it can be used independently of Godot and is the game physics engine used in Horizon Forbidden West.
Here, we take a look at Jolt learning resources, take a brief look at other C++ game engine alternatives and finally put together a 3D Jolt physics raylib demo, by expanding the Jolt hello world example. I hope that sounds like it is worth the read! Please let me know how I can improve the content, especially if there is something that might help others get started with Jolt.
⚡️ Jolt Learning Resources
Jolt is a modern, fully featured game physics engine with cross-platform and WASM support. It uses C++17 and there are also Zig bindings. There is also a Godot drop-in physics engine replacement extension for Godot 4.2.
The quickest way to get going is probably to build the CMake Jolt hello world sample. That sample simulates a sphere dropping onto a static surface. It just outputs the sphere velocity and position to the console, which makes it quicker to start on and allows you to bring your own graphics renderings (which is what we do later in this post).
Jolt has fantastic documentation. In fact, even if you are not interested in trying it, but want to learn more about how physics engines work, the docs are worth a read. See the section on how Jolt implements Collision Detection, for example.
🤔 Other C++ Game Physics Engines
- Box2D: 2D engine used in Unity and also earlier versions of Godot. Open source.
- Bullet: C++ 2003 physics engine available in Godot 3. Open source.
- Havoc: Physics engine used on Call of Duty games and other well-known games. Proprietary licensing.
- PhysX: mature C++ physics engine from NVIDIA, used in Unreal Engine and Unity 3D. Open source.
🧱 What I Built
The Jolt Hello World example is a good starting point. It is stand-alone and outputs the updated positions to the Terminal. I wanted something more visual, so added Jolt to the raylib Dear ImGui demo I built previously. The physics code I wrote is only a small extension on Jolt demo. I added restitution to the sphere, so that it bounces, and changed up the initial velocity a little for a better visual example.
🖥️ Jolt Physics raylib
I made minimal changes to the Hello World CMake file, and you can follow the link, further down, to see the full code.
To make cleaner code, I abstracted Jolt into a class, then just called the update method from the main game loop. This got an updated ball position from Jolt, ready to render the ball in its new position for the next frame.
int main()
{
// ... TRUNCATED
spdlog::info("Creating Physics Engine");
PhysicsEngine physics_engine{};
spdlog::info("Initialising Physics Engine");
physics_engine.initialise();
spdlog::info("Creating floor");
physics_engine.create_floor(floor_dimensions, floor_position);
spdlog::info("Creating ball");
physics_engine.create_ball(constants::kBallRadius,
sphere_position,
sphere_velocity);
spdlog::info("Initiating Pre-simulation Optimisation");
physics_engine.start_simulation();
SetTargetFPS(constants::kTargetFramerate);
spdlog::info("Starting Simulation");
while (!WindowShouldClose())
{
// TRUNCATED...
BeginDrawing();
draw_scene(camera,
sphere_position,
constants::kSphereColours[static_cast<size_t>(
selected_sphere_colour)],
font);
EndDrawing();
// advance the physics engine one step and get the updated sphere_position
physics_engine.update(frame_time, sphere_position);
}
spdlog::info("Preparing Physics Engine for Shutdown");
physics_engine.cleanup();
return 0;
}
🙌🏽 Jolt Physics raylib: Wrapping Up
In this Jolt physics raylib post, we had an introduction to the Jolt physics engine and using it with raylib. More specifically, we saw:
- how you can use Jolt with raylib;
- some resources for getting started with Jolt; and
- Hello World C++ code for using Jolt with raylib.
I hope you found this useful. As promised, you can get the full project code on the Rodney Lab GitHub repo. Of course, we have only looked at the basics here. As a next step, you might want to add some physics introspection and the ability to pause step incrementally, ready for any debugging you might need to do on your game physics. Do let me know how you use this code. Also reach out if there is anything I could improve to provide a better experience for you.
🙏🏽 Jolt Physics raylib: 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 consider supporting me through Buy me a Coffee.
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 @askRodney on X (previously Twitter) and also, join the #rodney Element Matrix room. Also, see further ways to get in touch with Rodney Lab. I post regularly on Game Dev as well as Rust and C++ (among other topics). Also, subscribe to the newsletter to keep up-to-date with our latest projects.