Jump to content

Trajectory Plotting Algorithm


steel999

Recommended Posts

I was looking into a way to plot a space trajectory (like KSP does) for a project of mine but was unable to find anything other than complex physics writings.

Is there any chance I could get a push in the right direction?

How did the developers for KSP research this?

Link to comment
Share on other sites

I was looking into a way to plot a space trajectory (like KSP does) for a project of mine but was unable to find anything other than complex physics writings.

Is there any chance I could get a push in the right direction?

How did the developers for KSP research this?

I'm not sure that this is entirely the right sub-forum. A mod may want to move it.

In short, you need to look up orbital mechanics. Watch a few videos, read a few articles, and you should eventually get the hang of it. I can explain the gist of it and how it fits into KSP below, with lots of links you can look up on Wikipedia (though I recommend searching for beginners' tutorials for these subjects as well). There's no real way to avoid the physics, as it is physics, though hopefully the perceived complexity can be reduced.

Ignoring general relativity, the motion of bodies in space, such as planets, moons, stars, spacecraft etc. can be described by Newton's law of gravitation. This is where the force between any two point masses (or spherical bodies, which is a good enough approximation in most cases for real objects) of mass m and M is

F = (G x M x m)/(r^2)

where F is the force you apply to both bodies in the direction of the other body, G is the gravitational constant, m is the mass of one body, M is the mass of the other, and r is the distance between their centres of mass.

Here is another visualisation from Wikipedia with different names for the variables (note that F = F1 = F2):

NewtonsLawOfUniversalGravitation.svg

To calculate the motion of a spacecraft, you therefore have to integrate this force over time between the spacecraft and every other body in the universe. In general this is impossible to solve analytically, even if the universe only contained 3 objects. KSP simplifies this a lot by only considering one body other than the spacecraft - this is known as the 2 body problem. In this setting the evolution of the motion of the spacecraft over time due to Newton's law can be solved exactly. You can simplify this further because since the planet/moon etc. is normally much more massive than the spacecraft, the tiny forces involved will not affect it much, so you only need to consider the forces on the spacecraft. The result is that it will always follow what is known as a conic trajectory, relative to the body, with the body at one of the foci. This trajectory, known as an orbit, forms the shape of either a circle, an ellipse, a parabola or a hyperbola, as pictured below:

200px-OrbitalEccentricityDemo.svg.png

The body is at the point F, and the spacecraft follows one of the coloured lines. A circular orbit is shown in grey, an elliptical one in red, a parabolic one in green and a hyperbolic one in blue.

KSP takes advantage of this exact solution by using it instead of calculating the forces on the spacecraft, which allows you to predict the motion of the spacecraft into the far future with little to no extra computation. This is why KSP's calculations are so robust. The downside, of course, is that the result is only absolutely correct for a system with only one major body. It can approximate more heavily populated systems (like in KSP) by limiting the active range of the gravitational pull of bodies depending on the strength of their gravity compared to their surroundings. This active range forms a region known as the Sphere of Influence (SoI). This provides a means for determining which single body to choose as the other body in the 2-body problem with the spacecraft, as you have no doubt experienced when crossing into the Mun's SoI, for example. What is (approximately) happening is the game is always checking which body has the most effect on the motion of the spacecraft, and it switches to whichever that is as necessary.

Edited by ZRM
Link to comment
Share on other sites

what do you mean by "plot" exactly?

are you asking about the physics aspect of figuring out a trajectory over time? or do you mean drawing in onto 3d space?

should you mean the latter, KSP uses this thing called Vectrosity for doing that...

Otherwise, there are few ways of answering your question other than a big ball of math :rolleyes: -- It's not really all that complicated, it just looks that way at first, tho.

cheers

Link to comment
Share on other sites

you can integrate as many objects as you have cpu time for. just get the vector sum of all the forces you want to account for and use that for your current integration step. for a spacecraft you dont have to account for mutual attraction for planetary objects, because the effect by a spacecraft on large bodies is infinitesimal, and you get a result that is close enough. for objects with large amounts of gravity, you must account for the effect of that object on all other objects (and those objects on eachother), and thats where the impossibility comes from, an endless recursive loop.

Link to comment
Share on other sites

you can integrate as many objects as you have cpu time for. just get the vector sum of all the forces you want to account for and use that for your current integration step.

Not that simple. The problem is that the integration method matters a lot. Euler just isn't going to cut it. In fact, any explicit Runge Kutta method, including Verlet which is so popular for physics simulation, are going to give you very poor results. The 1/R potential of gravity is very tough to integrate over. Any explicit method is going to end up accumulating kinetic energy errors. And because solution is very sensitive to object's velocity, you are going to end up with a very large error in trajectory.

Unfortunately, the only way to get a good trajectory computation for a ship affected by many attractors, even if later are assumed to be on rails, is to run an implicit method. Typically, 2nd or 3rd order Gauss-Legendre method is used.

I did some tests a while ago when the news of the comet heading for Mars first appeared. I managed to pull NASA data for the comet and all of the important massive bodies in the Solar system to see if I can run my own integration routine for predicting the closest approach. Even using Verlet with 1 second steps, after just a few months, the trajectory was off by enough to make it useless, and velocity was off by a few percent.

If you are going to make a video game, it's not such a bit deal. You can go ahead and just Verlet the whole thing. But you'll end up with precession of orbits, overly energetic fly-bys and many other artifacts of poor computation. If there is no need for player to do any super-precise trajectory estimates, that's not too bad. But if you want to have any predictive power at all in your simulation, you have to spend a lot of effort on that integration method.

Link to comment
Share on other sites

I was looking into a way to plot a space trajectory (like KSP does) for a project of mine but was unable to find anything other than complex physics writings.

Is there any chance I could get a push in the right direction?

How did the developers for KSP research this?

Welcome to the forums, there are a few solid books on the matter. A pretty sound choice is "Fundamentals of astrodynamics and applications" by David Vallado. Books like this might be pretty hard to find though. Could you perhaps clarify if you're interested (as ZRM mentioned) in plotting the trajectory through physical simulation or through the 2-body problem ("rails")? The former is quite trivial with basic physics equations.

The latter has many different methods which do involve somewhat complicated mathematics. Presume your project involves programming? If yes, what language?

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...