# brainrot 007: three-body gravity sandbox Live: https://brainrot.runfridge.dev/007/ Source: https://github.com/hwhang0917/brainrot/tree/main/007 License: MIT (see LICENSE at repo root); attribution only, no warranty. ## What it is An N-body gravity sandbox on a starfield. It opens on the figure-eight three-body orbit; bodies glow as tinted suns with coronas and slowly turning rays, and leave fading trails. Drag on the canvas to fling a new sun in (a ghost and an arrow show start and launch velocity), tap to drop one at rest; the mass slider (logarithmic, 0.1–10) or the mouse wheel sets its mass. Preset buttons: figure eight, lagrange, binary + planet, solar system, random, and clear. Bodies that touch merge; a body far out and heading away is ejected. A HUD shows body count, merged, ejected, total energy and time. Buttons: 📺 CRT and eye (hide controls). No sound, no URL params. ## How it was built - pixi.js 8. Stars and trails live in world units on a scaled container (7 world units across the shorter screen side); bodies are screen-space sprites. Each body is four sprites from textures painted once on a 2D canvas: limb-darkened disc, untinted white core, corona and a ray fan (corona and rays in an additive-blend container). Trails are 420 points per body drawn as 7 polylines of rising alpha. - Model (gravity.ts): natural units, G = 1, unit masses at distances of about one, so the figure-eight period is 6.3259 and loops every ~6 s at one sim second per real second. Kick-drift-kick leapfrog with fixed 0.002 substeps keeps energy honest through close passes; softening 0.03. Body radius = 0.07·cbrt(m). Merges conserve momentum and keep the heavier body's colour. Ejection beyond r=7 when moving outward. Max 24 bodies. - Presets: Chenciner & Montgomery figure-eight initial conditions; Lagrange equilateral triangle (unstable on purpose, round-off breaks it up); binary + light planet; random three; solar system with real mass ratios to a Sun of mass 4, orbits squeezed so Neptune fits, circular speeds, and the Sun given the opposite momentum so the barycentre stays put. - From the git history: the sandbox landed first; the next commit added the solar system preset and switched stars to textured sprites with corona and rays, because a blur filter clipped big halos at its box. The renderer gives tiny planets a 3 px minimum radius. - Vibe coded: prompted into existence with a coding agent, iterated commit by commit. ## Files - 007/index.html: page shell, meta, CSS, HUD line, hint, bottom bar with mass slider and preset buttons. - 007/main.ts: textures, Pixi scene, fit, drag-to-fling, mass slider/wheel, presets, trails, per-body sprite bookkeeping (`looks` Map, destroyed when a body merges or leaves), HUD; imports `hsl` (lib/color.ts), `mountCrt` (lib/crt.ts), `mountHud` (lib/hud.ts). - 007/gravity.ts: pure N-body model (`create`, `step`, `add`, `energy`, `momentum`, `radius`, `PRESETS`, constants). - 007/gravity.test.ts: figure-eight conserves energy (<1e-4) and returns near its start after 3 periods; merges conserve momentum; eject vs keep; solar system keeps all 9 bodies on their orbits for 40 s. ## Extending it - Run: `npm install && npm run dev`, open http://localhost:5173/007/ . Tests: `npm test`. Typecheck+build: `npm run build`. - Physics knobs: top of gravity.ts (SOFTENING, SUBSTEP, BODY_R, EJECT_R, MAX_BODIES). Look and input knobs: top of main.ts (VIEW, TRAIL_LEN, FLING, MASS_MIN/MAX, CORONA_R, RAYS_R…). - New preset: add a function to `PRESETS` in gravity.ts and a `