Below is aself-contained Python exampleusingmatplotliband itsanimationfunctionality to illustrate a simple, idealized 3D animation of a spacecraft traveling from to via a Hohmann-like transfer orbit and then back to at the next (extremely simplified) launch window.
Important Notes
This demonstration is purely illustrative—real orbital mechanics and accurate planetary positions/launch windows are far more complex.
We use circular orbits in a single plane and do not strictly compute exact Hohmann transfers or precise phase angles; the code simply shows an “elliptical path” for the spacecraft and times it so that it meets and then returns.
You can tweak parameters (distances, orbital periods, transfer times) for a more realistic or more visually appealing animation.
This demonstration is purely illustrative—real orbital mechanics and accurate planetary positions/launch windows are far more complex.
We use circular orbits in a single plane and do not strictly compute exact Hohmann transfers or precise phase angles; the code simply shows an “elliptical path” for the spacecraft and times it so that it meets and then returns.
You can tweak parameters (distances, orbital periods, transfer times) for a more realistic or more visually appealing animation.
pythonCopyimport numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from mpl_toolkits.mplot3d import Axes3D
--------------------
1. Define Constants
--------------------
For simplicity, we'll use Astronomical Units (AU) and years as units
(1 AU ~ distance from to Sun, 1 year ~ orbital period of )
These are not exact, just convenient approximations.
AU = 1.0
EARTH_ORBIT_RADIUS = 1.0 * AU at ~1
MARS_ORBIT_RADIUS = 1.524 * AU # Mars at ~1.524 AU
EARTH_ORBIT_PERIOD = 1.0 # 1 year
MARS_ORBIT_PERIOD = 1.881 # ~1.881 years
Animation time parameters
TOTAL_TIME = 2.5 # in years, for example (~2.5 years total)
N_STEPS = 500 # number of animation frames
Times for each frame
times = np.linspace(0, TOTAL_TIME, N_STEPS)
------------------------------------------------
2. Parametric Equations for Earth and Mars Orbits
------------------------------------------------
We assume circular orbits and that t=0 starts with at angle 0, at some offset
(A true Hohmann transfer needs a specific phase angle; we simplify here.)
For (circular orbit around the at the origin)
angle_E(t) = 2*pi * (t / Earth_orbit_period)
def earth_position(t):
angle = 2 * np.pi * (t / EARTH_ORBIT_PERIOD)
x = EARTH_ORBIT_RADIUS * np.cos(angle)
y = EARTH_ORBIT_RADIUS * np.sin(angle)
z = 0.0
return x, y, z
You can adjust these times for more accurate or visually pleasing transitions.
Define some times
transfer_time_out = 0.4 # fraction of total time for outbound trip
wait_time = 0.2 # fraction of total time waiting on
transfer_time_back = 0.4 # fraction of total time for return trip
Semi-major axes for elliptical orbits (outbound and inbound)
We'll pick something between ’s and ’ orbit radius
def elliptical_orbit(t_start, t_end, start_pos, end_pos, t):
"""Return x, y, z for an elliptical path from start_pos to end_pos
during times t in [t_start, t_end]. Outside that, returns start_pos or end_pos."""
if t < t_start:
return start_pos
if t > t_end:
return end_pos
code
# Fraction of transfer complete
frac = (t - t_start) / (t_end - t_start)
# We'll just do a simple interpolation in radial distance + a bit of "bow"
# for the ellipse in the XY plane to mimic an orbital arc.
# start_pos and end_pos are (x, y, z)
# We param by a simple 2D ellipse in polar coords.
# Convert start_pos and end_pos to polar angles/radii
sx, sy, <REDACTED> = <REDACTED>ex, ey, ez = end_pos
# Radii and angles
r_s = np.sqrt(sx**2 + sy**2)
theta_s = np.arctan2(sy, sx)
r_e = np.sqrt(ex**2 + ey**2)
theta_e = np.arctan2(ey, ex)
# Interpolate radius from <REDACTED> orbit to <REDACTED> orbit
# We'll do a simple elliptical approach:
# r(t) = "some curve" from r_s to r_e
# For a real <REDACTED> transfer, we’d define an ellipse. Here, we just do a naive approach.
# In a <REDACTED> transfer, the semi-major axis = (r_s + r_e)/2
# We'll add a small elliptical eccentricity by hand or do a naive param.
# For simplicity: r(t) = (1 - frac)*r_s + frac*r_e + extra "bump" in the middle
r = (1 - frac)*r_s + frac*r_e
# Let's add a small sinusoidal bump to mimic ellipse arc
r_bump = 0.2 * (r_e - r_s) * <URL>n(np.pi * frac)
r += r_bump
# Interpolate angle from theta_s to theta_e
# (This is *not* physically accurate, but looks somewhat plausible.)
theta = (1 - frac)*theta_s + frac*theta_e
x = r * np.cos(theta)
y = r * np.sin(theta)
z = 0.0
return (x, y, z)
Orbits:earth_position(t)andmars_position(t)assume circular orbits in the XY-plane around the at the origin.They return (x, y, z) coordinates for and at a given time.
earth_position(t)andmars_position(t)assume circular orbits in the XY-plane around the at the origin.
They return (x, y, z) coordinates for and at a given time.
Spacecraft Trajectory:We break the spacecraft timeline into three segments (outbound to , waiting on , inbound back to Earth).We useelliptical_orbitto generate a simple “arc” from ’s position at the start to ’s position at the end of the outbound segment, then from ’s position back to ’s final position on the inbound segment.In reality, you’d solve orbital mechanics to find a proper Hohmann transfer ellipse and phase angles.
We break the spacecraft timeline into three segments (outbound to , waiting on , inbound back to ).
We useelliptical_orbitto generate a simple “arc” from ’s position at the start to ’s position at the end of the outbound segment, then from ’s position back to ’s final position on the inbound segment.
In reality, you’d solve orbital mechanics to find a proper Hohmann transfer ellipse and phase angles.
Animation:We pre-compute and orbits over the entire time array for background lines.At each animation frame, we compute the positions of , , and the spacecraft, then update the artists (the lines and marker points).
We pre-compute and orbits over the entire time array for background lines.
At each animation frame, we compute the positions of , , and the spacecraft, then update the artists (the lines and marker points).
Customization:Adjusttransfer_time_out,wait_time, andtransfer_time_backto decide how long each mission phase lasts.TuneMARS_INITIAL_OFFSETfor the relative angle of at t=0.
Adjusttransfer_time_out,wait_time, andtransfer_time_backto decide how long each mission phase lasts.
TuneMARS_INITIAL_OFFSETfor the relative angle of at t=0.
Orbits:
earth_position(t)andmars_position(t)assume circular orbits in the XY-plane around the at the origin.
They return (x, y, z) coordinates for and at a given time.
earth_position(t)andmars_position(t)assume circular orbits in the XY-plane around the at the origin.
They return (x, y, z) coordinates for and at a given time.
Spacecraft Trajectory:
We break the spacecraft timeline into three segments (outbound to , waiting on , inbound back to ).
We useelliptical_orbitto generate a simple “arc” from ’s position at the start to ’s position at the end of the outbound segment, then from ’s position back to ’s final position on the inbound segment.
In reality, you’d solve orbital mechanics to find a proper Hohmann transfer ellipse and phase angles.
We break the spacecraft timeline into three segments (outbound to , waiting on , inbound back to ).
We useelliptical_orbitto generate a simple “arc” from ’s position at the start to ’s position at the end of the outbound segment, then from ’s position back to ’s final position on the inbound segment.
In reality, you’d solve orbital mechanics to find a proper Hohmann transfer ellipse and phase angles.
Animation:
We pre-compute and orbits over the entire time array for background lines.
At each animation frame, we compute the positions of , , and the spacecraft, then update the artists (the lines and marker points).
We pre-compute and orbits over the entire time array for background lines.
At each animation frame, we compute the positions of , , and the spacecraft, then update the artists (the lines and marker points).
Customization:
Adjusttransfer_time_out,wait_time, andtransfer_time_backto decide how long each mission phase lasts.
TuneMARS_INITIAL_OFFSETfor the relative angle of at t=0.
Adjusttransfer_time_out,wait_time, andtransfer_time_backto decide how long each mission phase lasts.
TuneMARS_INITIAL_OFFSETfor the relative angle of at t=0.
Running the Code
Save the script to a file, e.g.earth_mars_transfer.py, or run directly in a notebook cell.
Make sure you havematplotlibandnumpyinstalled.
You should see a 3D window where (blue) and (red) orbit the Sun (gold). A green spacecraft travels from to , waits, and then returns.
Save the script to a file, e.g.earth_mars_transfer.py, or run directly in a notebook cell.
Make sure you havematplotlibandnumpyinstalled.
You should see a 3D window where (blue) and (red) orbit the Sun (gold). A green spacecraft travels from to , waits, and then returns.
Feel free toexperiment, refine the orbital parameters, or switch to more physically accurate methods for the transfer arcs. This code is intended as a clear template for building more detailed 3D animations of planetary missions!