A simplified C++ simulation of an Earth-Moon free-return trajectory.
The goal of this project was to build a small numerical physics simulator that shows how a spacecraft can travel from Earth to the Moon and return back to Earth. The simulation is not meant to be a real mission-planning tool. It is a student project focused on orbital mechanics, numerical integration and clean scientific programming.
The program simulates the motion of a spacecraft in a simplified 2D Earth-Moon system.
The current version includes:
- Earth and Moon gravity,
- a moving Moon on a circular orbit,
- an Earth-centered frame with an indirect acceleration correction,
- fourth-order Runge-Kutta integration,
- a simple trajectory search for initial conditions,
- output files with trajectory data and mission summary,
- Python plots and GIF animations.
The spacecraft is treated as a point mass. Its motion is calculated from Newtonian gravity.
The gravitational acceleration has the form:
a = -G M r / |r|^3The Moon is modeled as moving on a circular orbit around Earth:
x_moon = d cos(omega t + phase)
y_moon = d sin(omega t + phase)where d is the average Earth-Moon distance and omega is the Moon's angular velocity.
Because the simulation is written in an Earth-centered frame, an indirect acceleration term is added. This corrects for the fact that the Moon also pulls on Earth.
The equations of motion are solved using the classical fourth-order Runge-Kutta method.
The state vector is:
x, y position
vx, vy velocity
m massFor the main coast phase, the spacecraft mass is treated as constant.
The selected simulation uses a simplified mission sequence:
parking orbit → impulsive TLI burn → Earth-Moon coast → Moon flyby → Earth returnThe burn is modeled as an instant velocity change, not as a real finite-duration engine burn.
Final selected parameters:
parking orbit altitude: 200 km
parking orbit period: 88.35 min
Moon phase at TLI: 0.4 rad
TLI speed: 10930 m/s
gamma: 0.375 radFor the selected trajectory, the simulation produced:
closest Moon approach: 8248.67 km
time of closest Moon approach: 93.32 h
maximum Earth altitude: 388020 km
return time: 187.23 h
return velocity: 10.98 km/sThese values are from the simplified model, so they should be treated as simulation results, not as real mission data.
The initial conditions were selected using a simple parameter search.
The search tested different values of:
Moon phase
initial position angle
initial velocity magnitude
initial velocity directionThe objective was to find a trajectory that:
- passes close to the Moon,
- returns close to Earth,
- keeps a clean free-return-like shape,
- avoids impact with the Moon or Earth.
This is not an advanced optimizer, but it is enough to find a good trajectory for this simplified model.
src/
├── constants.h
├── state.h
├── physics.h
├── physics.cpp
├── integrator.h
├── integrator.cpp
├── main.cpp
└── search.cpp
plots/
├── plot.py
├── animate.py
├── flight_inertial.gif
├── flight_rotating.gif
├── trajectory_inertial.png
├── trajectory_rotating.png
├── moon_distance.png
├── altitude.png
└── velocity.png
results/
├── trajectory.txt
├── summary.txt
└── search_best.txtCompile the main simulation:
g++ src/main.cpp src/physics.cpp src/integrator.cpp -o moon.exeRun it:
./moon.exeGenerate plots:
python plots/plot.pyGenerate GIF animations:
python plots/animate.pyRun the trajectory search:
g++ src/search.cpp src/physics.cpp src/integrator.cpp -o search.exe
./search.exeThe main simulation writes trajectory data to:
results/trajectory.txtThe columns are:
t x y vx vy m altitude velocity moon_x moon_y distance_moonA short mission summary is saved to:
results/summary.txtThis project is intentionally simplified.
Main limitations:
- 2D motion only,
- circular Moon orbit,
- no Sun gravity,
- no atmosphere,
- no real ephemeris data,
- no finite engine burn model,
- simplified spacecraft model,
- simplified trajectory search.
Because of this, the results are useful for learning and visualization, but not for real mission design.
Possible future improvements:
- adding Sun gravity,
- using the real Moon ephemeris data,
- improving the trajectory optimizer,
- adding a better TLI burn model,
- comparing different integration time steps,
- adding an interactive visualization,
- exporting more mission statistics.
C++
Python
NumPy
Matplotlib
Numerical Methods
Orbital MechanicsPatryk Kuna





