Skip to content
Galaxy Sim: Real-Time N-Body Galaxy Formation in WebGL

Galaxy Sim: Real-Time N-Body Galaxy Formation in WebGL

Watch 20,000 particles collapse from random gas clouds into a spinning spiral galaxy, all running in your browser. The result is quietly mesmerizing.

Details

Published

What it is

Open Galaxy Sim and you're watching 20,000 particles forming to become a spiral galaxy. Gas clouds drift in from random positions around a black hole, collide into a slowly flattening disk, and start glowing as stars fade into existence. It's an N-body simulation running entirely in your browser via WebGL and Three.js, and the whole thing has a quiet, hypnotic quality to it.

The project comes from Andrew Campbell as a final project for Berkeley's CS184 computer graphics course, built with two collaborators and extended from @magwo's fullofstars repo. The clever trick here is how it handles the brutal O(n²) pairwise gravity problem: rather than computing every interaction every frame, only a subset of star-to-star forces update per timestep, while forces from the dominant black holes stay current. Pairwise gravity refreshes roughly every 60 frames, which is the kind of principled shortcut that makes real-time possible without the scene falling apart.

Play with the controls panel (press h to toggle it), crank the gravitational strength slider, or spawn multiple galaxies and watch them collide. Press 2 for a guided camera, r for autorotation, and a for... something. The source code is clean and readable, and Andrew wrote a detailed project report walking through the physics model and every hack that made it work.

How it’s made

Technology
WebGL
Framework & Tool
Three.js
Subject
Space