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.
Full mission animation:
Mars capture in a Mars-centered reference frame:
Sun-centered frame following Mars:
- 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
The spacecraft acceleration is calculated from Newtonian gravity:
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.
The equations of motion are solved using the fourth-order Runge-Kutta method:
position derivative = velocity
velocity derivative = acceleration from gravityRK4 was used instead of simple Euler integration because the mission covers many months of flight and the trajectory is sensitive to accumulated numerical errors.
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:
The transfer time is:
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.
The rocket burns are modeled with the Tsiolkovsky rocket equation:
where:
Delta vis the velocity change from the burnv_eis the exhaust velocitym_0is the spacecraft mass before the burnm_fis the spacecraft mass after the burn
In the code, the equation is used in this form:
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:
-
Trans-Mars injection burn The spacecraft leaves low Earth orbit and enters a transfer trajectory toward Mars.
-
Mars capture burn The spacecraft slows down relative to Mars and becomes captured into a Mars-centered orbit.
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.11Mars 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 orbitThe 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.
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.gifCompile the final mission simulation:
g++ src/main.cpp src/physics.cpp src/integrator.cpp -o mars.exeRun it:
./mars.exeGenerate plots:
python scripts/plot.pyGenerate animations:
python scripts/animate.py
python scripts/animate2.py
python scripts/animate3.pyRun the search program:
g++ src/search.cpp src/physics.cpp src/integrator.cpp -o search.exe
./search.exeThe simulation creates:
results/trajectory.txt
results/telemetry.txt
results/summary.txtThe plotting and animation scripts create:
figures/*.png
figures/*.gifThis 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.
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
C++
Python
Matplotlib
Numerical integration
Orbital mechanics
Tsiolkovsky rocket equationThis 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.
Patryk Kuna







