Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mars Mission Simulator

This project is a simple 2D simulation of a spacecraft mission from Earth to Mars.

The main idea was to build a physics-based mission simulator in C++ and visualize the trajectory with Python. The spacecraft starts from a low Earth parking orbit, performs a trans-Mars injection burn, travels under gravity from the Sun, Earth, and Mars, and finally performs a Mars capture burn. After the capture burn, the spacecraft starts orbiting Mars in the simulation.

The project is intentionally simplified, but it connects several important ideas from orbital mechanics: Hohmann transfer, numerical integration, Newtonian gravity, the Tsiolkovsky rocket equation, fuel consumption, and Mars orbit insertion.

Preview

Cinematic animations

Full mission animation:

Full mission animation

Mars capture in a Mars-centered reference frame:

Mars orbit after capture

Sun-centered frame following Mars:

Mars following Sun frame

Full Mars transfer trajectory

Full trajectory

Features

  • 2D Sun-centered Solar System model
  • gravitational force from the Sun, Earth, and Mars
  • Earth and Mars moving on circular orbits
  • spacecraft launched from a low Earth parking orbit
  • Hohmann transfer estimate from Earth orbit to Mars orbit
  • numerical correction of the transfer using a parameter search
  • Tsiolkovsky rocket equation for fuel consumption
  • spacecraft mass loss during burns
  • RK4 numerical integration
  • Mars closest approach detection
  • capture burn applied during the simulation
  • post-capture Mars orbit visualization
  • trajectory, telemetry plots, and animated GIFs

Physical model

The spacecraft acceleration is calculated from Newtonian gravity:

$$\vec{a} = \sum_i G M_i \frac{\vec{r_i} - \vec{r}}{|\vec{r_i} - \vec{r}|^3}$$

In this simulation, the active gravity sources are:

  • Sun
  • Earth
  • Mars

The spacecraft is not moved along a predefined curve. Its position and velocity are updated numerically at every time step.

Numerical method

The equations of motion are solved using the fourth-order Runge-Kutta method:

position derivative = velocity
velocity derivative = acceleration from gravity

RK4 was used instead of simple Euler integration because the mission covers many months of flight and the trajectory is sensitive to accumulated numerical errors.

Hohmann transfer

The first estimate of the transfer is based on a Hohmann transfer from Earth's orbit to Mars' orbit.

The semi-major axis of the transfer ellipse is:

$$a = \frac{r_E + r_M}{2}$$

The transfer time is:

$$T = \pi \sqrt{\frac{a^3}{G M_{Sun}}}$$

This gives a good first guess for the Mars phase and the required launch velocity. Since the simulation also includes Earth gravity, finite parking orbit altitude, and numerical effects, a small numerical search is used to improve the final trajectory.

Tsiolkovsky rocket equation

The rocket burns are modeled with the Tsiolkovsky rocket equation:

$$\Delta v = v_e \ln\left(\frac{m_0}{m_f}\right)$$

where:

  • Delta v is the velocity change from the burn
  • v_e is the exhaust velocity
  • m_0 is the spacecraft mass before the burn
  • m_f is the spacecraft mass after the burn

In the code, the equation is used in this form:

$$m_f = m_0 e^{-\Delta v / v_e}$$

This means that every burn changes the spacecraft velocity, but also reduces its mass. If the spacecraft does not have enough available delta-v, the burn is limited by the dry mass.

The simulation uses two important burns:

  1. Trans-Mars injection burn The spacecraft leaves low Earth orbit and enters a transfer trajectory toward Mars.

  2. Mars capture burn The spacecraft slows down relative to Mars and becomes captured into a Mars-centered orbit.

Mission search

The file search.cpp is used as a simple mission planner.

It scans different values of:

  • initial Mars phase
  • launch delta-v correction factor

and finds the combination that gives the smallest Mars approach distance.

The final selected values used in the simulation were:

Mars phase = 1.09399 rad
launch delta-v factor = 1.11

Final mission result

Mars Mission Simulator - Mission Summary

transfer type = Hohmann transfer with numerical correction
integration method = RK4
gravity sources = Sun, Earth, Mars

