3
$\begingroup$

I'm making a space exploration game and its in a 2d plane so no z axis, and I'm using Newton's law of universal gravity, I would really like to calculate anddraw the predicted trajectory of a body, how would I proceed with this problem, I have the global position of the objects and the orbital velocities of them

$\endgroup$
5
  • 1
    $\begingroup$ Welcome to Stack Exchange! You have the trajectories, and you just want to draw (plot) them? Python's standard plotting library is matplotlib and you can use plot(x, y, '-') to connect the points with straight line segments or plot(x, y, 'o') to plot circles at each (x, y) point. Is that really the question you're asking? Give it a try, and then if you are unhappy with the results - the way your plotted orbits look, then add a screenshot to your question and explain further what you'd like to be different. $\endgroup$
    – uhoh
    Apr 7 at 12:31
  • $\begingroup$ Click "edit" and use this button to import your screenshot into your question i.stack.imgur.com/oj1e8.png $\endgroup$
    – uhoh
    Apr 7 at 12:33
  • $\begingroup$ Using matplotlib as recommended by uhoh would help you very much. Extension to a 3D simulation is well supported by mathplotlib. You may plot all 3 dimensions in a diagram and also the three 2D views for the XY, XZ and YZ planes. There are many answers here built with python and matplotlib. $\endgroup$
    – Uwe
    Apr 7 at 14:57
  • 1
    $\begingroup$ You may look into these questions: space.stackexchange.com/questions/48830/… and space.stackexchange.com/questions/25958/… and space.stackexchange.com/questions/25958/… $\endgroup$
    – Uwe
    Apr 8 at 5:04
  • 1
    $\begingroup$ I would recomend looking into "conic sections". These are what Kerbal Space Program uses, and what the Apollo program used. The numerical method posted in the answer below can become unstable. $\endgroup$ Apr 8 at 12:06

1 Answer 1

6
$\begingroup$

The easiest initial approach to this is probably to propagate your state forward in time steps, with a time delta small enough to have reasonable accuracy. You can then join the x-y points with line segments.

Say you have a state of a position x and y, and velocity vx and vy:

Pseudocode

delta = 0.001 //tradeoff performance and accuracy
for many many steps
    time += delta
    drawLine(x, y, x + vx * delta, y + vy * delta)
    x += vx * delta
    y += vy * delta
    for body in bodies: //everything affecting you with gravity
        distance = sqrt((x - body.x)² + (y - body.y)²)
        acceleration = body.mass / distance²
        vx -= acceleration * (x - body.x)/distance * delta
        vy -= acceleration * (y - body.y)/distance * delta

With discrete steps, there's obviously some error which grows over time though. Separating the physics and graphics may also be something to look into asa screen is just a couple of thousand pixels anyway, so a few thousand line segments should look like a properly smooth trajectory, while the physics simulation could easily be run for millions of iterations if you want it to.

Alternatively, you can try to convert your state vectors to orbital elements, and draw those as circles or ellipses (tip: an ellipse is just a circle scaled along an axis).

$\endgroup$
2
  • $\begingroup$ some python here space.stackexchange.com/a/63275/12102 but it doesn't help much as far as a basic "how to". $\endgroup$
    – uhoh
    Apr 7 at 22:58
  • 1
    $\begingroup$ This looks like the first order explicit Euler method. It should be accompanied with several disclaimers about limited accuracy, stability and non-conservativness. $\endgroup$ Apr 8 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.