Problem getting correct orbits of planets

Started by
14 comments, last by Inferiarum 11 years, 6 months ago
Hey, I'm making a little solar system simulation for myself to have fun with.

Problem is, I have all the planets at the correct distance that they should be away from each other, and each given the correct average velocity
But when I apply the force of gravity 'G = m/d^2', they don't orbit correctly. All of the outside planets make their way in towards the sun and eventually get launched out of orbit forever. I've been plugging in random values into their 'mass' and trying to fake my way through it, but it's not working.

If everything is set the way it's supposed to be, mercury gets it's correct orbit, venus is a little off, earth is a little more off and etc.

What could I do to make this work the way it's supposed to? I have a book on Orbital Elements, but I'm trying not to use orbital elements, because I want it to be more dynamic, so I can add random objects that would affect everything like real life.

So what more could i do with 'G = m/d^2' to get a correct orbit for each planet?
Advertisement
Did you try setting actual values for the masses, and using the universal gravitational constant? Also, you need to give each planet the correct tangential velocity depending on where it's located at the orbit (because orbits are not perfectly circular, so the velocity of the planet is not constant).

But when I apply the force of gravity 'G = m/d^2'[/quote]
This m is the sun's mass, right? Not the planet's.

If everything is set the way it's supposed to be, mercury gets it's correct orbit, venus is a little off, earth is a little more off and etc.[/quote]
What is your timestep? If it's too big errors will quickly creep in the simulation and ruin it. This could also be due to floating-point inaccuracy, losing a bit of precision every tick.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

i feel like floating point inaccuracy is a part of the problem, but definately not all.

'm' is the mass of everything, including the sun. Gravity from all objects affect all objects.

I have no tangential velocity, i just have normal velocity being affected by gravity.

I'm using the Universal Gravitational Constant.

i scaled all the masses and the Gravitational constant just a bit to try to avoid floating point error.
I imagine that could be a problem as well?


// time step is based off the time between frames
float dt;
const double G_Constant;

Vec3 gravityAccel = (G_Constant*objMass/distSqu) * direction ;
velocity -= gravityAccel * dt;

'm' is the mass of everything, including the sun. Gravity from all objects affect all objects.

Woh woh woh, what? Do you mean you sum up the gravitational accelerations from each object, right? You can't just take the sum of the mass of everything like that.

I have no tangential velocity, i just have normal velocity being affected by gravity.[/quote]
For objects in orbit, you can define a new coordinate system based on the tangent of the object with respect to the orbit. By definition, objects in orbit have velocity only in the tangential direction. Direction matters. If you don't set the velocity in the right direction the orbit will obviously be different (although in any case it will still form an orbit if the velocity is less than escape velocity).

Vec3 gravityAccel = (G_Constant*objMass/distSqu) * direction ;[/quote]
What is objMass?? Is it the mass of the object exerting the force, or is it the mass of the object being affected?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

no no no no lol, i do this algorthm for every object there is, i don't add them all up into 1 mass ^.^ lol.

and objMass is the mass of another object, not itself, sorry for making it a little cryptic lol.

distSqu is the distance between the 2 objects squared.

I may have to look up tangential velocity then
The value of gravityAccel is actually a force, not acceleration. You need to multiply it with mass to get the proper acceleration. Also, you need to include the masses of both objects in your gravity equation. Finally, ALWAYS apply opposite equal forces, otherwise your simulation will not conserve momentum. For two interacting objects a and b; when adding force f to object a, always add force -f to object b.

Cheers,
Mike

The value of gravityAccel is actually a force, not acceleration. You need to multiply it with mass to get the proper acceleration. Also, you need to include the masses of both objects in your gravity equation.

No. F = ma. a = F/m, and the force is defined as F = G m1m2/r^2, so a = F/m = G m/r^2. This is correct. Gravitational acceleration is independent of the "receiving" object's mass.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

You could experiment a little. Take two masses and draw them traveling at some random velocity (this implies direction too). Have a toggle button that either skips the gravitational force calculations or turns it on. When the button is toggled off all objects should continue in a straight line from their last computed velocity. This is an example of one of Newton's laws, an object at rest tends to stay at rest, an object in motion tends to stay in motion unless they are acted upon by an outside force. In particular this motion is linear. Then, click the toggle button to start computing gravitational force on each of the objects and soon they should be moving on a curved trajectory, assuming their velocity has some tangential component relative to the other.

Bacterius is correct. You have to supply the correct tangential velocity.
No. F = ma. a = F/m, and the force is defined as F = G m1m2/r^2, so a = F/m = G m/r^2. This is correct. Gravitational acceleration is independent of the "receiving" object's mass.[/quote]

Yes, you are completely right - But only if you pick the right mass, and I suspect this is where the error lies. Bugs are often harder to find in optimized, compacted code, and that's why I recommend using the original Newtonian force based equation. Also, for the acceleration based calculation to work you need two equations, one for each body. A1 = G m2/r^2 and A2 = G m1/r^2. Otherwise your simulation will not conserve momentum. Sorry if my first post was a bit unclear, I was in a bit of a hurry when writing it.

Cheers,
Mike

I may have to look up tangential velocity then


Calculating the tangential velocity based on orbit parameters like distance/eccentricity:
http://www.gamedev.n...58#entry3961958

Periapsis velocity:
v = sqrt((G*M/a)*(1 + e)/(1 - e)).

The planet Mercury's semi-major axis a = 57909176e3 metres, and has an orbit eccentricity e = 0.20563069:
v = sqrt((6.6742e-11 * 1.988435e30 / 57909176e3) * (1 + 0.20563069) / (1 - 0.20563069)),
v = 58976.3015.

That's very close to the maximum orbit speed of Mercury, as 58980 m/s on wikipedia.org.

(Note, 1.988435e30 == Sun's mass)

Oppositely, for the apoapsis velocity:
v = sqrt((G*M/a)*(1 - e)/(1 + e)),
v = 38858.47.

For a circular orbit, the eccentricity is 0.

These calculations help you find the length of the tangential velocity vector. As for finding the direction of the tangential velocity vector, the cross product operation will help you (if your orbit plane is nice and aligned with the coordinate system, it's very straightforward).

This topic is closed to new replies.

Advertisement