Welcome, Guest

Author Topic: New planets and initial velocity.  (Read 3334 times)

sknnywhiteman

  • *
  • Posts: 2
New planets and initial velocity.
« on: January 03, 2013, 01:45:07 PM »
I'm creating my own "Simulator", but it's not quite the same as this game. The point of this is to see gravity in action, while my simulator will be a little slower paced, and it'll have an overall "goal". One of the features I'm adding though, is randomly generating parts of a universe with galaxies, super clusters, ect.. What I noticed while playing with your game was you do a really good job at creating initial velocity with your newly spawned planets. I was wondering if you were willing to share how you do that? It works really well from what I can see, and that's the only part missing from my generation code.

Also, what is Universe Sandbox primarily written in?

Dan Dixon

  • Creator of Universe Sandbox
  • Developer
  • *****
  • Posts: 3244
    • Personal Site
Re: New planets and initial velocity.
« Reply #1 on: January 03, 2013, 03:43:29 PM »
Universe Sandbox is about 75000 lines of VB.NET and uses the Truevision3D engine.

The new version (still in development) is being written in C# and Unity3D.

Here's the C# code that we're using in Universe Sandbox 3 to calculate 2D orbits:

Code: [Select]
public static Vector3D CalcOrbit2DVelocity(Vector3D position, Vector3D parentPosition, Vector3D parentVelocity, double parentMass, double gravity)
{

Vector3D NVec = new Vector3D(0, 0, 0);
Vector3D DirectionOfPlanetFromParent = (position - parentPosition).Normalize();

double DistBetween = Vector3D.Distance(position, parentPosition);
double ForceForOrbit = 0;

if (DistBetween > 0)
ForceForOrbit = Math.Sqrt(gravity * parentMass / DistBetween);
else
ForceForOrbit = 0;

NVec.x = Math.Abs(DirectionOfPlanetFromParent.z);
NVec.z = Math.Abs(DirectionOfPlanetFromParent.x);

NVec.x *= ForceForOrbit;
NVec.y = 0;
NVec.z *= ForceForOrbit;

if (parentPosition.x > position.x)
{
NVec.z = -NVec.z;
}
if (parentPosition.z < position.z)
{
NVec.x = -NVec.x;
}

NVec += parentVelocity;

return NVec;
}
« Last Edit: January 03, 2013, 04:20:48 PM by Dan Dixon »

sknnywhiteman

  • *
  • Posts: 2
Re: New planets and initial velocity.
« Reply #2 on: January 03, 2013, 04:13:35 PM »
Okay. So, the main equation going from that code is
ForceForOrbit = Math.Sqrt(gravity * parentMass / DistBetween);?
And it's the acceleration?
And it's also really awesome for a company semi-commercial like you (making an income off of this title) to just share your source code like that. Most game developers I talk to are very secretive, and just give examples of pseudo-code, forcing me to make my own when the source code they use is perfectly fine. Thanks so much!

Also, I saw the Universe sandbox 3 demo video, and I really like it so far. It's looking good, and hopefully you'll be able to make something awesome with it. Best of luck!

Dan Dixon

  • Creator of Universe Sandbox
  • Developer
  • *****
  • Posts: 3244
    • Personal Site
Re: New planets and initial velocity.
« Reply #3 on: January 03, 2013, 04:23:49 PM »
...the main equation going from that code is
ForceForOrbit = Math.Sqrt(gravity * parentMass / DistBetween);?
And it's the acceleration?

Yes... And notice how we swap the x and z values here:
Code: [Select]
NVec.x = Math.Abs(DirectionOfPlanetFromParent.z);
NVec.z = Math.Abs(DirectionOfPlanetFromParent.x);

Where y is up and x, z are on the plane of the orbit.

And you're very welcome...

And feel free to post about your project in the forum... I think the readers here would find it interesting.