Simulating Gravity in C++

notes.

Simulating Gravity in C++

Source: Simulating Gravity in C++, kavan, 7:34, uploaded 2025-03-01, playlist index 312.

kavan opens with a solar system rendered in OpenGL. The planets are out of scale, although the motion comes from physics equations and a C++ program. The project starts as a way to make objects fall, grows into a Newtonian multi-body simulation, and then turns towards a visual account of curved spacetime. The code becomes a way to learn C++ syntax, rendering, vectors, and the awkward distance between a physical idea and a model that runs on a screen.

A circle, a window, and the first fall

The first task is an OpenGL window. OpenGL builds visible objects from small triangles, so a circle requires a list of vertices arranged around a centre. Its position, radius, and resolution determine the points, while basic trigonometric functions provide their coordinates. A drawCircle function keeps that work in one place and accepts a position vector, which lets the program move the centre inside the game loop.

Moving the circle down by a small amount produces motion, although kavan pauses over the difference between velocity and acceleration. Velocity describes a change in position over time. Gravity supplies acceleration. Near Earth’s surface, the video uses 9.81 m/s², which means that the velocity increases by 9.81 metres per second during each second of falling. The program therefore stores a velocity vector and increases its vertical component every frame before updating the position. A border-collision function turns the same object into a bouncing ball.

Newtonian gravity as a list of objects

The space simulation needs more than one mass. kavan wraps position, velocity, acceleration, an update function, and the earlier drawing function in an object class. A list holds several objects, and the loop draws and updates each one. The next step calculates the gravitational pull between every pair.

The video presents Newton’s equation as the gravitational constant multiplied by the two masses and divided by the distance between them squared. Newton’s second law then converts the resulting force into acceleration. Each object receives a mass, with the first example using 735 quintillion kilograms, roughly the mass of the Moon according to the video. The program applies the acceleration from every other object during each loop. Giving the bodies an initial velocity produces an orbit.

The structure is simple enough to see in the code. Each body carries its current state, the loop gathers the relations between bodies, and the update method turns those relations into the next frame. The demonstration has the quality of a sketch because the physical rule and the rendered object stay close together.

The move into three dimensions

kavan then clears the scene and rebuilds the renderer in 3D. The new version needs a camera with a field of view, position, and direction, along with shader programs, vertex-array objects, and vertex-buffer objects. The explanation moves quickly over the graphics plumbing, calling the parts “magic” because the video is interested in the simulation rather than a full OpenGL tutorial.

A circle can be made from points around a centre. A sphere needs a second coordinate. The program iterates over angular values that cover a full sphere, creates a quadrilateral between neighbouring points, and splits each quadrilateral into two triangles. Repeating that process fills a vertex list that OpenGL can render as a sphere. The earlier object and gravity code then carry across to the new renderer, producing three-dimensional orbits whose shapes change as the bodies gain another direction in which to move.

From force to curved spacetime

The video steps back from the working Newtonian model and changes the explanation of gravity. It first describes gravity as the pull between masses, then says that general relativity gives a different picture. Matter curves spacetime, and objects follow its natural curvature. The ground pushes a standing person upwards through the normal force, which keeps that person from following the falling path towards Earth’s centre. Someone in free fall feels weightless because the person follows an inertial path. From that viewpoint, the ground accelerates upwards.

kavan condenses the relation into a phrase: space tells matter how to move, and matter tells space how to curve. The next version of the project tries to make the second half visible through a grid. Lines spread across the scene, and the objects sit inside the resulting surface so that their movement appears to follow the curvature produced by mass.

A paraboloid in place of a full spacetime grid

The first attempt uses a three-dimensional grid. kavan shows the result, calls it the best attempt, and then qualifies it as inaccurate. Several other attempts look worse. The simulation therefore adopts a two-dimensional grid with the x and z coordinates held in place. The y coordinate comes from a formula for a funnel-shaped paraboloid, the surface used by many popular gravity visualisations.

That change works almost at once. It also exposes the limits of the picture. Large masses make the surface unstable and cause the simulation to blow up, so kavan scales the masses down. With the smaller values, the program produces a solar-system scene containing the Sun, Earth, the Moon, Mars, and Jupiter. The velocities need adjustment before the orbits settle into a stable-looking arrangement.

The visual grid gives the Newtonian motion a relativistic setting, although the video does not claim that it has implemented general relativity. The objects still move through a hand-built numerical model, and the paraboloid supplies an accessible image of curvature. The failed 3D versions remain part of the account because they show where the idea becomes harder to represent than the equation that first made the objects orbit.

Solar-system orbits, extra suns, and black holes

The finished scene lets kavan spawn new objects during the simulation. Adding a second Sun shows how another large mass disturbs the existing orbits. The experiment turns the stable solar-system arrangement into a question about how much of that stability comes from the chosen masses and initial velocities.

Black holes enter through density. A small object with a very large mass creates the visual impression of a black hole, and the video shows a simulation in which the Sun orbits a tiny object representing the closest black hole to us. The object is too small to see at the ordinary scale. Enlarging it by roughly a quintillion times makes the scale legible on screen.

kavan says that black holes inspired the project and describes simulating one in greater detail as a longer-term plan. The public description adds a useful piece of history: at the start of the project, kavan says, C++ syntax was still unfamiliar. The video presents the finished highlights, while the learning happened across the attempts that the edit leaves out.

Limits of the demonstration

The source gives a working visual explanation rather than a complete account of numerical physics or OpenGL. It shows the inverse-square Newtonian force calculation, the conversion to acceleration, the rendered orbits, and the later paraboloid surface. It does not specify the timestep, integration method, unit conversions used by the screen, collision handling between bodies, exact mass scaling, or the stability criteria behind the selected velocities. The solar-system scene therefore demonstrates the behaviour kavan obtained in this project rather than a physically scaled model of our system.

The relativity section has the same boundary. The video accurately presents the intuition that free fall follows spacetime geometry and that a standing person feels the normal force from the ground. Its grid is a visual analogy built from a paraboloid. The source’s captions do not preserve the displayed equation in a form that can be checked here, and the video itself describes the earlier 3D grid as inaccurate. Those limits matter because a convincing surface can suggest a more complete physical theory than the program contains.

Further reading / references

20 paragraphs1,328 words8,370 characters