← All notes

From a Physics Engine to an Arcade

The physics on this blog started with real questions. Where did a fireball come down? What would a starship drive cost in energy, and does the bill ever close? At what speed does the water boil off the face of a hull? The answers come from our own engine, powered by rainshower, and the engine works by one rule: a number does not count until a second, independent route gives it back. A closed form against an integration. A simulation against a measurement. Two methods that share nothing, agreeing.

That rule is what refused my own drive, and what found the speed where the water boils off a hull. It also turned out to be a very good way to look at game physics.

Somebody asked whether we can model everything

We cannot, and the answer turned into a physics playground: ten toys, each printing the number that would catch it lying. Three of them took the standard tricks of game physics and held each one up against the real thing.

  • The orbit. One moon, flown three ways. Euler's method, the first one anyone learns, takes a straight step along a curve. Each step lands a little outside, so each one adds a little energy. In ten orbits at 200 steps each, it invents 58 percent more energy than the moon started with, and the orbit it draws looks smooth, calm and believable the whole time.
  • The bounce. A ball dropped on a floor that returns 0.8 of its speed bounces infinitely often and is at rest after 4.06 seconds, a sum anyone can check with a pencil. The usual game loop steps time 1/60 of a second at a time and flips the ball's speed when it finds it under the floor. Twenty seconds in, it has bounced 174 times and still holds 1 percent of its energy. It never stops.
  • The rope. Game ropes have been points joined by links, pulled back to length a few times a frame, since 2001. Against the curve a real chain hangs in, one pass a frame leaves the rope 75 percent longer than it is. And giving it more links to look smoother makes it worse.

A dark panel with a small grey planet in the middle. A green moon and a blue ring sit together on a circle around it, and a red moon has spiralled outward, its red trail a widening loop well outside the circle.

The same moon flown three ways. Red is Euler, inventing energy. Green and blue are Verlet and RK4, still on the circle. A frame from the playground.

None of these is a bug in the usual sense. Each is a well formed answer, drawn smoothly on screen, that happens to be false. That is the shape this blog keeps coming back to, and games are where it looks most at home, because a game only has to look right.

Then the toys became games

Once the checks were in place, it was a short step to games that keep them.

Orbit Golf flies a ball under real inverse-square gravity from every planet on the board. Its step is fixed, 1/240 of a game second, using velocity Verlet, the method the playground showed holding a moon's energy. It has sixteen levels, and the site's checks fly a winning shot on every one of them on every build. Then I flew all 273,600 shots the first eight levels allow, and the map caught two of my own level hints lying.

Little Rocket is gravity, air, engines that push harder in a vacuum, and staging. Nothing in its code is told the rocket equation. Burned out in empty space, its rockets land on the rocket equation to nine decimal places anyway. A ledger in the readout counts every metre a second the engines give, and that is how the game shows orbit costing about a thousand metres a second more than orbital speed.

The GregTech pieces work the same way, with the modpack's own files as the outside answer. The circuit bench reads every recipe from them, and a script reads every number back out to check it.

Then they needed their own site

There got to be enough of them that they moved into an arcade of their own: Tofte Simulators, 41 simulators and games. Each one that came from a post links back to it. Twelve are games, and six of those were made for the arcade first. Those six had no post behind them, so each was given something outside its own code to answer to:

  • Long Range flies a bullet through standard air. Its tests hold it to an independent ballistics solver from 200 to 1,500 yards, to gravity alone in a vacuum, and to the textbook rule for wind drift. Every load uses the manufacturer's published figures.
  • Frisbee Golf flies four real discs on the aerodynamic tables of the shotshaper project. It is held to shotshaper's forces within a centimetre, and to its three measured throws.
  • Boiler Room runs GregTech's small steam boiler by the mod's own source, tick for tick. Every number names the file it came from, and a script checks each one against the mod's code.
  • Surf runs Counter-Strike: Source's movement rules, written here from the physics and checked number by number against Valve's published SDK. Its tests pin free fall, friction's stop, the air cap and the exact speed a strafe can add.
  • Wingsuit flies a pilot on two figures published for real wingsuits, a best glide of 2.5 to 1 at 45 metres a second. I could read them only in search summaries, and the code says so. Its tests hold the model to both.
  • Sand Lab is a falling-sand game, and every discovery it can announce has to be made by a test first. Its tests also check that a fall four times as high takes about twice as long, and that water finds one level through a U-bend.

The arcade's 172 game tests run on every build, and a failing one stops the deploy.

Event Horizon in the Surf game: a line of ramps spiralling away over a black hole, the hole a dark disc ringed with light at the lower left and its glowing disc filling the view below

What the games give back

Holding a game to an outside answer does more than keep it honest. It makes the game answer questions too.

Run Surf's ghost through an energy ledger and about nine tenths of the energy it moves with turns out to come from falling. Strafing adds about a tenth. Fly the wingsuit's two published numbers through a textbook wing, and the slowest way down turns out not to be the furthest. Neither result was the point of building the game. Both came out because the games were built to be checked.

It is the same engine and the same rule in both places, whether it is pricing a starship or bouncing a ball. A game is just a simulation that somebody wants to play, and it deserves the same check as everything else.