|
| 1 | +// Ripple impacts: presentational events (near-miss shivers) that the particle |
| 2 | +// substrate reads. Like `crashFlashSeconds`, these live on RunState, are |
| 3 | +// computed in updateRun, and never touch collision — a cube that is grazed and |
| 4 | +// a cube that is hit are decided by the same pure (s, θ) rects; this module only |
| 5 | +// decides how the *rendered* world reacts to a graze. |
| 6 | +// |
| 7 | +// The ripple constants and `impactRadialOffset` below are the TS twin of the |
| 8 | +// GLSL in src/render/particles/displacement.glsl.ts. The point skin ripples on |
| 9 | +// the GPU; cubes shiver on the CPU (a radial nudge, see obstacles.ts); both read |
| 10 | +// these same numbers so the tunnel wall and the cube it surrounds ripple as one. |
| 11 | + |
| 12 | +import { cellCenterS, LANE_ANGLE, TUBE_RADIUS } from "../tube/space"; |
| 13 | +import { angleForLane, lanesFromMask, type LaneMask } from "./coordinates"; |
| 14 | + |
| 15 | +export type RippleImpact = { |
| 16 | + readonly s: number; |
| 17 | + readonly theta: number; |
| 18 | + readonly age: number; // seconds since the graze |
| 19 | + readonly strength: number; // [0, 1] |
| 20 | +}; |
| 21 | + |
| 22 | +export const MAX_IMPACTS = 16; |
| 23 | +export const IMPACT_LIFETIME_SECONDS = 0.9; |
| 24 | + |
| 25 | +// Shared ripple shape (see the GLSL twin). Amplitude is the world-space peak |
| 26 | +// radial displacement; the rest shape the expanding, decaying ring. |
| 27 | +export const IMPACT_AMPLITUDE = 0.9; |
| 28 | +export const IMPACT_FREQUENCY = 1.4; |
| 29 | +export const IMPACT_SPEED = 7; |
| 30 | +export const IMPACT_FALLOFF = 0.06; |
| 31 | + |
| 32 | +// A pass closer than the inner edge would have clipped the cube (player |
| 33 | +// half-angle 0.3 + cube half-angle 0.5 = 0.8 lanes); beyond the outer edge the |
| 34 | +// pass is too wide to feel. Strength runs 1 at the grazing edge to 0 at the |
| 35 | +// outer edge. |
| 36 | +const NEAR_MISS_INNER = 0.8 * LANE_ANGLE; |
| 37 | +const NEAR_MISS_OUTER = 2.2 * LANE_ANGLE; |
| 38 | + |
| 39 | +const wrapAngle = (angle: number): number => Math.atan2(Math.sin(angle), Math.cos(angle)); |
| 40 | + |
| 41 | +const smoothstep = (edge0: number, edge1: number, x: number): number => { |
| 42 | + const t = Math.min(1, Math.max(0, (x - edge0) / (edge1 - edge0))); |
| 43 | + return t * t * (3 - 2 * t); |
| 44 | +}; |
| 45 | + |
| 46 | +// Strength of a near miss at a given angular gap. 0 for a hit (gap ≤ inner) or a |
| 47 | +// wide pass (gap ≥ outer); rises monotonically as the gap narrows toward the |
| 48 | +// grazing edge. |
| 49 | +export const nearMissStrength = (angleGap: number): number => { |
| 50 | + const gap = Math.abs(angleGap); |
| 51 | + if (gap <= NEAR_MISS_INNER || gap >= NEAR_MISS_OUTER) { |
| 52 | + return 0; |
| 53 | + } |
| 54 | + const t = (NEAR_MISS_OUTER - gap) / (NEAR_MISS_OUTER - NEAR_MISS_INNER); |
| 55 | + return t * t * (3 - 2 * t); |
| 56 | +}; |
| 57 | + |
| 58 | +export type NearMissFrame = { |
| 59 | + readonly absoluteCell: number; |
| 60 | + readonly obstacleMask: LaneMask; |
| 61 | +}; |
| 62 | + |
| 63 | +// Emit a ripple the single frame the player passes a cube's centre without |
| 64 | +// hitting it. Firing on the centre-crossing makes it exactly once per pass, with |
| 65 | +// no growing "already seen" set to carry on RunState. |
| 66 | +export const detectNearMisses = ( |
| 67 | + frames: readonly NearMissFrame[], |
| 68 | + previousDistance: number, |
| 69 | + currentDistance: number, |
| 70 | + angle: number, |
| 71 | +): readonly RippleImpact[] => { |
| 72 | + const impacts: RippleImpact[] = []; |
| 73 | + |
| 74 | + for (const frame of frames) { |
| 75 | + const centerS = cellCenterS(frame.absoluteCell); |
| 76 | + const crossedThisFrame = previousDistance < centerS && centerS <= currentDistance; |
| 77 | + |
| 78 | + if (!crossedThisFrame) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + for (const lane of lanesFromMask(frame.obstacleMask)) { |
| 83 | + const laneAngle = angleForLane(lane); |
| 84 | + const strength = nearMissStrength(wrapAngle(angle - laneAngle)); |
| 85 | + |
| 86 | + if (strength > 0) { |
| 87 | + impacts.push({ s: centerS, theta: laneAngle, age: 0, strength }); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return impacts; |
| 93 | +}; |
| 94 | + |
| 95 | +// Age the live ripples, drop the expired, append the new, and keep the freshest |
| 96 | +// MAX_IMPACTS (the ring buffer the shader reads). |
| 97 | +export const advanceImpacts = ( |
| 98 | + impacts: readonly RippleImpact[], |
| 99 | + detected: readonly RippleImpact[], |
| 100 | + dtSeconds: number, |
| 101 | +): readonly RippleImpact[] => { |
| 102 | + const aged = impacts |
| 103 | + .map((impact) => ({ ...impact, age: impact.age + dtSeconds })) |
| 104 | + .filter((impact) => impact.age < IMPACT_LIFETIME_SECONDS); |
| 105 | + const merged = [...aged, ...detected]; |
| 106 | + return merged.length <= MAX_IMPACTS |
| 107 | + ? merged |
| 108 | + : merged.slice(merged.length - MAX_IMPACTS); |
| 109 | +}; |
| 110 | + |
| 111 | +// TS twin of the GLSL ripple sum: the world-space radial displacement at a point |
| 112 | +// in tube space. Cubes use it to shiver; the point skin computes the same on the |
| 113 | +// GPU for the surrounding wall. |
| 114 | +export const impactRadialOffset = ( |
| 115 | + s: number, |
| 116 | + theta: number, |
| 117 | + impacts: readonly RippleImpact[], |
| 118 | +): number => { |
| 119 | + let total = 0; |
| 120 | + |
| 121 | + for (const impact of impacts) { |
| 122 | + if (impact.strength <= 0) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + const ds = s - impact.s; |
| 126 | + const dTheta = wrapAngle(theta - impact.theta); |
| 127 | + const dist = Math.hypot(ds, dTheta * TUBE_RADIUS); |
| 128 | + const ageNorm = Math.min(1, impact.age / IMPACT_LIFETIME_SECONDS); |
| 129 | + const envelope = |
| 130 | + smoothstep(0, 0.12, ageNorm) * (1 - smoothstep(0.45, 1, ageNorm)); |
| 131 | + const ring = Math.sin(dist * IMPACT_FREQUENCY - impact.age * IMPACT_SPEED); |
| 132 | + const spatial = Math.exp(-dist * dist * IMPACT_FALLOFF); |
| 133 | + total += impact.strength * envelope * ring * spatial; |
| 134 | + } |
| 135 | + |
| 136 | + return total * IMPACT_AMPLITUDE; |
| 137 | +}; |
0 commit comments