# Orbit Simulator

walter
I would like to start a fun project to create a vb6 program to create a orbit simulator.

Is there anybody that's done this that could give me some tips?

## Answers and Replies

Staff Emeritus
Gold Member
It should be a simple matter. At each time step, calculate the net force, and thus the new velocity, of each of the bodies.

- Warren

Staff Emeritus
Gold Member
Welcome to Physics Forums walter!

It's probably a good idea to start with some simplifying assumptions.

For example, treat the Earth as a homogeneous sphere, and consider it the only massive object (other than the satellite - I'm assuming you want to simulate the orbits of satellites around the Earth). You could also ignore general relativity for your first few versions.

Gold Member
Dearly Missed
walter said:
I would like to start a fun project to create a vb6 program to create a orbit simulator.

Is there anybody that's done this that could give me some tips?

I did, years and years ago. Probably chroot and nereid did too but they are not volunteering the information

Did you ever write a bouncing ball program?

Nereid is right about simplifying. For starters make the problem real real simple.

the Earth is just a point with a certain mass

the satellite is just a dot going around the earth

you even can work ONLY IN TWO DIMENSIONS so all vectors are just two numbers, an x component and a y component

you fix on some time interval like one second, or ten seconds, or a minute

at each iteration you calculate the acceleration of something at the sat's position

you add that to the existing velocity to get the new velocity

you see how his position changes (in one time interval) using that new velocity

you find his new position

then you go around the loop again (find new accel. find new veloc. find new position, find new accel find new veloc find new position...)

doubtless there are refinements but what the heck this will make the dot move on your screen!

Staff Emeritus
Gold Member
Unless you go with either an extremely small timeslice or only a few orbits, there is a chance that using the Euler method (described by marcus) will not give you a closed orbit. Errors in the timestep cause the orbits to fall outwards. You may need to implement a runge-kutta method for long duration simulations.

Gold Member
Dearly Missed
enigma said:
Unless you go with either an extremely small timeslice or only a few orbits, there is a chance that using the Euler method (described by marcus) will not give you a closed orbit. Errors in the timestep cause the orbits to fall outwards. You may need to implement a runge-kutta method for long duration simulations.

do you remember the bouncing ball in "Men in Black" which keeps picking up energy?

Tommy Lee Jones and Rip Torn have to duck

you will find out lots of interesting things

small numerical errors can accumulate and the orbits can blossom

I urge you to try ASAP to write the simplest orbit program and then
you can refine it if you want

walter
I'm willing to start with a 2d model, but I was thinking about planetary orbits.

I guess I can start with the Sun & Earth.

Gold Member
Dearly Missed
walter said:
I'm willing to start with a 2d model, but I was thinking about planetary orbits.

I guess I can start with the Sun & Earth.

great

you will be able to specify some initial conditions in your program

like say the Earth starts 100 units out
and you can specify the initial speed and direction that it is moving

perhaps you can specify 4 numbers
positionX, positionY, velocX, and velocY

perhaps you decide to start with posX = 100, and posY = 0

and perhaps velocY = 1
with velocX = 0

then the program will take over and move the dot around on the screen

please tell us when you have something running

it may be possible later to refine it so it can work better or to have several planets or make it work in 3D

Staff Emeritus
Gold Member
What language are you planning on writing this in?

walter
My plan was to use visual basic 6.0

litghost
I think using a polar rendering method would assist in making simple systems(it would also allow for easy modeling to 3d(simply provide another angle)). Of course this is just a random idea.

walter
Ceptimus, I pretty much wanted to do what you did on the solar system simulation on your home page.

Could you give me a couple of pointers where to start?

ceptimus
You need to specify the masses, initial position, and initial velocities of all the planets. The only other things you need are 'Big G', the gravitational constant, and as you don't really want to wait for a whole year for the Earth to go round once, you need a time multiplier (how many times faster than reality the simulation is to run).

If you do 'view source' from your web browser while my sim is displaying, you will see how they are defined for my program (and you can play with the values if you copy the page to your own machine).

The program works in time slices. You only need the simplest of calculations to work out the new position and velocity of each body after each time slice.

The force, f, between any two bodies, M1, and M2 separated by distance d is:

f = G * M1*M2 / d * d

You can totalize all the forces acting on a body from each other body - the easiest way is if you resolve the forces into X and Y (and Z if you work in 3D) components.

The acceleration, a is given by a = m / f

The new velocity v, from previous one u is given by v = u + a * t

New position is just the old position plus the velocity times the time (you can use either the old or the new velocity - it doesn't really matter if the time increment is small).

I'll send you the source code if you wish (it's in java, which is similar to C) Drop me a PM if you want it.

A Visual Basic implementation should be pretty easy to do - I only did it in java, rather than some other language, as I wanted to stick it on a web page.

Last edited:
Homework Helper
ceptimus,
Do your simulations incorporate GR? (I don't speak java, C, or probably any other language you can think of, so I don't think I would be able to figure it out from there.)

ceptimus
No turin. It only uses Newtonian mechanics, and even that is only approximated - the velocities and positions are only calculated at each time interval, so the orbits would actually consist of many small straight line jumps, if you could zoom in enough to see them up close.

Providing the time intervals are short enough, relative to the orbital period though, this is quite accurate enough, and even NASA uses such methods to calculate the trajectories of its space probes.

You only really get a noticable difference between Newtonian calculations and General Relativity ones when the speeds become an appreciable fraction of the speed of light - and for situations where the speeds (compared to light speed) are low, Newtonian calculations are much simpler and faster to do. :)

