To create a live 3D simulation of a rocket traveling from Earth to Mars using real orbital dynamics, we need to consider several key components:
-
Orbital Mechanics: The motion of celestial bodies (Earth and Mars) follows Keplerian orbits around the Sun. The rocket's trajectory will be modeled using a Hohmann transfer orbit, which is an elliptical orbit that efficiently transfers a spacecraft between two circular orbits.
-
Numerical Integration: To simulate the rocket's motion, we'll use numerical integration techniques (e.g., Runge-Kutta methods) to solve the equations of motion under gravitational forces.
-
Visualization: Using Matplotlib, we'll plot the positions of Earth, Mars, and the rocket over time in a 3D space.
Below is a Python implementation that accomplishes this:
Python Code Implementation
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
# Constants
G = 6.67430e-11 # Gravitational constant (m^3 kg^-1 s^-2)
M_sun = 1.989e30 # Mass of the Sun (kg)
AU = 1.496e11 # Astronomical Unit (m)
year = 365.25 * 24 * 3600 # Seconds in a year
# Orbital parameters for Earth and Mars
r_earth = 1.0 * AU # Earth's orbital radius (m)
r_mars = 1.524 * AU # Mars' orbital radius (m)
# Initial conditions
earth_position = np.array([r_earth, 0, 0]) # Earth starts at (r_earth, 0, 0)
mars_position = np.array([r_mars, 0, 0]) # Mars starts at (r_mars, 0, 0)
rocket_position = np.array([r_earth, 0, 0]) # Rocket starts at Earth's position
# Velocities (circular orbit assumption)
v_earth = np.sqrt(G * M_sun / r_earth) # Earth's orbital velocity
v_mars = np.sqrt(G * M_sun / r_mars) # Mars' orbital velocity
rocket_velocity = np.array([0, v_earth + np.sqrt(G * M_sun * (2 / r_earth - 1 / ((r_earth + r_mars) / 2))), 0])
# Time setup
t_start = 0
t_end = 2 * year # Simulate for 2 years
t_eval = np.linspace(t_start, t_end, 1000) # Time points for evaluation
# Define the system of ODEs
def orbital_dynamics(t, y):
"""
y[0:3] = position of Earth
y[3:6] = velocity of Earth
y[6:9] = position of Mars
y[9:12] = velocity of Mars
y[12:15] = position of Rocket
y[15:18] = velocity of Rocket
"""
r_earth = y[0:3]
v_earth = y[3:6]
r_mars = y[6:9]
v_mars = y[9:12]
r_rocket = y[12:15]
v_rocket = y[15:18]
# Gravitational accelerations
a_earth = -G * M_sun * r_earth / np.linalg.norm(r_earth)**3
a_mars = -G * M_sun * r_mars / np.linalg.norm(r_mars)**3
a_rocket = -G * M_sun * r_rocket / np.linalg.norm(r_rocket)**3
return np.concatenate([v_earth, a_earth, v_mars, a_mars, v_rocket, a_rocket])
# Initial state vector
y0 = np.concatenate([
earth_position, np.array([0, v_earth, 0]),
mars_position, np.array([0, v_mars, 0]),
rocket_position, rocket_velocity
])
# Solve the ODE
sol = solve_ivp(orbital_dynamics, [t_start, t_end], y0, t_eval=t_eval, method='RK45')
# Extract positions
earth_x, earth_y, earth_z = sol.y[0], sol.y[1], sol.y[2]
mars_x, mars_y, mars_z = sol.y[6], sol.y[7], sol.y[8]
rocket_x, rocket_y, rocket_z = sol.y[12], sol.y[13], sol.y[14]
# Plotting
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# Plot orbits
ax.plot(earth_x, earth_y, earth_z, label="Earth Orbit", color="blue")
ax.plot(mars_x, mars_y, mars_z, label="Mars Orbit", color="red")
ax.plot(rocket_x, rocket_y, rocket_z, label="Rocket Trajectory", color="green")
# Plot current positions
ax.scatter(earth_x[-1], earth_y[-1], earth_z[-1], color="blue", label="Earth")
ax.scatter(mars_x[-1], mars_y[-1], mars_z[-1], color="red", label="Mars")
ax.scatter(rocket_x[-1], rocket_y[-1], rocket_z[-1], color="green", label="Rocket")
# Labels and legend
ax.set_xlabel("X (m)")
ax.set_ylabel("Y (m)")
ax.set_zlabel("Z (m)")
ax.legend()
plt.title("Rocket Trajectory from Earth to Mars")
plt.show()
Explanation of the Code
-
Orbital Dynamics:
- The
orbital_dynamics function defines the system of ordinary differential equations (ODEs) governing the motion of Earth, Mars, and the rocket under the influence of the Sun's gravity.
- The gravitational acceleration for each body is calculated using Newton's law of gravitation.
-
Initial Conditions:
- Earth and Mars are placed in their respective circular orbits around the Sun.
- The rocket starts at Earth's position with a velocity adjusted for a Hohmann transfer orbit.
-
Numerical Integration:
- The
solve_ivp function from SciPy is used to numerically integrate the ODEs over the specified time range.
-
Visualization:
- The positions of Earth, Mars, and the rocket are plotted in 3D using Matplotlib.
- The trajectories of Earth, Mars, and the rocket are visualized, along with their final positions.
Output
The output is a 3D plot showing:
- The circular orbits of Earth and Mars.
- The elliptical trajectory of the rocket as it travels from Earth to Mars.
- The final positions of Earth, Mars, and the rocket at the end of the simulation.
This simulation provides a clear visualization of the rocket's path relative to Earth and Mars, demonstrating the principles of orbital mechanics and transfer orbits.