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
-
1Welcome 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. – uhoh Apr 07 '23 at 12:31
-
Click "edit" and use this button to import your screenshot into your question https://i.stack.imgur.com/oj1e8.png – uhoh Apr 07 '23 at 12:33
-
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. – Uwe Apr 07 '23 at 14:57
-
1You may look into these questions: https://space.stackexchange.com/questions/48830/what-is-the-fastest-satellite-in-earth-orbit/48904#48904 and https://space.stackexchange.com/questions/25958/how-can-i-plot-a-satellites-orbit-in-3d-from-a-tle-using-python-and-skyfield and https://space.stackexchange.com/questions/25958/how-can-i-plot-a-satellites-orbit-in-3d-from-a-tle-using-python-and-skyfield/25959#25959 – Uwe Apr 08 '23 at 05:04
-
1I 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. – Greg Miller Apr 08 '23 at 12:06
1 Answers
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).
- 43,384
- 3
- 143
- 244
-
some python here https://space.stackexchange.com/a/63275/12102 but it doesn't help much as far as a basic "how to". – uhoh Apr 07 '23 at 22:58
-
1This looks like the first order explicit Euler method. It should be accompanied with several disclaimers about limited accuracy, stability and non-conservativness. – Vladimir F Героям слава Apr 08 '23 at 14:38