Welcome, Guest

Author Topic: Making my own simulator?  (Read 4140 times)

atomic7732

  • Global Moderator
  • *****
  • Posts: 3849
  • caught in the river turning blue
    • Paladin of Storms
Making my own simulator?
« on: January 05, 2009, 06:24:48 PM »
 ??? ??? ??? ??? If you could help me with how to start a program, just a simple one like your "planets" one. It doesn't even have to be in 3D, 2D would satisfy me for now. Unless, you wanted to tell me. I only want to make simple. Like 10-20 Objects, solid color... run on XML files (if possible)...
« Last Edit: January 05, 2009, 11:23:32 PM by Dan Dixon »

Dan Dixon

  • Creator of Universe Sandbox
  • Developer
  • *****
  • Posts: 3244
    • Personal Site
Re: Making my own simulator??? (DAN!!!! I NEED HELP)
« Reply #1 on: January 05, 2009, 11:23:12 PM »
Good times. A simple gravity simulator isn't too difficult get working.

Screenshots of my earlier work ('Planets') can be found here:
http://dandixon.us/programming/planets.htm

What language will you be writing this in?

And turns out there's little difference between 2D and 3D as far as the math is concerned.

You're going to want to create a class for each body. This needs to have variables to store the X,Y,Z position and X,Y,Z velocity of each body along with the mass. Diameter, Color, Texture, etc... are all optional.

You'll start by either hard coding the initial positions and velocities or loading them from a file.

Then for each frame of the simulation you'll calculate the change in velocity of each body on every other body:

Here's some pseudo-code:

Code: [Select]
For A = 1 to Total#OfBodies
  For B = 1 to Total#OfBodies
    If A is not equal to B then
      D = Distance between bodies A & B
      Force = G * MassOfB / D * D
      VelocityChange.X = VelocityChange.X + (Force  * (X distance between bodies A & B) / D))
      VelocityChange.Y = VelocityChange.Y + (Force  * (Y distance between bodies A & B) / D))
      VelocityChange.Z = VelocityChange.Z + (Force  * (Z distance between bodies A & B) / D))
  Next B
  BodyAVelocity = BodyAVelocity + (Fv * TimeScale)
Next A

G = Gravity Constant
I use:   6.6725985 * (10 ^ (-11)) * (59736 * (10 ^ 20))    which is the 'real gravity constant in kg' * the mass of earth (since I store all masses where 1 = Earth)
Starting out you can just set G = 1 or .00001 or something like that.
TimeScale is the length of time that each frame represents. Again, just hardcode this to be 1.

Then once you've calculated each body's new velocity for that frame you add it to its current position:

Code: [Select]
For A = 1 to Total#OfBodies
  BodyAPosition = BodyAPosition + (BodyAVelocity * TimeScale)
Next A

Then you draw the scene using the new positions that you just calculated.

Does that help? Just ask if you have any more questions. I'd enjoy seeing what you come up with.

atomic7732

  • Global Moderator
  • *****
  • Posts: 3849
  • caught in the river turning blue
    • Paladin of Storms
Re: Making my own simulator?
« Reply #2 on: January 06, 2009, 03:17:23 PM »
Do I just put those into an .exe or whatever? I never made a program before. Do I edit what you typed or what. I'm going to get a friend that knows a little bit of programing. If I show him this he MIGHT be able to help me.
« Last Edit: January 06, 2009, 03:28:28 PM by NeutronStar »

Dan Dixon

  • Creator of Universe Sandbox
  • Developer
  • *****
  • Posts: 3244
    • Personal Site
Re: Making my own simulator?
« Reply #3 on: January 06, 2009, 05:37:47 PM »
If you've never programed before than a gravity simulator might be a bit complicated to take on as a first project.

If you'd like to learn about programming check out Microsoft's Small Basic. It's designed for beginners and is a great way to get started:
http://msdn.microsoft.com/en-us/devlabs/cc950524.aspx


atomic7732

  • Global Moderator
  • *****
  • Posts: 3849
  • caught in the river turning blue
    • Paladin of Storms
Re: Making my own simulator?
« Reply #4 on: January 06, 2009, 07:21:05 PM »
ok thanks