# brainrot 014: ship in a wave tank Live: https://brainrot.runfridge.dev/014/ Source: https://github.com/hwhang0917/brainrot/tree/main/014 License: MIT (see LICENSE at repo root); attribution only, no warranty. ## What it is A model basin drawn as a blueprint-style drawing-office sheet. A side view shows a cutaway ship floating in GPU-simulated water, with a paddle wavemaker at the left and a damping beach at the right. A select picks the hull: galley, RMS Titanic, VLCC oil tanker or large cruise ship. Changing it reloads the page with `?ship=`. Sliders set wave height (0-20 m), period (4-20 s) and chop, and a "Rogue wave" button swells the paddle for one period. Readouts show a wave-height scale, probe level, mean level, hull status (afloat/flooding/broken/sunk), draft, trim, a hull-stress scale as % of the bending limit, and a flooded bar per compartment. There is no sound and no click interaction on the water. URL params: - `?ship=box|galley|titanic|tanker|cruise`; box is the calibration barge and is not in the select. - `?tier=phone|desktop` forces the grid size. - `?h=&t=&chop=` preset the sliders. - `?hold` locks the hull in place: forces are read and nothing breaks. - `?debug` paints the pressure field (red positive, blue negative). ## How it was built - **Water.** A 2D FLIP/PIC solver runs entirely in WebGL2 float textures (`fluid.ts`, all GLSL in one file). Particles live in an RGBA32F texture, 4 per cell. Each substep runs: - particle-to-grid transfer onto a MAC-staggered grid; - velocity extrapolation into the air; - a red-black SOR pressure solve (SOR 1.8, 1500 warm-up sweeps at fill so the hydrostatic field exists before anything moves); - grid-to-particle with PIC_BLEND 0.1, a CFL clamp of 0.5 cells per substep, and push-out from walls. - **Grid tiers.** Desktop is 512×256 with 2 substeps and 48 iterations. Phone is 256×128 with 1 substep and 32 iterations. DT is 1/60. - **Walls.** Walls are line segments rasterised into a solid texture, tagged with each segment's velocity and index. A breached plate just goes inactive, so flooding is not modelled: water flows in. Units are cells and seconds, G_SIM = 60 cells/s². - **Ship.** The ship is a CPU rigid body (two once it breaks) in `body.ts`. It is fed by per-segment pressure forces that a GPU reduce pass computes and that are read back one step late through a pixel pack buffer. Buoyancy, slamming and flood water all arrive as pressure, so trim and sinking follow from it. On top of that the ship has: - physical added mass; - heave and pitch damping; - a soft mooring; - a bending moment against a material capacity (wood, riveted steel from 1912, modern steel); - plate breaches when net pressure stays over the slam limit, in metres of head, for BREACH_SECONDS; - a break in two after BREAK_SECONDS over the limit; - a sticky flood lamp. - **Waves.** `waves.ts` Froude-scales each ship (λ m/cell, time scale √(G_SIM·λ/g)). It solves the dispersion relation by Newton's method and sets the paddle stroke with the Biésel piston formula × PADDLE_GAIN 1.6. - **Hard-won rules from the history.** They are commented at the top of each constant; don't change them casually: - apply gravity on the grid side of the FLIP delta, because the delta was lifting the water; - the density correction is one-sided (crowding only) and local over a 3×3 window; - capsule push-out goes against the real segment, fetched by index; - plates are drawn after bulkheads so corners never push water inside; - pressure samples are taken SAMPLE_OFF cells off the wall and brought back to the plate line; - interior walls only feel water inside the hull; - slam limits must be sustained, because the solver throws spikes. - **Later changes.** A frame-rate watchdog demotes a desktop to the phone tier. Uniform locations are cached. A dummy attribute stops the browser faking a quarter-million-vertex buffer per draw. The flood survey runs in 32 strips per compartment. MAX_SEGMENTS went from 192 to 384 because the cruise ship has 277 walls. - The page was vibe coded: prompted into existence with a coding agent and iterated commit by commit. ## Files - 014/index.html: the drawing-office sheet layout, controls, linear scales (`.rule` data-max/data-step), and the WebGL2-unsupported "Dry dock" note. - 014/main.ts: glue. It handles tier choice, ship setup, hull fill, probes, slider-to-paddle via Froude scaling, the fixed-step loop (max 4 steps/frame, one step on a struggling frame), readouts, the watchdog and the debug hooks. Imports `lib/crt.ts`. - 014/fluid.ts: the GPU FLIP solver, TIERS, the solver constants, `createFluid` (step, reduce, readback, fill, setSegments, debug column reads), and `compileProgram`. - 014/draw.ts: one full-screen fragment pass that draws water, foam, walls, the cutaway art through the body transform, flood water and breaches. Also `paintCutaway` (Canvas2D hull skin, paint, frames, deco). - 014/body.ts: rigid body(s), readback to forces, bending/stress, breaches, `breakAt`, status, and `instruments` (draft, trim, stress %). - 014/ships.ts: the hull catalogue in metres (BOX, GALLEY, TITANIC, TANKER, CRUISE), materials, `buildHull` (plates split at MAX_SEGMENT_CELLS, stations), and polygon area helpers. - 014/tank.ts: shared basin constants (G_SIM, WATER_LEVEL, PADDLE_X0, BEACH, SPONGE), body indices, Segment/Body/Pose types, and tank walls. - 014/waves.ts: wavenumber, pistonStroke, scale, and `paddleX` (ramp, chop harmonic, raised-cosine rogue). - 014/body.test.ts, ships.test.ts, waves.test.ts: pure node tests. These cover barge draft, bending wL²/8, flooding/breach/break from a synthetic readback, every hull floating and fitting MAX_SEGMENTS, dispersion limits, and paddle ramp/rogue. - 014/page.test.ts: headless-browser tests. These cover still water staying flat, the barge at its design draft, a bow hole trimming the Titanic by the head, and no sideways scroll on a phone. ## Extending it - Run: `npm install && npm run dev`, open http://localhost:5173/014/ . Tests: `npm test`. Typecheck+build: `npm run build`. - Debug hooks on `window`: - `__debug()`: forces by station, stress, body state and raw readback; - `__breach(i)` and `__breachNear(fx, fy)`: tear a hull plate open by index, or the one nearest a point given as fractions of length and height; - `__inside()`: particles inside the hull by row and column band, to find a leak; - `__column(x)`, `__dens(x)`, `__solid(x)`: pressure, density and solid columns; - `__hist(x0,x1,y0,y1)`: a particle histogram. - Knobs: - solver constants at the top of `fluid.ts`; - body/structure constants at the top of `body.ts`; - FLUID_DENSITY_CAL, the knob that puts the barge at its design draft; - per-ship compartments, weights, materials and stations in `ships.ts`; - paddle gains in `waves.ts`. - Adding a ship: add it to `SHIPS` in ships.ts. ships.test.ts checks that it closes, floats, has compartment weights summing to 1, and fits `MAX_SEGMENTS` with the tank walls on every tier. body.test.ts checks that still-water bending stays well inside the limit. - Two invariants to keep. GLSL constants are spliced in through `f(n)`, because GLSL ES has no int-to-float promotion. Every draw is indexed by `gl_VertexID`, which is why the dummy attribute exists. - Browser tests use `test/browser.ts` (vite plus headless chrome over CDP with SwiftShader). They skip when no Chrome is found (set CHROME_PATH), so CI skips them. Software rendering runs the tank much slower than real time, so waits need generous slack (FLOOD_MS 45 s). - Conventions: keep pure simulation in its own module with a `node --test` file beside it; match the surrounding code style; Conventional Commits `type(014): message`.