Reinventing the Universe

Alex Anderson 🚀Alex Anderson 🚀, July 19, 2023

Hi gang! Happy Thorium Thursday!

I'm in a mood today, so brace yourself for some snarky high-level math explanations. Hopefully you get a kick out of it.

Carl Sagan famously said

If you wish to make an apple pie from scratch, you must first invent the universe.

Of course, we're not building something as intimidating as an apple pie - just a little spaceship game. So we don't have to invent the whole universe. But there are parts of it that come in handy.

For example, there's that part of physics which says "When you apply thrust to an object, it should move." I've already managed to create a crude approximation in Thorium Nova of that which allows ships to fly around planets and even travel between solar systems.

But there's another ingredient that's almost as important: "objects that come in contact should bounce off each other." This is a tricky proposition due to a pesky feature of mathematics called "precision".

Suppose you've got a ship going 1,500 m/s, which is the maximum speed for impulse engines, and you're measuring your universe in meters, like we do in Thorium Nova. That means that every frame (1/60th of a second), that ship moves 25 meters.

Suppose directly in front of that ship is a shuttlecraft, a mere 10 meters long. In the real universe, that shuttlecraft is gonna get clobbered if it doesn't move. But with a sufficiently naïve simulation, it's possible for the fast ship to jump right over the little ship without ever coming in contact with it, since it moves further than the length of the shuttlecraft every frame.

That's why we turn to Real Mathematicians™ who know a thing or two about "raytracing" - whatever that is. There are plenty of physics libraries out there - some which already power your favorite video games. (Anybody else have their mind blown by Tears of the Kingdom's crazy Ultra Hand physics?)

I figured I'd try popping one of these fancy engines into Thorium Nova, and I managed to get it working! You can see it in action in this tweet.

There was a problem, though - again, that annoying precision was rearing its head again. This time, it had to do with how numbers are stored in the computer. Decimals are kinda tough to store using 1s and 0s, so folks came up with a clever math trick that uses a base, an exponent, and a mantissa to create a floating point number, where the decimal can move around depending on how much precision is needed.

The problem with floats is that each number only gets so many 1s and 0s. As numbers get bigger, instead of precision being used for storing extra decimal places, those decimal places start disappearing so those bigger numbers can be represented.

And we're dealing with numbers astronomically large numbers, so I found out pretty quickly that this physics system just couldn't handle it. The way I found out was by trying to run into a planet by pointing my ship at it and activating the engines. But instead of running into the planet, my ship strafed around it - the nature of the numerical precision made it so my sidewise velocity increased faster than my forward velocity. Weird.

So I found myself in a bit of a pickle. And that's why you haven't heard from me in a while. I've been stuck trying to find a way to solve this that isn't crazy town banana pants.

What I've come up with is a little bit of slight-of-hand. See, the precision doesn't fall off until about 2 million kilometers, which is about 5 times the distance from the Earth to the moon - more than enough room to play around.

So the plan is to have two physics engines. Around every major celestial body, including planets, moons, and permanent space stations, there will be a sphere of Smart Physics, where ships can collide and such. Within these spheres, Smart Physics controls how the universe works. Everywhere outside of these spheres, the dumb physics engine that allows ships to fly right through each other will be in charge.

A beautifully drawn image of how these physics bubbles will work

This does mean that things could get silly if two ships were to encounter each other out in Interstellar Space. Maybe I'll make it so Smart Physics bubbles pop up if a player ship happens to stop in the middle of of space. But the idea is when you're traveling at warp speeds, which is even necessary to get between planets in Thorium Nova, you're traveling so fast that the likelihood you run into anything is near 0.

Believe me, I wish there were a better way, but this is the best I can think of. The crazy thing is that I can guarantee other big-name games do similar kinds of magic tricks to make everything work.

And at this point I want to just be done with it and move on. I've got so many other ideas about ship behavior, combat, long range messages, AI and large language models, faction reputations, and mission building that I want to get to. But this is such a foundational piece that I feel like I can't move on till it's done.

So "patience," I tell myself. "We'll get there eventually." Like the universe making its first apple pie, it'll all come together with enough time.