A spiral galaxy forming in real time as thousands of glowing particles flatten from a spherical cloud into a rotating disk around a central black hole, rendered in WebGL.
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.
- Live Demo: https://andrewdcampbell.github.io/galaxy-sim
- Source Code: https://github.com/andrewdcampbell/galaxy-sim
- Author: Andrew Campbell (LinkedIn)