Last edited:
Gold Member
walter said:
I would like to start a fun project to create a vb6 program to create a orbit simulator.

Is there anybody that's done this that could give me some tips?
Sorry for pulling up such an old thread, but I've only been a member of this facinating forum for about 2 weeks. But I did exactly what you described. I wrote an orbit simulator in VB 6.0. You can download it here: www.gravitysimulator.com

It performs its calculations on a 3d universe. It uses Euler's method, a first-order method. I'm hoping to post a new version in about a month or two that will use a Runge-Kutta4 engine as well.

Walter, it doesn't look like you've posted since April, so I doubt you'll read this, but if you do, and you still want to tackle such a project, let me know and I'll be happy to help you.
enigma said:
Unless you go with either an extremely small timeslice or only a few orbits, there is a chance that using the Euler method (described by marcus) will not give you a closed orbit. Errors in the timestep cause the orbits to fall outwards. You may need to implement a runge-kutta method for long duration simulations.
Actually, the first order Euler method works remarkably well. With a timestep as large as 16767 seconds per time step, the solar system holds together for over a million years. Probably more, as I ended the simulation at 1 million years because it took my computer a month to do. I can speed this up to 65536 if I remove moons from the solar system. Faster than that causes Mercury to deviate from its orbit.

In fact, the Euler Method is accurate enough that if I use JPL's Horizons system to accurately place the planets in their January 1, 2003 position, Mars and Earth pass each other at exactly the right time and distance as the real Mars did August 2003 when it made its closest approach in 60000 years. Running the simulation further into the future, Mars also passed Earth at the correct time and distance for its next opposition (2005? I think). But the 2007 opposition came about 2 days early or late, but at the right distance, and the 2009 or 2010 opposition was about 7 days off its predicted date, but the correct distance. I'm not sure if Runge Kutta would give better results here. I might just need a more accurate value for G or for Sun mass.

In the post titled Sedna in this forum, check posts an article about the origin of Sedna's orbit. Doing a Google on the scientist's name in that article I located his paper at http://www.boulder.swri.edu/~hal/PDF/CR105.pdf . I was able to use my program to reproduce his simulation of a 0.05 solar-mass brown dwarf with bodies orbiting in the 20-100 AU range, passing 200 AU from the Sun with a starting velocity of 1km/s. My simulation unfolded almost exactly like theirs did. 8 of the 20 test particles orbiting the brown dwarf entered solar orbits after the passage.

The new version I hope to release soon is over 10x faster because I've cleaned up the code and put the Euler engine in a C++ dll. It may be even faster still if I can get my RK4 engine working.

Staff Emeritus
Here is what I would suggest, as far as the general layout of your program goes.

1. An initial conditions input module, which accepts the planets masses, initial velocities, and positions
2. A module which generates the equations of motion. I would suggest using the Hamiltonian formulation of the equations of motion if at all possible, unless the math turns out to be beyond your level.
3. A differential equation solver module. Use a standardized interface so you can replace this module with different variants as the program evolves. See the notes below about using a "canned" differential equation solver.
4. A plotting/graphing display module, which displays the results.

As far as the differential equation solver, I'd recommend starting with an out-of-the-book Runge Kutta method. Apparently there are no "Numerical Recipies in Visual Basic" books. This isn't necessarily the best alogrithm, but it's rugged and standard and "good enough", and once you get it up and running, you'll be in a position to plug in a different algorithm using the same interface if you really want to.

http://www.vb-helper.com/books_recommended.html#numerical_algorithms [Broken]
suggests that the "Numerical recipies in Fortran" will be the easiest to translate into visual basic. If you've got the money, buy the book, but if you don't, get a copy from the library.

I would suggest getting your numerical integrator to correctly solve

dx/dt = x. (The soluiton is of course x=exp(t)).

before attempting to actually use it to generate orbits. The next step is to get it to solve the equations of motion for a circular orbit.

Because it's canned, I'd suggest starting with the differental equation solver. Using the data structures of the canned solver will impact the design decisions made in the rest of your programming project.

Last edited by a moderator: