# Calculating orbits of moons, when both the planet and star are involved.

1. Jul 7, 2012

### sydan

Hi there!
I'm developing a game that involves simulating a star system. I'm using the Unity engine and its physics engine. I've got gravity working perfect.

I've also developed an algorithm that works out how much force you would need to apply to a body to start it orbiting around another (stationary) body. It works perfectly; I've made a series of moons orbit a planet without deviation for an unlimited length of time.
The problem I now have is that when I set the planet up to orbit around the star, the moons don't stay with it, they just get left behind.

To try and overcome this problem I made my orbit calculating algorithm recursive so that each moon first works out the force it needs to start orbiting the planet, and then adds that to the force it would need to orbit the sun to try and keep it with the planet.
This didn't work, the moons don't orbit the planet as far as I can tell. All the moons and the planet start orbiting the star and gradually spread out across the orbit so that some moons are ahead and some are behind.

Just so its clear these are perfectly circular orbits. I was just wondering if anybody had any idea how to make the maths work for this? I'm a bit stumped... I can't think how to do it...
Matt

2. Jul 7, 2012

### Simon Bridge

Welcome to PF.
presumably not perfect... or you wouldn't be here.
presumably not unlimited but ho hum...
... does the moon end up falling into the star or flying off the screen?
Yes, that would be about right.

You are experiencing the pain of the three body problem. There is no analytical solution.

Can you get a planet to move in a straight line at a constant speed without leaving it's moon behind? It may be that your planets are going too fast in their orbits (or that you have a dud algorithm ... ) for the moons to keep up: have you compared the speeds with the escape velocities? Or it could be that the star's gravity is too strong and it is just ripping the moons away to itself.

Have you set up your model so the star's gravity also affects the moon?

3. Jul 9, 2012

### Rapidrain

I've been working on a java app which shows the orbits of satellites around a gravity source. I might have the smallest of tips concerning the moon, planet and star problem.

I was able to take a satellite flying at a certain velocity near a gravity source at a certain distance and moving a certain angle and determine from these whether a hyperbola or ellipse would form.
The hairy part was incorporating the fact that the the gravity source was *also* moving.

I had to take the velocity vector of the planet (the earth) and subtract it from the velocity vector of the moon (luna) and then (!!!) calculate the orbit of luna. This would make the earth look like it was standing still, and the whole math problem woud be easier. Then I added the vector of the earth back to both and continued with the animation. It caused luna to rotate around the earth but of course from the perspective of the star, watching the earth move as well, the path traced out by luna was a kind of spiral.

If your luna gets left behind perhaps its kinetic energy, rotational speed, is not high enough to stay "stuck" to the earth.

I don't know what a Unity engine is, but I hope the vector tip helps.

4. Jul 9, 2012

### sydan

It's pretty close! I put time up to 100x and left it for several hours and there was no deviation in 2 body orbits.

Fall towards the star..

I set the planet up to move in a straight line... If the moons are not given the same velocity they just get left behind. If they are given the same velocity they don't orbit properly, they lose their orbits very quickly (after about 2-3 revolutions), some of them spin off, others fall in towards the planet.

The algorithm works perfectly for stationary bodies... I can't see how I should edit it for moving ones.

I'll set about comparing velocities. The star may be ripping them off, but I'm not certain the star is the problem.

Has anyone solved this problem before?

5. Jul 9, 2012

### sydan

Awesome!

Sounds pretty close to my problem.

This sounds like a reasonable solution. I'd have to really thrash it out, but I feel you're close to the correct answer. Thanks for the hint :)

I'm not too experienced with how kinetic energy will affect the situation. What's the maths for it?

The unity engine is just a game that supplies a preconstructed physics engine for you, it covers basic mass, forces, friction, torque, etc. If you want it to be more complex you have to add to it.

Thank you for the reply! I'll get back as soon as I've tested further.

6. Jul 9, 2012

### sydan

Did some more tests. I found that moons orbit planets that are moving with consistent velocity completely fine if there's only one or two moons involved. Lots of moons cause too much disturbance for orbits to be maintained.

Moons and planets rotating around a sun doesn't work at all, even if there's only one moon. So perhaps the gravity of the sun is stripping the moons off.

7. Jul 10, 2012

### Rapidrain

Well said. This is what I've been thinking for the past 18 months.

There is very much math involved in the orbit calculations. First and foremost : conservation of energy. KE + PE is always the same.

KE = v*v/2 and PE = -GM/d

So you have two equations but dx/dt (change of distance over time) is v and x is d (distance) so you have an integral to solve :yuck: which I gave up on quite soon. You see, initially I thought I could find a function of distance in a two dimensional space = f(t). The stuff I just mentioned however gets you only a time = f(distance) in a one dimensional setting and that is all.

Look up the vis-viva forumula from Isaac Newton and study the geometry of an ellipse. Look at the dimensions of the ellipse and the major axis, minor axis, eccentricity and position of the focus points. Find out the ratio of the velocities between the periapsis and apoapsis. Find out how Kepler calculated the orbital period. Orbital mechanics are really interesting.

I don't want to take the fun out of it from you so I won't bore you with all the details of my iteration loops etc. Here's a screen shot of what I've got so far.

With my little project I just recently had to solve a a cubic polynomial with the code. That was cool. All kinds of strange things have to get solved. I bet if I continue with it I'll certainly find a Higgs Boson lying somewhere in it by next spring.

#### Attached Files:

• ###### SpaceFlight 110.jpg
File size:
34.4 KB
Views:
174
8. Jul 10, 2012

### mikeph

The motions are chaotic, so any step-wise integration based on a set of initial conditions will tend to give you errors that grow over time. I can guarantee you you don't have gravity working "perfectly"!

Try making the mass of the moons extremely small compared to the other two. This is equivalent to the "restricted" three body problem and it should lead to better stability in the simulations. The moon will still orbit semi-realistically because it's still a free-falling mass, the difference will be that the motions of the sun/planet will be independent of the position of the moon.

Also if you have any control over the physics it is useful to plot the total energy of the system as a function of time because this will give a barometer of the inaccuracy of the simulation. If it varied by over 10%, for example, you know things are getting more and more divergent from reality.

9. Jul 13, 2012

### sydan

Hi, thanks for the comment. I haven't had time to test much but I've found that the total energy of the system gradually decreases. This sort of makes sense as there's no such thing as heat or sound in the simulation so collisions are approximated. But generally I can't tell if the decrease is too much yet.

To the general problem... I might have to cheat and fix it using some sort of cover up. I can't think how to get the whole thing to work otherwise.