Report article RSS Feed Space is BIG in SubLight

If you like the technical nitty-gritty details about game development, then you're in for a treat!

Posted by Spacew00t on Jul 26th, 2013

Space is a BIG place!

I knew going into SubLight that space was big, and I wanted to show people just how big it is. Little did I realize how much I myself would learn about the scale of the universe! For starters, we can say that the universe is about 15 billion light years in radius ( give or take a few billion). Okay, well that's big, but it's still pronounceable, not too intimidating, right? I mean, some people probably have that much money!

This is the deception that light years lull us into, because we assume a light year is a reasonably sized unit. In fact, it's unbelievably MASSIVE! It's so astonishingly large, that it's almost meaningless outside of scientific measurements. The problem I ran into while simulating universes in SubLight is the huge disparity between interplanetary distances, interstellar distances, and intergalactic distances.

The Float

When programming a game, you have to decide what your smallest measuring units represent, and in doing so, determine the max distance you can go from the origin of your Universe (since your computer often handles data in predefined ranges). Floats are often used, because they conveniently store decimal numbers, but they are inaccurate at large values. Because of the special way they store information, they allow a large range, but only at a certain accuracy. Once you get above a certain number, their inaccuracy is greater than 1. That means statements like this will return true:

10,000,000,000,000,000 - 1 == 10,000,000,000,000,000

Basically, the computer will think that ten quadrillion minus one equals ten quadrillion!
Still though, ten quadrillion, who needs a number that large? Unfortunately, SubLight does. If we wanted to simulate our universe, each of those units would have to equal more than 14,000,000 kilometers! That means the smallest distance our ship could ever travel is 36 times the distance the Earth is from our Moon! You’d be leaping all over the place, unable to orbit planets, dock with space stations, or even keep the camera smoothly tracking the ship! We’re looking for at least centimeter or millimeter accuracy here, the smallest amount someone could reasonably notice in game.
Okay, so if 10 quadrillion units isn’t enough, what is?

The Int

If you want real accuracy, you need to use a type of data that has rigidly defined limits. Although this means your units won’t be able to be as large, at least they’ll never be inaccurate! First, we have int, with a max value of 2,147,483,647, but as we’ve already established, 10 quadrillion isn’t enough, so 2 billion definitely won’t be enough!

The Long

The next option is the long, and with a max value of 9,223,372,036,854,775,807, or more than 9 quintillion. This sounds good! I had to look up what the name of it was, so surely that must be long enough to reliably simulate the universe. Well... unfortunately no, even that massive number only gives us accuracy down to 15,000 kilometers. Better... but nowhere near the millimeter accuracy we’re aiming for. What now? There aren’t many bigger datatypes, and since long got us most of the way there, why not two longs?

Assuming the first long represents everything to the left of the decimal point, and the second long is everything to the right of the decimal point... that could work!If the first long represents light years, and the second long a fraction of a light year, that gives us accuracy almost down to the millimeter, finally!

The actual value is closer to 1.02571254 mm per 0.0000000000000000001 units of our system, but damn, at least we can get accuracy out to 9,223,372,036,854,775,807 light years! Oh, wait... that’s a lot of light years... we were only shooting for about 15 billion! No problem, by changing 1 of our units to represent a gigameter (1 million kilometers) instead of a light year, we get 0.1 nanometers of accuracy out to 100 billion light years! Hot damn, we’ve got accuracies way smaller than we need at distances more than 5 times what we wanted!

What’s the cost of all this to your computer? At the end of the day, not much. Since floats take up about 32 bits, and our new two long data type is 128, it’s only a 4x increase in storage space for more than 900x the maximum distance. Since the universe in SubLight is procedurally generated, we can do trickery that requires us to only store the systems we’ve visited and interacted with. That means we can store the positions of a million systems in less than 16 megabytes (and I very much challenge you to visit a million systems!).

As for its cost on the CPU, I can only venture the vaguest guess that it probably won’t affect performance much, since there’s only a couple thousand of these two long units being messed around with every frame.

I know this was a rather dry and technical post, but hopefully it can shine some light on the issues of simulating distances on such an intergalactic scale! And remember, if you'd like to help fund development, you can buy the alpha here (or just download it for free in the downloads section!)

Special thanks to this article, which inspired this post, and helped me solve some of the problems I was having.

Post comment Comments
Sph!nx
Sph!nx Jul 26 2013, 7:35am says:

Very informative. Thanks!

+3 votes     reply to comment
isbeorn
isbeorn Jul 26 2013, 8:11am says:

Entertaining post :)

+3 votes     reply to comment
BioDestroyer
BioDestroyer Jul 26 2013, 11:15am says:

Indeed that's a pretty big space, tracking.

+3 votes     reply to comment
hagamablabla
hagamablabla Jul 26 2013, 11:16am says:

The magic of binary storage. 4 times the storage space gives you 900 times the data.

+3 votes     reply to comment
Galacticruler
Galacticruler Jul 26 2013, 11:38am says:

“Space is big. You just won't believe how vastly, hugely, mind-bogglingly big it is. I mean, you may think it's a long way down the road to the chemist's, but that's just peanuts to space.”
― Douglas Adams, The Hitchhiker's Guide to the Galaxy

Honestly, I don't see why the space has to be THAT big, unless you plan for #% S.o.L. engines.

EDIT:
Also, another thing about SL I'd like to know is how flexible is it? Can it be modded easily or would that require and SDK or code hack?

+2 votes     reply to comment
Spacew00t
Spacew00t Jul 26 2013, 11:43am replied:

That quote sums it up pretty well!

Also, I'd like space to be that large since the game is about generations of people traveling through space, meaning you can fast forward time until they reach their destination.

EDIT:

Just saw your edit, I'd like to make adding new parts as easy as it is in Kerbal Space Program, so pretty easy! Right now that's a lot of work, but it's definitely a priority.

+1 vote     reply to comment
flamming_python
flamming_python Jul 26 2013, 12:24pm says:

Instead of the 2 long data type - you should try playing with logarithms instead. The vast majority of the time you'll be travelling between stars/star systems/galaxies - where milimetre accuracy is hardly needed because there is nothing but empty space there.
You only need the milimetre accuracy when you are in a star system.
Think about it.

+1 vote     reply to comment
Spacew00t
Spacew00t Jul 26 2013, 12:33pm replied:

Another possibility is using octrees, but using 2 longs is honestly simpler to implement and debug.

+1 vote     reply to comment
Pelf
Pelf Jul 26 2013, 6:20pm replied: Online

How would the 2 longs system even be implemented?

+1 vote     reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

News
Browse
News
Report Abuse
Report article
Related Games
SubLight
SubLight Indie Single Player Futuristic Sim
Related Engines
Unity
Unity Commercial Released May 30, 2005
Related Groups
Lunra Games LLC
Lunra Games LLC Developer with 2 members