initial spacecraft mass = 30000 kg
final spacecraft mass = 7194.25 kg
fuel used = 22805.8 kg
initial available delta-v = 7242.47 m/s

Mars phase = 1.09399 rad
launch delta-v factor = 1.11

launch delta-v estimate = 3986.62 m/s
launch delta-v applied = 3986.62 m/s
Mars capture delta-v estimate = 2439 m/s
Mars capture delta-v applied = 2439 m/s
total delta-v applied = 6425.62 m/s

Hohmann transfer time = 258.865 days
closest Mars approach before capture = 130294 km
time of closest Mars approach = 368.079 days
Mars relative speed at closest approach = 573.983 m/s
maximum Sun distance = 2.28391e+08 km

capture time = 295.842 days
capture distance from Mars center = 130570 km
Mars encounter status = captured into Mars orbit

The capture is not a low Mars orbit. The spacecraft enters a wide Mars orbit because the closest approach distance is still large compared to the radius of Mars. This is acceptable for this version of the project, because the goal was to simulate the complete transfer and capture process in a clear way.

Plots

Distance profile

Distance profile

Velocity profile

Velocity profile

Spacecraft mass

Mass profile

Distance from the Sun

Sun distance profile

Project structure

mars-mission-simulator/
├── README.md
├── src/
│   ├── main.cpp
│   ├── search.cpp
│   ├── constants.h
│   ├── vector2.h
│   ├── body.h
│   ├── spacecraft.h
│   ├── physics.cpp
│   ├── physics.h
│   ├── integrator.cpp
│   └── integrator.h
├── scripts/
│   ├── plot.py
│   ├── animate.py
│   ├── animate2.py
│   └── animate3.py
├── results/
│   ├── trajectory.txt
│   ├── telemetry.txt
│   ├── summary.txt
│   └── search.txt
└── figures/
    ├── trajectory_full.png
    ├── mars_capture_zoom.png
    ├── distance_profile.png
    ├── velocity_profile.png
    ├── mass_profile.png
    ├── sun_distance_profile.png
    ├── mars_mission_full_dark.gif
    ├── mars_orbit_after_capture_dark.gif
    └── mars_following_sun_frame_dark.gif

How to build and run

Compile the final mission simulation:

g++ src/main.cpp src/physics.cpp src/integrator.cpp -o mars.exe

Run it:

./mars.exe

Generate plots:

python scripts/plot.py

Generate animations:

python scripts/animate.py
python scripts/animate2.py
python scripts/animate3.py

Run the search program:

g++ src/search.cpp src/physics.cpp src/integrator.cpp -o search.exe
./search.exe

Output files

The simulation creates:

results/trajectory.txt
results/telemetry.txt
results/summary.txt

The plotting and animation scripts create:

figures/*.png
figures/*.gif

Limitations

This is not a full professional mission design tool. Some simplifications are used:

  • planets move on circular orbits
  • the model is 2D
  • no orbital inclination is included
  • no atmosphere or aerobraking is modeled
  • no real ephemeris data is used
  • burns are treated as instantaneous velocity changes
  • Mars capture is simplified into a circularizing burn at the encounter distance

Even with these simplifications, the project shows the main structure of an interplanetary mission and gives useful visual results.

Future improvements

Possible next steps:

  • using the real planetary ephemerides
  • adding eliptical planetary orbits
  • adding 3D-motion and inclinations
  • modeling finite-duration engine burns
  • improving Mars orbit insertion
  • targeting a lower Mars periapsis
  • adding a launch date optimization
  • comparing numerical trajectory with analytical Hohmann transfer

Technologies

C++
Python
Matplotlib
Numerical integration
Orbital mechanics
Tsiolkovsky rocket equation

Summary

This project simulates a complete simplified Earth-to-Mars mission. The spacecraft starts from a parking orbit around Earth, uses fuel to enter a Mars transfer trajectory, travels through the Solar System under gravity, and performs a capture burn near Mars. The final result is visualized with trajectory plots, telemetry plots and cinematic animations.

Author

Patryk Kuna

About

2D Mars mission simulation in C++ using Sun-centered orbital mechanics, Hohmann transfer, Tsiolkovsky rocket equation, fuel consumption and Mars capture burn.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors