Using Jolt with flecs & Dear ImGui: Game Physics Introspection
Tags: cpp, gamedev, cmake
Published: 2024-04-17 16:32:43

---
title: "Using Jolt with flecs & Dear ImGui: Game Physics Introspection"
published: "true"
description: "Using Jolt with flecs & Dear ImGui 👀 to add game physics introspection to a raylib game, with pause and stepping for dev tools debugging 🐜."
tags: "cplusplus, gamedev, cmake"
canonical_url: "https://rodneylab.com/using-jolt-with-flecs-dear-imgui/"
cover_image: "https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdp6kado4xd4g8bz821y.png"
---
## 👀 Jolt Game Physics Introspection with flecs & Dear ImGui

In this post we extend on recent posts, using Jolt with flecs & Dear ImGui to add a kind of dev tools debugging panel to a raylib game. In previous posts, we added Jolt physics to the raylib game, and also looked at adding Dear ImGui to raylib in the previous iteration of the dev tools panel.

Here, we take things a step further. For debugging game physics, I wanted to be able to pause the simulation and also step through it. I also wanted to be able to inspect game entity physical properties in real-time. I added an ECS using flecs. flecs does have its own explorer, though I preferred trying to keep everything in the same window, using ImGui. As a result, you can see the live values for physical properties, tied to the game state as you pause and step through.

Here, I just run through some decisions made in putting the demo together. If that sounds interesting to you, then let&rsquo;s press on!

## 🧱 What I Built

I wanted something I could use in games I am working on in raylib, and went for a two-view approach. The main view shows the game or simulation as the end-user would see it. The second view, opened by pressing the <kbd>F9</kbd> key, is for developers, much like dev tools in a web browser. This developer view scales the main app down onto a texture to make way for the dev tools or debugging panel.

![Using Jolt with flecs: screen capture shows a red sphere sitting on a large wireframe grid, in the centre of the screen.  At the top, left there is a rendering frames per second readout.  Below that, text reads 'Press F 9 for I m G u i debug mode'.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gemexlbg871tmv2w28p9.png)

The demo features a bouncing ball with physics managed by Jolt. For now the dev tools panel lets you update the ball&rsquo;s mesh and get real-time readouts of its position and velocity. Finally, you can pause playback, making it easier to read data, and also step through the simulation, frame-by-frame; handy for debugging.

![Using Jolt with flecs: screen capture shows a red sphere bouncing above a large wireframe grid, in the centre of a large panel.  A smaller panel sits to the left of the main one.  This smaller panel has velocity and position details for the sphere, as well as Play, Pause, and Step buttons.  A list of colours with red (matching the colour of the sphere) selected sits at the bottom of the panel.  At the top, left of the main panel, there is a rendering frames per second readout.  Below that, text reads 'Press F 9 for I m G u i debug mode'.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dultwpna68gspb75u2fa.png)

## 🧩 flecs ECS

I went for flecs as the ECS. It has a nice API and compiles to WASM (great for putting demos together). The <a href="https://www.flecs.dev/flecs/">flecs docs are also fantastic</a> too. I used C++ in this project, though flecs also supports C and C#. Finally, there is a <a href="https://github.com/flecs-hub/explorer">web-based flecs ECS monitoring app</a> and a <a href="https://www.flecs.dev/flecs/group__c__addons__stats.html">flecs statistics add-on</a> (which I did not use here).

<a href="https://github.com/skypjack/entt">EnTT is a popular alternative to flecs</a> for C++, which has different performance/memory characteristics.

## ⚡️ ImGui: Immediate Mode GUI Library

Immediate mode GUIs are brilliant for this kind of project. They have a small drawback in that you can have less control over precise layout. The huge advantage, though, is that there is no need for callbacks and on-click handlers; the button and code executed when it is pressed are right next to each-other.

As an example, here is the code for the play button used to restart simulation after pausing:

```cplusplus
// Play button
// Button styling
ImGui::PushID(0);
ImGui::PushStyleColor(
    ImGuiCol_Button,
    (ImVec4)ImColor::HSV(kPlayHue, kRegularSaturation, kRegularLightness));
ImGui::PushStyleColor(
    ImGuiCol_ButtonHovered,
    (ImVec4)ImColor::HSV(kPlayHue, kHoveredSaturation, kHoveredLightness));
ImGui::PushStyleColor(
    ImGuiCol_Button,
    (ImVec4)ImColor::HSV(kPlayHue, kEngagedSaturation, kEngagedLightness));

// Add the button itself
if (ImGui::Button("Play"))
{
    // Code to execute when the button is clicked
    spdlog::info("Play clicked");
    if (dev_panel_state._paused)
    {
        dev_panel_state._paused = false;
    }
}
ImGui::PopStyleColor(3);
ImGui::PopID();
```

We set colours for the three button states, then inline, have the logic for updating the app state. The <a href="https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html">Dear ImGui Online Manual has a ton of code examples</a>, which are invaluable for building this kind of widget if you are new to Dear ImGui.

<a href="https://github.com/Immediate-Mode-UI/Nuklear">Nuklear is an alternative Immediate GUI</a>, also written in C.

## 🏁 Next Steps
  
So far, the code just represents a minimal viable concept. I want to add it to an existing project to get a better feel for shortcomings and what is missing.  It would be interesting, also to experiment more with the flecs World Explorer, for inspiration on features and also to consider focus on features which that tool does not already offer, to avoid reinventing the wheel.

## 🙌🏽 Using Jolt with flecs: Wrapping Up

In this using Jolt with flecs post, I walked through some of the decisions I made in putting the demo together. More specifically, we saw:

- **why you might use flecs with Dear ImGui for Jolt physics introspection**;
- why **I believe an immediate mode GUI like Dear ImGui is a good match** for this project; and
- some **tips on starting out with Dear ImGui**.

I hope you found this useful. As promised, you can <a href="https://github.com/rodneylab/raylib-flecs-imgui-introspection">get the full project code on the Rodney Lab GitHub repo</a>. What suggestions do you have for taking this further? Is this something you would find useful in your game development tool belt? 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.

## 🙏🏽 Using Jolt with flecs: 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="https://rodneylab.com/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.