1
\$\begingroup\$

I need to figure out how to calculate the orbit of an object around one or more bodies for a game that I am making (built on Cocos2D-Swift). The game is in two dimensions, so it's a two-dimensional orbit I am calculating. I currently have it set up so that, in the update loop, it calculates the force of each of the bodies on the object, the acceleration due to that force, and the change in velocity due to that acceleration. This would probably work fine if I were going through the update loop a thousand times per second, but on the iPhone 6+ simulator, I'm going through it approximately thirty times per second, which is a discretization so wide that it makes the orbit of the object very unstable.

As a result, I am looking for an equation that tells me the position of the orbiting object as a function of time; therefore, the force, acceleration, and velocity are not discretized, so the simulation is accurate. An example of what I am looking for is how Kerbal Space Program calculates the orbital paths ahead of time. Any thoughts? Thanks.

\$\endgroup\$
1
\$\begingroup\$

For one body, you can use Kepler's laws to create an elliptical equation paramaterized by time.

For two or more bodies, you're out of luck in terms of a nice closed-form solution. See the N-body problem. However, you can still solve this problem numerically. Instead of doing what you're doing now (which is called Euler Integration), you can instead use a more stable method like Runge-Kutte 4. Here is a tutorial on how to use the RK4 method to compute orbits.

\$\endgroup\$
3
  • \$\begingroup\$ As it deals with planetary systems, then how does Kerbal Space Program calculate the paths? I suppose the developer could have calculated the path using RK4 or Velocity Verlet ahead of time, and then had the orbiting body follow the path. \$\endgroup\$
    – ben
    Feb 9 '15 at 23:32
  • \$\begingroup\$ I am not sure specifically how KSP does it, but it looks like the orbits are always ellipses, and always centered on a single object (the closest one). That means it could potentially be just using kepler's laws. The physics simulation also seems to have two modes: one that's affected by air resistance and contact forces, and the other that just travels along the ellipse. \$\endgroup\$
    – mklingen
    Feb 9 '15 at 23:41
  • \$\begingroup\$ @ben mklingen is right, KSP does use kepler only orbits. If you want to use Kepler orbits in your game, that's fine. It will still be pretty realistic. \$\endgroup\$
    – Lysol
    Feb 10 '15 at 18:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .