Wednesday, April 22, 2015

How I Got Around Floating Point Inaccuracies



This game is in space and space is big. It's minuscule compared to actual space, but as you can see in the video above, I encountered issues very quickly. This is not a new problem, it's happened before in Kerbal Space Program. They called it the "Space Kraken" and although it didn't tear apart any ships it still made the game unpleasant to look at.

The solution to the same issue was talked about in Squad's Unite talk. Instead of moving the ship, you move space around it. The issue with this solution was that I couldn't keep the ship's physics if it wasn't moving. It was probably possible, but I couldn't figure it out anyways.

What I did was that once the ship reached a certain distance from the center of the world, it's position is reset to (0,0,0). The world was moved by the vector from the ship's position on the threshold of escaping it's little bubble to the center of the world. This made it appear as though nothing but the had changed other than the perturbations which were gone now.

The variable chunkSize is the threshold of the "chunk" of space that can be traversed before the ship is pulled back to (0,0,0). When the distance from (0,0,0) is equal to chunkSize, the ship's position is set to (0,0,0). Furthermore, the solar system is moved by the vector bewtween the ship and the center of the world so that the solar system appears to move.