Skip to content

Hiba-Malkan/gravitas

Folders and files

NameName
Last commit message
Last commit date

Latest commit

a9e1535 · · Mar 20, 2026

History

14 Commits
Mar 8, 2026
Mar 18, 2026
Mar 20, 2026
Mar 20, 2026
Mar 18, 2026

Repository files navigation

Gravitas

Gravitas is a collection of orbital mechanics simualations. Right now, I have n-body gravity and hohmann transfer simulations. I hope to add lagrange points simulation in a future update..

Live Demo: https://gravitas-eta.vercel.app/


Why I made it?

I like physics and math and plan to major in either one in the future. I also have college application coming up. This made a great personal project to showcase my passion for physics in my college applications.


Simulations

N-Body Gravity

The n-body gravity problem asks: given N objects that all gravitationally attract each other at the same time, how do they move?

Unlike the two-body problem (one planet and one star), there is no closed form solution for N ≥ 3 bodies, so you have to simulate it step by step.

This simulation runs the full solar system, in which, every planet exerts a gravitational force on every other planet, in every frame, and the positions are updated accordingly. You can zoom, pan, add new bodies by clicking the canvas and see how the affect the system (collisions, merges, ejected out of system), and switch to a binary star system as well..

Hohmann Transfer

A Hohmann Transfer is a two-burn maneuver thats moves a spacecraft between two circular orbits using the minimum possible change in velocity (hence, most efficient way). The first burn is to leave the first orbit and enter an elliptical transfer orbit, and the second burn, when the spacecraft arrives at the second orbit, to circularise.

This simulation calculates the two burns, transfer time, phase angle for any two planetary orbits (in planet mode), and animates the spacecraft travelling the path. Satellite mode does the same for orbits around any solar system body, in km, and allows you to change the altitude and inclination as well.

How to run

git clone https://github.com/Hiba-Malkan/gravitas.git

cd gravitas
python3 -m http.server 80800

Then open http://localhost:8080 in your browser.

Or open index.html directly, no need for starting the server.

Tech Stack

-- HTML

-- CSS

-- Javascript

Equations and Math

Accelaration due to Gravity (Newton's Law of Gravitation)

a i = G j i m j ( r j r i ) | r j r i | 3

Force between every pair of bodies, all added together. G = 4π² in AU.

Gravitational softening

| r | 2 | r | 2 + ε 2

A small ε² term added to the distance to prevent the force from tending to infinity during close encounters (so accelaration doesn't tend to infinity and bodies don't go zooming across the canvas). Used ε = 0.001 AU.

RK4 Integration

k 1 = f ( t , y ) k 2 = f ! ( t + h 2 , , y + h 2 k 1 ) k 3 = f ! ( t + h 2 , , y + h 2 k 2 ) k 4 = f ( t + h , , y + h k 3 )

y n + 1 = y n + h 6 ( k 1 + 2 k 2 + 2 k 3 + k 4 )

4th order Runge-Kutta integrator evaluates the derivative at 4 points per step and takes a weighed average. This keeps orbits stable long term unlike the Euler method I was previously using.

Vis-Viva Equation

v = G M ( 2 r 1 a )

Vis-viva equation gives the speed of an object at distance r from the central body, in an orbit with a semi-major axis, a. This is used for all Hohmann Transfer velocity calculations. For a circular orbit r = a, so the equation simplifies to v = √(GM/r).

Hohmann Transfer

Semi-major axis of the transfer ellipse is:

a t r a n s f e r = r 1 + r 2 2

Delta-v (change in velocity) at each burn is calcualted using the vis-viva difference between circular velocity and transfer ellipse velocity at each endpoint.

Transfer time, then, (half of the period of the ellipse):

t t r a n s f e r = π a 3 G M

Phase Angle

Phase angle is the specific angular separation required between a spacecraft and its target planet (or two planets) at the moment of launch to ensure they meet at the destination after a Hohmann transfer orbit (defination taken from Google AI overview).

ϕ = 180 ° ( 1 t t r a n s f e r T t a r g e t )

Kepler's Equation

M = E e sin ( E )

M is mean anomaly (linear in time), E is eccentric anomaly , e is eccentricity. This has no closed-form solution and hence is solved numerically using Newtons-Raphson iteration. It is used to compute the spacecraft's position on the ellipse at each animation frame.

True anomaly from eccentric anomaly:

tan ν 2 = 1 + e 1 e tan E 2

Inclination change (combined maneuver)

Used in the simulation to combine the plane change with the circularisation burn using the law of cosines:

Δ v c o m b i n e d = v t 2 + v c 2 2 v t v c cos ( Δ i )

Energy conservation (n-body)

E = i 1 2 m i v i 2 K E i j G m i m j r i j P E = const

Tracked to ensure that energy doesn't drift significantly and the integator does not continue to accumulate error. In a perfect scenario this would never work, but in practice, it should only drift slightly.

Things I struggled with

RK4 implementation : to evaluate the derivative at an intermediate point you have to temporarily move all the bodies to their intermediate positions, calc. forces and then restore them before the next substep. I forgot to restore the bodies and they started teleporting.

Binary stars initial conditions : both the stars in the preset needed to orbit their shared barycentre, not the origin. so, getting the velocities required wroking out the relative circular orbital speed and them splitting it proportionally by mass.

Spacecraft Animation : I was using const. angualr speed around the ellipse, and this does not follow Kepler's second law (Law of Areas- equal area swept in equal time). I had to convert mean anomaly to true anomaly using Newton-Raphson method.

Canvas sizing: offsetWidth returns zero when a panel is just made visible. fix was a requestAnimationFrame delay before measuring (this took a lot of time to figure out for how insignificant this was).


AI Usage:

Used AI to validate code to the effect that it followed the laws of physics. Used github's copilot for code autocompletion (however time spent by using copilot autocomplete is not tracked by Hackatime anyway).

Used Google's AI Overview for equations used in this readme (the way they're written in the readme?)


Built for Hack Club

Created: 20 March, 2025

About

Physics simulation for n-body gravity and Hohmann Transfer.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published