Welcome, Guest

Author Topic: Universe Sandbox implementation question regarding calculations  (Read 4378 times)

Brachy

  • *
  • Posts: 3
Hey people,

This is my first post here and this is a question about the implementation of Universe Sandbox. I've seen another thread where a developer shows some code from the engine so I don't think asking this question should be a problem. This is a question aimed more at the developer but if anyone else can answer it that would be great too.

How does Universe Sandbox deal with the huge values of the masses of the Sun and planets when performing calculations? I am creating a simulation of the solar system for my University final year project and have ran into the issue that calculations with such huge values are not possible.

Do you scale these values down?

Thanks for reading.


Dan Dixon

  • Creator of Universe Sandbox
  • Developer
  • *****
  • Posts: 3244
    • Personal Site
Re: Universe Sandbox implementation question regarding calculations
« Reply #1 on: March 11, 2014, 07:45:04 PM »
What are the problems you're running into specifically?

What language are you using?

What data type are you using for your calculations? Universe Sandbox uses doubles for most simulation based values: mass, position, velocities, sizes.
http://msdn.microsoft.com/en-us/library/678hzkk9.aspx

Brachy

  • *
  • Posts: 3
Re: Universe Sandbox implementation question regarding calculations
« Reply #2 on: March 11, 2014, 09:22:27 PM »
I'm using C++ and I'm using doubles. Mass is in kg and when calculating initial velocity and Newton's law of universal gravitation multiplying the planet mass by the sun mass will either cause a crash or just be too calculation intensive for the simulation to work.

Dan Dixon

  • Creator of Universe Sandbox
  • Developer
  • *****
  • Posts: 3244
    • Personal Site
Re: Universe Sandbox implementation question regarding calculations
« Reply #3 on: March 11, 2014, 10:09:08 PM »
That shouldn't be a problem at all...

In Universe Sandbox 2 we're using meters, kilograms, seconds, and kelvin... as our base units. Multiplying a planet mass by the sun mass should totally work.

Brachy

  • *
  • Posts: 3
Re: Universe Sandbox implementation question regarding calculations
« Reply #4 on: March 12, 2014, 11:14:57 AM »
That shouldn't be a problem at all...

In Universe Sandbox 2 we're using meters, kilograms, seconds, and kelvin... as our base units. Multiplying a planet mass by the sun mass should totally work.

Yup you're correct, the issue was a NaN error, which wasn't because of the values being extortionately big (which I presumed) but was an error in the way I was calculating. Thanks for the help though, if you don't mind me asking you one more question, when working with real-life values for the distance between planets, mass, size Etc. How do you fit all of your simulation on screen? I realise you must scale down the distances somehow but I can't figure a way to do that nicely as I presume velocities and gravity must be scaled down too?

Dan Dixon

  • Creator of Universe Sandbox
  • Developer
  • *****
  • Posts: 3244
    • Personal Site
Re: Universe Sandbox implementation question regarding calculations
« Reply #5 on: March 12, 2014, 03:35:27 PM »
We do all of the calculations in what we call simulation space (using double precision values)... and then we convert everything into game space to render it on screen.

The camera, in game space, always stays at 0,0,0 and everything is moved and scaled around it (meaning we track the position of the camera using double precision values in the simulation space).

Every frame we calculate the ScaleFactor based on what we are closest to:
Code: [Select]
ScaleFactor = Math.Min(1d, 1d / (0.000001d + DistanceToClosestSurface));
And then for each planet and moon we calculate a game space position and scale:

Code: [Select]
Body.gameSpace.position = Transform(Body.Position, out gameScaleFactor);
Body.gameSpace.localScale = Vector3.one * (float)(radius * gameScaleFactor);

Here's the Transform code that's called above:
Code: [Select]
public static Vector3 Transform(Vector3D position, out float scale)
    {
        Vector3D.Subtract(ref position, ref CameraPosition, ref position);
        Vector3D.Scale(ref position, ScaleFactor);
        scale = (float)(ScaleFactor);
        return position.ToVector3();
    }

Note:
Body.Position is a Vector3D with the position of the body in meters.
CameraPosition is a Vector3D with the camera position in meters.
Vector3D is a collection of 3 doubles.
Vector3 is a collection of 3 floats.

Does that help?
« Last Edit: March 12, 2014, 03:45:57 PM by Dan Dixon »