Physics in SandBox: Why I Chose Jolt Physics


Jolt Physics determinism test results

Continuing my development diary. Today I want to talk about one of the very important things in a networked virtual world: physics.

The requirement that makes everything harder

The project is networked, which means every node must see the same world. Not a "roughly similar" one, but the same: if a box position diverges between the client and the server, everything else will diverge a couple of seconds later, and no amount of interpolation will save it.

Physics is the hardest part. A physics engine runs on floating-point numbers, and determinism does not appear there by itself: it has to be ensured in every small detail.

I was honestly prepared to write physics myself. A custom physics engine is very interesting, and it gives control over every operation, but it also means months of work, plus collision and joint quality that I alone would hardly make better than people who have spent years on it.

Jolt Physics

While looking at alternatives, I came across Jolt Physics. First, it is MIT-licensed, so it can be used in a closed-source commercial product, which is essential for the project. Second, the documentation explicitly advertises a determinism mode (CROSS_PLATFORM_DETERMINISTIC), and specifically cross-platform determinism: the same result across compilers and architectures.

It sounded too good to take on faith. Documentation is not proof, and "determinism" means different things in different engines: sometimes it means "the same on the same machine", while I need "the same on the server and the client". Especially considering that clients may run on machines with different architectures and operating systems.

So I did not pull the engine into the project right away. First, a test bench.

What exactly I tested

For this, I built a test bench: a small standalone program that is not part of the main build.

  • A scene with a floor and 64 dynamic boxes placed by a deterministic generator with a fixed seed;
  • A fixed time step and scripted impulses at predefined ticks, so the scene is not just "things falling down";
  • A canonical state hash: positions, rotations, linear and angular velocities. Two hash variants: a quantized one (position to 1/1024 m, rotation components to 1/16384) as the main criterion, and a raw bitwise one for diagnostics;
  • Saving the state to a blob in the middle of a run and restoring from it, checking that the final hash matches the continuous run.

The scenario is the same in every run: 1000 ticks and separately 20000 ticks, 1, 4, and 8 threads, Debug and Release.

About arm64 and the emulator

Building for x86_64 was routine, but arm64 was more interesting: it has a different SIMD path, NEON instead of SSE, and that is exactly where determinism usually falls apart.

I did not have real arm64 hardware at hand, so I used QEMU user-mode emulation: I started an aarch64 container on the same x86 host, built the dependencies and the bench inside it, and ran the same scenarios. That it is emulation is obvious without any flags: if you enter the "arm64" container and run lscpu, you see the same x86 processor of the host instead of an ARM one. The build and the run are real, the aarch64 binary is real, it is just executed by an emulator.

Results

In short: on this scene and in this configuration, there were no divergences at all.

Scenario Blob
1000 ticks, x86_64 and arm64 (QEMU), Debug/Release, 1/4/8 threads 25752 bytes, hash matches
20000 ticks, x86_64 and arm64 (QEMU), Debug/Release 25752 bytes, hash matches

What this means:

  • The hashes matched not only at the end but also at every intermediate tick where I recorded them, so the divergence did not "accumulate and then disappear": it never existed;
  • Debug and Release produce the same result, so compiler optimizations do not change the picture;
  • The number of threads does not affect the result: 1, 4, and 8 all produce the same one;
  • Saving and restoring in the middle of a run produces exactly the same state as a continuous run in all 16 runs;
  • x86_64 went through SSE, while arm64 used no SSE at all, and these are different execution paths of the same code, yet the hash is identical;
  • The raw bitwise hash matched too, not just the quantized one. That is already stronger than "visually identical".

What this does not prove yet

  • There was no native ARM. Emulation proves that the aarch64 code behaves like the x86_64 code. It does not prove that real ARM hardware will behave the same way. That check is postponed until I have a machine.
  • The scene is simple. It has neither joints nor ragdoll, and contact events are not part of the hash.
  • One compiler. Clang only.
  • Only Linux was tested. Both builds are Linux x86_64 and Linux aarch64; Windows did not participate in this check.
  • Step timing under emulation is meaningless. Measurements from ARM emulation are about 30 times slower; that is a property of the emulator, not of ARM.

What's next

At the moment I am choosing Jolt as the physics engine library and, just in case, hiding it behind abstract interfaces. If further checks go as well, this will save a huge amount of time and let me avoid implementing my own physics library right now. I will close the question for good after ragdoll with joints and contacts and a repeated run on a real ARM machine: if determinism holds there as well, the physics engine choice can be considered settled. And if something falls apart somewhere, at least I will know where exactly, instead of finding out later.