Forming a Planet
This project is a variation on Newton's Revenge, a demo built with the PhysicsJS library by Jasper Palfree. It simulates the early stages of planetary formation: dozens of particles pull on each other through Newtonian gravity, clump together into a spinning mass, break apart, drift for a while, then come back together again.
How Planets Actually Form
The process shown here is a simplified version of what happens in nature. After a star ignites, the remaining gas and dust in its solar system flattens into a rotating protoplanetary disk. Within that disk, rocky particles collide and stick together, forming larger and larger clumps. As these clumps grow, their gravity strengthens, attracting even more material. Eventually they become planetesimals (bodies roughly a kilometer across), which collide with one another to build up the rocky inner planets. Meanwhile, in the cooler outer regions, gases freeze into giant balls that become gas giants like Jupiter and Saturn.
This process, called accretion, is driven almost entirely by gravity. The visualization above captures the essence of it: small bodies attracting one another, colliding, and gradually assembling into larger structures.
The Simulation
The simulation works by computing gravitational attraction between every pair of particles on each frame. Each particle has a mass proportional to its radius, and the gravitational force between any two particles follows Newton's law of universal gravitation: force is proportional to the product of their masses and inversely proportional to the square of the distance between them.
When particles collide, an impulse response pushes them apart based on their relative velocity and masses. The edges of the canvas act as walls with slight energy loss on each bounce (a restitution of 0.99), which keeps the system from becoming too chaotic.
The values are tuned so the simulation stays interesting. Gravity is strong enough to pull particles into clumps, but the elastic collisions prevent them from permanently sticking together. The result is a cycle: particles drift, gravity pulls them into a spinning cluster, collisions at the center fling some particles outward, and the whole process repeats.
Why It Doesn't Settle Down
In a real planetary system, collisions dissipate energy as heat, so objects eventually merge permanently. Here, collisions are nearly perfectly elastic (restitution close to 1), meaning very little energy is lost. This keeps the system in a perpetual state of assembly and disassembly, which is more visually interesting than a stable clump sitting in the center.
Click and hold anywhere on the canvas to pull particles toward your cursor. This acts as an attractor with its own gravitational pull, letting you gather particles into dense clusters or scatter a forming planet by dragging through it.
Emergence in Gravity
What makes gravitational simulations compelling is that the global structure (a spinning planet-like mass) emerges from nothing but pairwise interactions between simple particles. No particle knows about the larger pattern. Each one simply accelerates toward every other particle according to a single equation. Yet the collective result is organized, dynamic, and strikingly reminiscent of real astrophysical processes.
The same principle operates at every scale in the universe, from dust grains in a protoplanetary disk to galaxies clustering along cosmic filaments. Gravity is the simplest of forces, yet it builds the most complex structures in nature.