A dusty disk grows rocky worlds, ice giants and gas giants by N-body accretion.
A young star is circled by thousands of planetesimals and a few dozen embryos. Embryos pull on everything, planetesimals feel the star and the embryos and pull back on the embryos, so dynamical friction and scattering come out of the same sums; planetesimals take block time steps by orbital period so the slow outer disk costs as little as the fast inner one. Collisions merge perfectly within a few Hill radii, conserving mass and momentum. While the gas lasts it drags solids inward until they pile up in pressure traps at the inner edge and just past the frost line, damps eccentricities, drives type I migration, and lets icy cores above about three Earth masses run away into gas giants that carve gaps in the glowing gas. Then the gas clears, embryos stir each other into giant impacts, and the strip at the bottom compares the final system with earlier runs.
Try it. Click for a new system from a new seed. Drag to tilt and turn the disk. F or Space fast-forwards, arrow keys tilt and rotate. When a system is finished it is added to the comparison strip, and an idle view forges the next one.
Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.
Build a protoplanetary disk that grows its own planets, using JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.
Start simple:
- Use units where the star's GM is 1 and draw it at the center of a canvas that fills the window. Place about 1,500 small planetesimals on nearly circular orbits between radius 0.3 and 2.5, each with speed sqrt(1 / r) and a little random eccentricity.
- Add about 20 heavier embryos spread through the same range. Only embryos have gravity of their own: planetesimals feel the star and the embryos, embryos feel the star, each other and the planetesimals.
- Step everything with a symplectic (leapfrog) integrator and a time step small enough for the innermost orbit.
- When a planetesimal comes within a couple of Hill radii, r * (m / 3)^(1/3), of an embryo, merge it in, conserving mass and momentum. Do the same when two embryos collide.
- Draw planetesimals as dots and embryos as circles that grow with their mass.
Once that works, make it beautiful:
- Add a frost line at about radius 1.3: solids beyond it carry ice, are more plentiful and are drawn pale blue, while rocky ones inside are rust colored.
- Draw a glowing gas disk behind everything that slowly fades away, and while it lasts let it damp eccentricities and let big icy cores swell into banded gas giants.
- Draw each planet's orbit as a Kepler ellipse computed from its position and velocity, and click to reseed a new system.
Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as gas drag that traps solids at the frost line, migration of embryos through the gas, or a strip that compares the final systems from several runs.