A 2D orbit/gravity simulator written in C++ using SFML.
It models orbital motion using the velocity–Verlet integrator, which is derived from a Taylor expansion of position and the trapezoidal rule for velocity. This simulator lets you specify the gravitational strength (μ), initial position (r₀), and initial velocity (v₀), and then visualizes the trajectory in real time with stars, glow, and a fading trail.
I’ve always been fascinated by the logic and theory behind how our world works, especially gravity. By building a gravity/orbit simulator, I wanted to see how mathematical laws (Newton’s law of gravitation) could be turned into code, algorithms, and visualizations. The project is a combination of c++/SFML(graphics), verlet algorithm(velocity) and Newtonian Laws of Gravity
- Console prompts for initial μ, r₀, v₀ with defaults and recommended ranges
- Velocity–Verlet integration scheme
- SFML rendering: stars, glowing center, fading orbit trail
This section walks from Taylor expansion of position through the final Velocity–Verlet scheme.
Expand
Dropping higher-order terms gives:
This is how we obtain
From Newton’s law:
Acceleration for the particle:
After updating
This gives
Velocity evolves according to:
Since
So the velocity update is:
This gives
Together, the update rules are:
-
$r_{\text{new}}$ comes from Taylor expansion. -
$a_{\text{new}}$ comes from the gravitational field formula applied at the new position. -
$v_{\text{new}}$ comes from integrating acceleration with the trapezoid rule.
To check correctness:
- Specific Energy
- Angular Momentum
For circular orbit (μ=1, r=1, v=1):
Role:
- User input (μ, r₀, v₀)
- SFML window setup and rendering
- Time loop: calls
verlet()every frame - Displays visual orbit, trail, HUD
Physics Features Implemented:
-
Calls to:
-
accel(r, mu)→ gravitational acceleration -
verlet(r, v, a, dt, mu)→ motion update
-
-
HUD output of:
$E = \tfrac{1}{2} |v|^2 - \mu/|r|$ $L = x v_y - y v_x$
-
Converts physics coordinates → screen pixels
Math formulas used:
double rmag = norm(r); // ‖r‖
double vsq = dot(v, v); // v⋅v = |v|²
double E = 0.5 * vsq - mu / rmag; // specific energy
double L = r.x * v.y - r.y * v.x; // angular momentumRole:
- Defines a 2D vector struct (
Vec2) - Implements basic vector operations
Functions Implemented:
-
add(Vec2 a, Vec2 b)→$a + b$ -
sub(Vec2 a, Vec2 b)→$a - b$ -
scale(Vec2 a, double k)→$k \cdot a$ -
dot(Vec2 a, Vec2 b)→$a \cdot b = ax \cdot bx + ay \cdot by$ -
norm(Vec2 a)→$|a| = \sqrt{a \cdot a}$
Math formulas:
// norm = ||a|| = sqrt(ax² + ay²)
double norm(Vec2 a) {
return std::sqrt(dot(a, a));
}Role:
- Contains core physics equations
- Handles gravitational acceleration
- Runs the velocity–Verlet algorithm
Implements:
Purpose: Calculates gravitational acceleration based on distance from the origin.
double factor = -mu / (rmag * rmag * rmag);
return scale(r, factor);Implements:
Purpose: Performs one update of the particle’s motion using a second-order accurate method derived from Taylor expansion + trapezoid rule.
Vec2 r_new = add(r, add(scale(v, dt), scale(a, 0.5 * dt * dt)));
Vec2 a_new = accel(r_new, mu);
Vec2 v_new = add(v, scale(add(a, a_new), 0.5 * dt));