fuzzyworld

tower / files / src / terrain / falls.ts

falls.ts

Blame: the commit that last changed each line · file · history

80bcc97a 2026-09-17 Pavel Safronov1/**
2 * Waterfall sheets. See world-generation.md §4.3 and design.md §1.9.2.
3 *
4 * A fall is where a river's surface steps down (compile.ts). This builds the falling
5 * water: a sheet leaving the lip at the river's speed, dropping under gravity, and —
6 * because everything that falls in Tower carries the spin — drifting **antispinward** as
7 * it goes, by exactly the free-fall drift canon/motion.ts computes. A kilometre-high fall
8 * lands 133 m antispinward of the lip. The waterfall is the compass (§1.9.2); this is the
9 * first time the view draws one.
10 *
11 * Pure geometry, no Three.js, so the lean can be tested against the canon it claims.
12 */
13import { RADIUS_M } from '../canon/constants.js'
14import { freeFallDriftM } from '../canon/motion.js'
15import type { CFall } from './compile.js'
16
17export interface FallSheet {
18 /** The lip's centre, km, tower frame: where the mesh is placed. */
19 origin: [number, number, number]
20 /** Vertices relative to the origin, km. */
21 positions: Float32Array
22 /** u across the sheet 0..1, v down it 0..1. */
23 uvs: Float32Array
24 indices: Uint16Array
25 heightM: number
26}
27
28export const FALL_ROWS = 32
29export const FALL_COLS = 6
30
31/**
32 * Where a drop of water that left the lip at `offset` metres across it is, `y` metres
33 * down: in (z, a) on the unrolled floor and in elevation.
34 */
35export function fallPoint(f: CFall, offset: number, y: number): { z: number; a: number; elevationM: number } {
36 const t = Math.sqrt((2 * y) / 9.80665)
37 const forward = f.speedMS * t
38 // Across the flow: (dz, da) turned a quarter to spinward.
39 const acrossZ = -f.da, acrossA = f.dz
40 return {
41 z: f.z + f.dz * forward + acrossZ * offset,
42 // Antispinward is -a: the drift is subtracted.
43 a: f.a + f.da * forward + acrossA * offset - freeFallDriftM(y),
44 elevationM: f.topM - y,
45 }
46}
47
48export function fallSheet(f: CFall): FallSheet {
49 const height = f.topM - f.bottomM
50 const cos = Math.cos(f.a / RADIUS_M), sin = Math.sin(f.a / RADIUS_M)
51 const r0 = (RADIUS_M - f.topM) / 1000
52 const origin: [number, number, number] = [r0 * cos, r0 * sin, f.z / 1000]
53 const positions = new Float32Array((FALL_ROWS + 1) * (FALL_COLS + 1) * 3)
54 const uvs = new Float32Array((FALL_ROWS + 1) * (FALL_COLS + 1) * 2)
55 let k = 0
56 for (let row = 0; row <= FALL_ROWS; row++) {
57 // Rows bunched toward the lip, where the sheet curves most.
58 const v = (row / FALL_ROWS) ** 1.6
59 for (let col = 0; col <= FALL_COLS; col++) {
60 const u = col / FALL_COLS
61 const p = fallPoint(f, (u - 0.5) * f.widthM, v * height)
62 const theta = p.a / RADIUS_M
63 const r = (RADIUS_M - p.elevationM) / 1000
64 positions[3 * k] = r * Math.cos(theta) - origin[0]
65 positions[3 * k + 1] = r * Math.sin(theta) - origin[1]
66 positions[3 * k + 2] = p.z / 1000 - origin[2]
67 uvs[2 * k] = u
68 uvs[2 * k + 1] = v
69 k++
70 }
71 }
72 const idx: number[] = []
73 for (let row = 0; row < FALL_ROWS; row++) {
74 for (let col = 0; col < FALL_COLS; col++) {
75 const a = row * (FALL_COLS + 1) + col
76 const b = a + 1, c = a + FALL_COLS + 1, d = c + 1
77 idx.push(a, c, b, b, c, d)
78 }
79 }
80 return { origin, positions, uvs, indices: new Uint16Array(idx), heightM: height }
81}