Jolt Physics raylib: trying 3D C++ Game Physics Engine
Tags: cpp, gamedev, cmake
Published: 2024-04-10 08:55:13

---
title: "Jolt Physics raylib: trying 3D C++ Game Physics Engine"
published: "true"
description: "Jolt Physics raylib ⚡️ trying Jolt C++ game development physics library with raylib in a 3D hello world 🎱 bouncing sphere example."
tags: "cplusplus, gamedev, cmake"
canonical_url: "https://rodneylab.com/jolt-physics-raylib/"
cover_image: "https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cta0l0shjlgqxwdi7s1k.png"
---
## 🏎️ 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 <a href="https://rodneylab.com/rust-game-physics-engines/">recent post on Rust physics engines</a>, 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 <a href="https://store.steampowered.com/app/2420110/Horizon_Forbidden_West_Complete_Edition">game physics engine used in Horizon Forbidden West</a>.

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, <a href="https://github.com/jrouwe/JoltPhysics#features">fully featured game physics engine</a> with cross-platform and WASM support.  It uses C++17 and there are also Zig bindings.  There is also a <a href="https://github.com/godot-jolt/godot-jolt">Godot drop-in physics engine</a> replacement extension for Godot 4.2.

The quickest way to get going is probably to build the <a href="https://github.com/jrouwe/JoltPhysicsHelloWorld">CMake Jolt hello world sample</a>.  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 Physics raylib: screen capture from Jolt hello world shows terminal output. First line reads '[2024-03-30 13:21:22.049] [info] A body got activated', then '[2024-03-30 13:21:22.050] [info] Step 1: Position = (0.00, 2.00, 0.00), Velocity = (0.00, -5.00, 0.00)', 15 more steps show the ball accelerating downward and dropping, with y velocity becoming more negative and y position tending towards zero. Between steps 16 and 17, the log reads 'Contact validate callback'.  This is repeated after each of the remaining 4 steps in view, over which velocity and position vary only slightly. Y position is around 0.45 and y velocity 0 for these last steps.  All shown steps are within the span of a few milliseconds.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/09yrncab6ub68uslnvpv.png)

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 <a href="https://jrouwe.github.io/JoltPhysics/index.html#collision-detection">how Jolt implements Collision Detection</a>, for example.

## 🤔 Other C++ Game Physics Engines

- <a href="https://box2d.org/">Box2D</a>: **2D engine** used in Unity and also earlier versions of Godot. Open source.
- <a href="https://github.com/bulletphysics/bullet3">Bullet</a>: C++ 2003 physics engine available in Godot 3. Open source.
- <a href="https://www.havok.com/havok-physics/">Havoc</a>: Physics engine used on **Call of Duty games** and other well-known games. Proprietary licensing.
- <a href="https://github.com/NVIDIA-Omniverse/PhysX">PhysX</a>: **mature C++ physics engine** from NVIDIA, used in Unreal Engine and Unity 3D.  Open source.

## 🧱 What I Built

![Jolt Physics raylib: screen capture shows a small, red sphere mid-bounce above a larger, flat wireframe grid in three-dimensional perspective.  At the top, left of the main panel, text shows the app is rendering at 60 frames per second.  Below that, text reads 'Press F 9 for I m G u i debug mode'.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cqptyfi5p1ug48kjeiwg.png)

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.


```cpp
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 <a href="https://github.com/rodneylab/jolt-raylib-hello-world">get the full project code on the Rodney Lab GitHub repo</a>.  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 <a aria-label="Support Rodney Lab via Buy me a Coffee" href="https://rodneylab.com/giving/">consider supporting me through Buy me a Coffee</a>.

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 <a href="https://twitter.com/messages/compose?recipient_id=1323579817258831875">@askRodney</a> on X (previously Twitter) and also, join the <a href="https://matrix.to/#/%23rodney:matrix.org">#rodney</a> Element Matrix room. Also, see <a aria-label="Get in touch with Rodney Lab" href="v/contact/">further ways to get in touch with Rodney Lab</a>. I post regularly on <a href="https://rodneylab.com/tags/gaming/">Game Dev</a> as well as <a href="https://rodneylab.com/tags/rust/">Rust</a> and <a href="https://rodneylab.com/tags/c++/">C++</a> (among other topics). Also, <a aria-label="Subscribe to the Rodney Lab newsletter" href="https://newsletter.rodneylab.com/issue/latest-issue">subscribe to the newsletter to keep up-to-date</a> with our latest projects.