tags:

views:

447

answers:

4
+7  Q: 

Orbital Mechanics

Does anyone have an example of implementing Orbital Mechanics (preferably in XNA)? The code I am currently using is below, but it doesn't "feel right" when it executes. The object just bends ever so slightly to the planet, and no matter how much I tweak the variables I cant get it to enter an orbit, or even a partial orbit.

shot.Position += shot.Velocity;  

foreach (Sprite planet in planets)  
{  
  Vector2 directionToPlanet = (planet.Position - shot.Position);  
  directionToPlanet.Normalize();  

  float distance = Vector2.DistanceSquared(shot.Position, planet.Position);  

  float gPull = (float)(planet.gravityStrength * (planet.Mass * shot.Mass) / distance) + planet.gravityField;  
  shot.Position += new Vector2(directionToPlanet.X * gPull, directionToPlanet.Y * gPull);  
}

Edit Marking Mendelt's answer correct for pointing out that I need to update the velocity, not the position. I also needed to change the calculation of gPull to

float gPull = shot.Mass * planet.Mass / distanceSqr * planet.gStr;
+4  A: 

Newton-Raphson iteration is not a stable way to solve this problem (that is you can't get it right using so simple an integrator for the differential equation). Consider using a second (or higher) order solution: Runge-Kutta is good and is fairly easy to implement in this case.

From the point of view of numeric analysis, the problem of orbital mechanics reduces to that of solving the set of coupled differential equations:

x_i'' + G m_i \sum_{i != j} m_j r_ji/(|r_ji|)^3 = 0

where the x's are three-vectors representing the positions of the bodies, the m's are the masses of the same bodies, and r_ji = x_j - x_i is the vector displacement between bodies j and i.

dmckee
True, but you can get a few orbits to happen without resorting to Runge-Kutta. It just won't stay in the same orbit as long as it would in the real world, as the errors accumulate rapidly.
Daniel Earwicker
@Earwiker: Yep. It's been a long time, but seem to recall my NR orbit simulator accumulating an radial error of a few percent per orbit. But it also breaks the conservation of energy and angular momentum. Us physics types find that *very* frustrating...
dmckee
Oh I know, I'm a physics type myself! :)
Daniel Earwicker
Of course, a gradually changing orbit doesn't necessarily break conservation laws in the real world - for example, the Moon is much further away from the Earth than it was a few billion years ago (due to tidal effects rather than approximate integration).
Daniel Earwicker
Yeah, in the Earth-Moon case, the energy and angular momentum come from the Earth's spin. No problem there.
dmckee
Runge-Kutta actually is not a consistent method either. It seems perfect, but upon deeper analysis you can show that it doesn't conserve fundamental quantities that much simpler codes will conserve if you simply use finer time/resolution steps. That being said, for most purpose it is a great choice.
Alex
@Alex: Correct, but the problem is pushed back by a couple of terms.
dmckee
+7  A: 

In the last line you're updating the position of the shot. You should be updating the velocity.

You might want to take a look at the code in this blogpost http://blog.mendeltsiebenga.com/post/Fun-with-planets.aspx No xna, but working orbital mechanics. (although i never got rid of the screen-flicker)

Mendelt
interestingly enough, that gives me a nice sin wave...not exactly what I'm looking for, but that is a result of my equation
Joe
@Joe: If you plot a single component of position (say x) against for an object in a circular orbit, a sine wave (plus possible phase) is what you expect...
dmckee
+2  A: 

A) We have no idea what your input values are.

B) You might want to use a better approximation than Newton-Raphson.

C) Passing objects do not generally fall into orbit IRL, gravity is extremely weak, it takes equally weak velocities or truly exceptional masses to get much more than curvature.

annakata
+2  A: 

A passing object will not enter orbit. One characteristic of an orbit is that you will return to the same point (relative to the body being orbited) with the same velocity. If you started from effective infinity, you'll go back to effective infinity.

In order to enter orbit, you will need to change the velocity at some point in a way unrelated to gravity, or perhaps have additional large bodies. Similarly, you can't launch an object into orbit from the surface: you have to have something (like a last rocket burn) once the satellite reaches the desired altitude. Otherwise it will try to return to the launch point, which is on the surface.

Some of my worst debugging experiences were when the program was fine and my test data or calculations were off. Make sure you know what to look for.

David Thornley
What's the difference between "a passing object" and "an orbiting object" if they are going the same velocity at the same distance from the other body?
Daniel Earwicker
My impression was that the "passing body" was coming from far away. The difference between that and an orbiting body is of course velocity. I could have been misinterpreting the OP, of course.
David Thornley
Pretty sure you're wrong about this, since Triton is believed to be a captured satellite—it was passing; now it's orbiting. If I understand correctly, no matter how far away an object comes when it nears a more massive body, it may enter orbit if the velocity and distance are right.
P Daddy
Simply put, if you take a string and tie the ends together, you have a loop, but if you tie one end to the middle of the string, you still have a loop. To orbit, you need not return to your point of origin.
P Daddy
@P Daddy: Gravitational interaction are fully time reversal invariant. For a "passing body" to be captures *requires* a thrust or an interaction with a third body. And all two-body gravitational interaction paths are conic sections (in the COM frame).
dmckee
@P Daddy: If the velocity and position are right, the object will be in orbit. It won't be right if the object comes from effectively infinity. This is of course in the two-body case. It is possible to capture in more complex cases.
David Thornley
You guys are way over my head, with the COM stuff, and all (I hate COM!) :·)
P Daddy