# brainrot 002: criticality simulation Live: https://brainrot.runfridge.dev/002/ Source: https://github.com/hwhang0917/brainrot/tree/main/002 License: MIT (see LICENSE at repo root); attribution only, no warranty. ## What it is A box of glowing nuclei drifting like lava-lamp blobs, with neutrons flying between them. Nuclei spontaneously emit neutrons; a neutron that hits a nucleus may fission it (flash ring, heat, 2–3 new neutrons), and a chain reaction can run away. A "nuclei" slider at the bottom sets the fuel; a side gauge shows core temperature in °C and reads MELTDOWN near the top. Every emission and fission is a Geiger click (audio starts muted; the sound gate asks on load). Buttons: 📺 CRT, mute, eye (hide controls). No URL params. ## How it was built - pixi.js 8: blobs drawn into a `Graphics` inside a container with a half-resolution `BlurFilter` (the lava-lamp look), sharper cores and neutrons on top, background colour lerped toward red with temperature. The heat colour stops are duplicated in index.html's #gauge gradient and `HEAT_STOPS` in main.ts. - Model (sim.ts), units are CSS pixels: nuclei have heat; the bottom 15% of the box heats them, hotter rises and cooler sinks (buoyancy), with wobble, drag and O(n²) pairwise repulsion. Neutrons fly straight at 350 px/s, leak out of the box edges, or hit a nucleus: fission with probability 0.45, otherwise absorbed. Spontaneous emission 0.08 per nucleus per second. Caps: 1500 neutrons, 600 nuclei. Temperature rises 0.004 per fission and cools 25%/s. - Every random draw goes through an injectable `rng` so tests can seed it (lib/rng.ts `seeded`). - From the git history: the Geiger tick was first three full-band snaps per frame; it became a 12 ms bandpassed noise burst through a compressor, rate-limited to ~40 randomly spaced ticks a second. The audio was then split into audio.ts and is reused by 006. Most recently the slider stopped being a raw nucleus count: because neutrons leak out of the edges, a fixed count was a bomb on a phone and a dud on a desktop. The slider is now "fuel", and `fuelToCount` scales it by (box area / 390×844)^0.65 (measured, between a leak-only sqrt and linear), re-derived on resize. - Vibe coded: prompted into existence with a coding agent, iterated commit by commit. ## Files - 002/index.html: page shell, meta, CSS for the slider, gauge, CRT and mute buttons. - 002/main.ts: Pixi scene, render loop, flashes, gauge, slider wiring, mute; imports `mountCrt` (lib/crt.ts), `mountHud` (lib/hud.ts), `mountSoundGate` (lib/soundGate.ts). - 002/sim.ts: pure criticality model (`create`, `step`, `setFuel`, `setCount`, `resize`, `fuelToCount`, constants). - 002/audio.ts: `geiger()`: lazily unlocked AudioContext, compressor, mute gain, rate-limited bandpassed click; also imported by 006/main.ts. - 002/sim.test.ts: node tests for fission, leakage, setCount, cooling, fuel scaling, and seeded subcritical-vs-runaway on phone and desktop boxes. ## Extending it - Run: `npm install && npm run dev`, open http://localhost:5173/002/ . Tests: `npm test`. Typecheck+build: `npm run build`. - Physics knobs: constants at the top of sim.ts (FISSION_PROB, EMIT_RATE, NEUTRON_SPEED, FUEL_EXP, MIN/MAX/DEFAULT_FUEL…). Visual knobs: top of main.ts. - Invariant guarded by tests: DEFAULT_FUEL (40) stays subcritical and MAX_FUEL (200) runs away on both 390×844 and 1920×1080. Retune FUEL_EXP or FISSION_PROB only with that test passing. - Neutrons spawn just outside the emitter's hit radius, or they would re-fission their own nucleus next frame. - `step` takes `rng` and `fissionProb` parameters; keep randomness injectable. `sim.events` is the per-step hook for flashes and clicks. - audio.ts is shared with 006: changing `geiger()`'s return shape (`click`, `mute`, `unlock`, `context`, `output`) breaks the Chernobyl page. - Conventions: keep pure simulation in its own module with a `node --test` file beside it; match the surrounding code style; Conventional Commits `type(002): message`.