WOAH
A dark red nebula backdrop with one orange body near the centre, a handful of tiny bodies with faint orbit trails, an Add Body button top right and pause and speed controls along the bottom

Play

A student's gravity engine keeps 6,000 bodies at 60 Hz

By The host5 min#physics#webassembly#space

HelioSim is an N-body gravity simulator written in C++ and compiled to WebAssembly. Barnes-Hut trees, Verlet steps and a published benchmark table show how far one tab can go.

Small points of light are sliding across a blood-red nebula, each dragging a thin line behind it, and an orange dot near the middle of the frame is pulling on all of them. Nothing is scripted. Every curve on screen is being recomputed from Newton's inverse-square law about sixty times a second, and the line trailing behind each body is a record of where gravity has already taken it.

This is HelioSim, a gravity simulator by Clemens Koprolin, who posts as JustClemens and says on his own site that he is studying at ETH Zurich. The page gives you a canvas and almost nothing else: a line of instructions in the top corner (drag to orbit, scroll or pinch to zoom, shift to pan), an "Add Body" button, and a small bar with reverse, pause, play and a speed readout that starts at 1x.

What you can do to it

Add a body and it joins the system. Run time backwards with the reverse button, or push it past 1x to watch an orbit play out in a few seconds. The repository README lists the rest: planets, stars and black holes drawn differently, collisions that merge bodies while preserving momentum, and orbit trails in two flavours, a historical one behind each body and a predicted one running ahead of it.

The predicted trail is the part I kept coming back to. To draw where a body will be, the engine has to run the future in a scratch copy of the system, every frame, on top of running the present. That is a lot of physics to hide behind one thin line.

The number he published

Most toys leave you to guess how hard they are working. The README prints a benchmark table: milliseconds for one complete force pass, meaning a tree rebuild plus an acceleration for every body.

At 1,000 bodies the browser build takes 1.30 to 1.50 ms. At 6,000 it takes 16.40 ms, just inside a 60 Hz frame. At 10,000 it is 30.80 to 31.30 ms, at 50,000 it is 236.70 to 246.90 ms, and at 100,000 bodies it is 544.10 to 579.40 ms. The native C++ build, measured on the same Apple M3 Max, lands between 494 and 522 ms at 100,000. WebAssembly is running within roughly ten percent of native at that size, and at 1,000 bodies the browser figure sits level with it.

He is also upfront about the other side. The app runs three physics substeps per frame, which means six force passes, so the README puts the force-only ceiling for a full 60 FPS in the browser at about 1,750 bodies, and says collisions, trail prediction and rendering push that lower. The 6,000 figure is for one pass, not the whole app. Both numbers are in the same document, which is why I trust either.

How it is built

The physics engine is C++, compiled to WebAssembly with Emscripten, and it draws through OpenGL ES 3. A browser has no OpenGL ES, so Emscripten supplies the bridge: its OpenGL support layer turns the same GL calls into WebGL 2 when you link with the right flag. The Show HN post lists GLFW as the window layer, and the project page on his site describes the whole thing as a C++ and OpenGL simulator with Barnes-Hut optimization, so the code in the tab is a desktop-style program, not a JavaScript rewrite.

The force calculation is the clever bit. Adding up every pair of bodies costs N squared, so ten thousand bodies means a hundred million pair interactions per pass. HelioSim instead uses a Barnes-Hut octree. The method comes from Josh Barnes and Piet Hut's December 1986 paper in Nature, which brings the cost down to N log N by treating a distant clump of bodies as one heavier body sitting at its centre of mass.

Whether a clump is far enough is decided by a ratio, usually called theta: the width of the tree node divided by the distance to it. Below the threshold, the node is summed up as a single point. Above it, the code opens the node and looks at its children. A theta of zero degenerates to the full pairwise sum, so it is the accuracy dial. The benchmark runs at 0.5, with at most 8 bodies per leaf, and at 10,000 bodies the README measures the tree at about 27 ms against 296 ms for the direct solver Koprolin started with, roughly eleven times faster.

The time step is velocity Verlet, which has second-order error and, as the integrator's reference article puts it, lets energy oscillate around the true value instead of drifting away from it. For an orbit simulator that is the difference between a planet that circles for an hour and one that spirals off after ten minutes. The tests folder ships correctness, accuracy and scaling checks, run with make test.

What it cost him

The hard part was not the maths. In the Show HN post, Koprolin calls this his first larger WebAssembly project, says he was surprised by how well browsers handle a C++ and OpenGL ES pipeline, and names the tiring part: bridging events and state between JavaScript and C++. Every button on the page lives on the JavaScript side of that border and every body lives on the other. The post asked for performance reports from other machines, and the repo has a browser benchmark build, make benchmark-wasm, so you can send your own numbers.

The build is deliberately plain: make, Emscripten, Python 3 for a local server, and GLM for vector maths, vendored in the repo. Twelve commits, one author, ten entries at the top level of the repo.

Where to go next

If you want the whole route, clone the repo and run make. If you want a sibling to compare against, Philipp Jurašić's Barnes-Hut galaxy page embeds another C++ simulation compiled with Emscripten, from a TUM course project; the page says a Morton-ordered traversal bought it about 20 percent more frames. It makes a good second tab beside this one, because the two disagree about how much to show you.

And for the same itch from another angle, the physics toys on one developer's shelf take fluids and fur where this takes gravity. Koprolin's own site lists HelioSim among his graphics projects, next to a Three.js multiplayer tank game and a Blender scene.

Previously

Escher's Print Gallery zooms forever in a shader