# Calculating planet trajectory

1. Apr 25, 2012

### avpx

Hello. I am programming a 2d solar system simulation for a game. All went fine until I got to this part. Tried learning it in the past 3 days but could not understand most of it. Hoped to find help hhere.
I have the location of both planets, their mass, their initial velocity and thats it. How do i calculate their location in each point in time? Thanks.

2. Apr 25, 2012

### Steely Dan

Are these planets orbiting each other, or are they both orbiting a fixed star at the center of the solar system? The latter is a bit simpler but both are possible to do.

3. Apr 26, 2012

### avpx

they all orbit they same star. but i'd be happy if you can explain me both.
Also, what if i have a ship that can manipulate its orbit, escaping it with an escape velocity and/or crushing on a planet, how would i represent this in one single formula? Is it possible? Or will i have to make 2 different functikns that will switch between them depending on the velocity?

4. Apr 26, 2012

### Travis_King

Depends on how much accuracy you want. A star system isn't multiple two-body problems (especially when big planets are invovled), they are n-body problems. Let's say 3 for your case, Star, P1, P2. The main force of change will be the Star-P1 and Star-P2 reactions, but the P1-P2 forces will have an influence as well.

If you're just working in 2D it simplifies things, and theres a lot out there on simulating orbits.

Do you mean a ship that can change a planets orbit? You'll have to work in some nice programming if you want to incorporate that.

5. Apr 26, 2012

### Steely Dan

Fortunately, there's basically only one formula that you need to model the motion of the planets: Newton's second law, coupled with Newton's law of gravitation. This says (in Cartesian coordinates, or spherical coordinates if you want):

$$\frac{d^2 \vec{r}}{dt^2} = \frac{GM}{r^2} \hat{r}$$

where $r$ is the distance from the planet to the star, $M$ is the mass of the star, and $G$ is the gravitational constant. This equation can be solved for an explicit formula if it's something simple like a planet orbiting a star, but that's not possible if you want something more complicated like a spaceship orbiting a planet. So my first recommendation is for you to solve this differential equation as a function of time for both planets independently. These are not incredibly difficult to solve in a code but if you've never done it before, it will require a little background reading. See Euler's method and then the Runge-Kutta method.

6. Apr 26, 2012

### avpx

Thank you very much for the reply. My system is 2d system. there is a star, orbiting planets and small objects such as ships and sats that can manipulate their trajectory with some thrust. Their trajectory can be around a planet, aka orbiting, escaping the system with an escape velocity or just crushing down on a planet.
In my simulation, I plan to ignore other planet's gravity on each other to avoid using too much calculations, thus valueable processing power. Also, although my solar system is an imaginery one, I do not plan to change planet orbits. They will stay the same, at least in this version of the sim.

To the second reply. I first tried to implement the gravity law and all went well until i got to the part when i want to speed the time. The trick would work if every meter in game calculation is made and acceleration, velocity and position all updated. But if i want to jump, say 20 min later, it wouldnt work.
Thank you for the formula, I will try that.

7. Apr 26, 2012

### Staff: Mentor

If you want to make larger jumps without calculating the orbits in small time steps, things become a bit more complicated. While it is possible for the positions of the planets (the follow ellipses around the sun and you can calculate an expression which gives you the position for each planet for arbitrary times), it will not work with the small ships in the general case, as they might be influenced by the sun and a planet at the same time.

For ships in an orbit very close to a planet or with a position far away from the planets, you can use ellipses again.

However, this is all a bit more tricky then just 20 small steps of a minute each ;).

8. Apr 27, 2012

### avpx

I understand. My idea was that I the ship will succumb to only one gravity at a time, depending on its strength.
For example, instead of calculating every gravity field that each and every planet generates on this ship, It will calculate only the significant one, the strongest one. I'll see how to fix that in later versions.

9. Apr 27, 2012

### Travis_King

10. Apr 27, 2012

### Travis_King

11. Apr 27, 2012

### Staff: Mentor

As mentioned before, this is a good approximation for orbits close to the planet (orbits around the planet) or orbits far away from planets (orbits around the sun).

However, in the former case, you should calculate the position relative to the planet. This is easier and will make errors small enough to calculate even large time steps (something comparable to a month on earth).

12. Apr 28, 2012

Note taken.