SOURCE / PINNED RELEASE
Made of little things.
winniegolf
- Release
- 017b791c0f4b…
- Author-recorded commit
- 612845c125fd…
- License
- LICENSE
- Author’s source reference
- nostr://npub18weq5jzw27gkhwcuf6gu57alcptjdvh78nlulgpkz8s7pnlxfnnspc00s9/wss%3A%2F%2Fgit.napplet.soy%2F/n-2d501957e21
Archive hash verified: 713b42364f27a576…. The source-to-build association is the author’s claim; it has not been independently rebuilt.
import type { Ball, Hit, Level, Rect, Spinner, Surface, Vec } from './types.js';
export const BALL_R = 13;
export const HOLE_R = 19;
export const MAX_SHOT_SPEED = 620;
export const MIN_SHOT_SPEED = 95;
export const MAX_SPEED = 720;
export const GREEN_DECEL = 135;
/** Bunkers are a real carry: contact with the sand kills speed hard. */
export const SAND_DECEL = 850;
export const WALL_RESTITUTION = 0.72;
export const BUMPER_RESTITUTION = 1.14;
export const STOP_SPEED = 7;
export const SINK_SPEED = 250;
/** Cup assist: a slow ball this close is drawn the rest of the way in. */
export const PULL_RADIUS = HOLE_R + 6;
export const PULL_ACCEL = 460;
export const PULL_MAX_SPEED = 250;
const SUBSTEP = 1 / 300;
const MAX_SUBSTEPS = 60;
export interface StepEvents {
sunk: boolean;
wet: boolean;
rail: boolean;
bumper: boolean;
at: Vec | null;
}
export function emptyEvents(): StepEvents {
return { sunk: false, wet: false, rail: false, bumper: false, at: null };
}
export function pointInRect(x: number, y: number, rect: Rect): boolean {
return x >= rect.x && x <= rect.x + rect.w && y >= rect.y && y <= rect.y + rect.h;
}
/** Circle versus axis-aligned rect. Returns the push-out normal and depth. */
export function circleRectHit(cx: number, cy: number, r: number, rect: Rect): Hit | null {
const nearX = Math.max(rect.x, Math.min(cx, rect.x + rect.w));
const nearY = Math.max(rect.y, Math.min(cy, rect.y + rect.h));
const dx = cx - nearX;
const dy = cy - nearY;
const d2 = dx * dx + dy * dy;
if (d2 > r * r) return null;
if (d2 > 1e-9) {
const d = Math.sqrt(d2);
return { nx: dx / d, ny: dy / d, depth: r - d };
}
const left = cx - rect.x;
const right = rect.x + rect.w - cx;
const top = cy - rect.y;
const bottom = rect.y + rect.h - cy;
const nearest = Math.min(left, right, top, bottom);
if (nearest === left) return { nx: -1, ny: 0, depth: left + r };
if (nearest === right) return { nx: 1, ny: 0, depth: right + r };
if (nearest === top) return { nx: 0, ny: -1, depth: top + r };
return { nx: 0, ny: 1, depth: bottom + r };
}
/**
* Sand catches the ball on contact — the circle dipping into the bunker is
* enough, not only the centre. That is what makes the sand a real hazard
* instead of a patch you can skim.
*
* Water is the opposite rule: only the centre leaving the land loses the
* ball, so the edge may hang over the water and a narrow bridge stays
* threadable. See `isLand`.
*/
export function surfaceAt(x: number, y: number, level: Level): Surface {
for (const rect of level.sand) {
if (circleRectHit(x, y, BALL_R, rect)) return 'sand';
}
return 'green';
}
/**
* Water loss is a centre test only. A ball whose edge is over the water is
* still dry as long as the centre is on land — that is the threading rule.
*/
export function isLand(x: number, y: number, level: Level): boolean {
if (!level.land) return true;
for (const rect of level.land) if (pointInRect(x, y, rect)) return true;
return false;
}
export function spinnerAngle(spinner: Spinner, time: number): number {
return spinner.phase + spinner.speed * time;
}
function clampSpeed(ball: Ball): void {
const sp = Math.hypot(ball.vx, ball.vy);
if (sp > MAX_SPEED) {
const k = MAX_SPEED / sp;
ball.vx *= k;
ball.vy *= k;
}
}
function resolveRails(ball: Ball, level: Level, out: StepEvents): void {
for (let pass = 0; pass < 3; pass += 1) {
let touched = false;
for (const rect of level.walls) {
const hit = circleRectHit(ball.x, ball.y, BALL_R, rect);
if (!hit) continue;
ball.x += hit.nx * hit.depth;
ball.y += hit.ny * hit.depth;
const vn = ball.vx * hit.nx + ball.vy * hit.ny;
if (vn < 0) {
ball.vx -= (1 + WALL_RESTITUTION) * vn * hit.nx;
ball.vy -= (1 + WALL_RESTITUTION) * vn * hit.ny;
out.rail = true;
out.at = { x: ball.x, y: ball.y };
}
touched = true;
}
if (!touched) break;
}
clampSpeed(ball);
}
function resolveBumpers(ball: Ball, level: Level, out: StepEvents): void {
for (const bumper of level.bumpers) {
const dx = ball.x - bumper.x;
const dy = ball.y - bumper.y;
const d = Math.hypot(dx, dy);
const min = BALL_R + bumper.r;
if (d >= min || d < 1e-6) continue;
const nx = dx / d;
const ny = dy / d;
ball.x = bumper.x + nx * min;
ball.y = bumper.y + ny * min;
const vn = ball.vx * nx + ball.vy * ny;
if (vn < 0) {
ball.vx -= (1 + BUMPER_RESTITUTION) * vn * nx;
ball.vy -= (1 + BUMPER_RESTITUTION) * vn * ny;
out.bumper = true;
out.at = { x: ball.x, y: ball.y };
}
}
clampSpeed(ball);
}
function resolveSpinners(ball: Ball, level: Level, time: number, out: StepEvents): void {
for (const spinner of level.spinners) {
const angle = spinnerAngle(spinner, time);
const dx = Math.cos(angle);
const dy = Math.sin(angle);
const ax = spinner.cx - dx * spinner.half;
const ay = spinner.cy - dy * spinner.half;
const abx = dx * spinner.half * 2;
const aby = dy * spinner.half * 2;
const len2 = abx * abx + aby * aby;
let t = ((ball.x - ax) * abx + (ball.y - ay) * aby) / len2;
t = Math.max(0, Math.min(1, t));
const cx = ax + abx * t;
const cy = ay + aby * t;
let nx = ball.x - cx;
let ny = ball.y - cy;
let d = Math.hypot(nx, ny);
const min = BALL_R + spinner.thick / 2;
if (d >= min) continue;
if (d < 1e-6) {
nx = -dy;
ny = dx;
d = 1;
} else {
nx /= d;
ny /= d;
}
ball.x = cx + nx * min;
ball.y = cy + ny * min;
// The blade drags the ball along with its own surface motion.
const rx = cx - spinner.cx;
const ry = cy - spinner.cy;
const surfX = -ry * spinner.speed;
const surfY = rx * spinner.speed;
const relX = ball.vx - surfX;
const relY = ball.vy - surfY;
const vn = relX * nx + relY * ny;
if (vn < 0) {
ball.vx = relX - (1 + WALL_RESTITUTION) * vn * nx + surfX;
ball.vy = relY - (1 + WALL_RESTITUTION) * vn * ny + surfY;
out.rail = true;
out.at = { x: ball.x, y: ball.y };
}
}
clampSpeed(ball);
}
function stepOnce(ball: Ball, level: Level, dt: number, time: number, out: StepEvents): void {
const cupDx = level.cup.x - ball.x;
const cupDy = level.cup.y - ball.y;
const cupDist = Math.hypot(cupDx, cupDy);
let speed = Math.hypot(ball.vx, ball.vy);
if (cupDist < PULL_RADIUS && cupDist > 1e-3 && speed < PULL_MAX_SPEED) {
const pull = (PULL_ACCEL * dt) / cupDist;
ball.vx += cupDx * pull;
ball.vy += cupDy * pull;
speed = Math.hypot(ball.vx, ball.vy);
}
const decel = surfaceAt(ball.x, ball.y, level) === 'sand' ? SAND_DECEL : GREEN_DECEL;
if (speed > 0) {
const next = Math.max(0, speed - decel * dt);
const k = next / speed;
ball.vx *= k;
ball.vy *= k;
speed = next;
}
ball.x += ball.vx * dt;
ball.y += ball.vy * dt;
resolveRails(ball, level, out);
resolveBumpers(ball, level, out);
resolveSpinners(ball, level, time, out);
if (speed < STOP_SPEED && cupDist >= PULL_RADIUS) {
ball.vx = 0;
ball.vy = 0;
}
if (!isLand(ball.x, ball.y, level)) out.wet = true;
const afterDx = level.cup.x - ball.x;
const afterDy = level.cup.y - ball.y;
if (Math.hypot(afterDx, afterDy) < HOLE_R && Math.hypot(ball.vx, ball.vy) < SINK_SPEED) {
out.sunk = true;
}
}
/** Advance the ball by `dt` seconds of simulation time. Returns the new time. */
export function simulate(
ball: Ball,
level: Level,
dt: number,
time: number,
out: StepEvents,
): number {
let remaining = dt;
let clock = time;
let steps = 0;
while (remaining > 1e-6 && steps < MAX_SUBSTEPS) {
const h = Math.min(SUBSTEP, remaining);
stepOnce(ball, level, h, clock, out);
clock += h;
remaining -= h;
steps += 1;
if (out.sunk || out.wet) break;
}
return clock;
}
/**
* Cast a dashed preview from the ball, bouncing off rails. Used for the aim
* guide; hazards and moving blades are deliberately ignored so the line stays
* readable.
*/
export function predictPath(
origin: Vec,
dir: Vec,
level: Level,
maxDistance: number,
maxBounces: number,
): Vec[] {
const points: Vec[] = [{ x: origin.x, y: origin.y }];
let px = origin.x;
let py = origin.y;
let dx = dir.x;
let dy = dir.y;
let travelled = 0;
let bounces = 0;
const step = 4;
while (travelled < maxDistance && bounces <= maxBounces) {
let nx = px + dx * step;
let ny = py + dy * step;
let bounced = false;
for (const rect of level.walls) {
const hit = circleRectHit(nx, ny, BALL_R, rect);
if (!hit) continue;
nx += hit.nx * hit.depth;
ny += hit.ny * hit.depth;
const vn = dx * hit.nx + dy * hit.ny;
if (vn < 0) {
dx -= 2 * vn * hit.nx;
dy -= 2 * vn * hit.ny;
}
points.push({ x: nx, y: ny });
px = nx;
py = ny;
bounces += 1;
travelled += step;
bounced = true;
break;
}
if (bounced) continue;
px = nx;
py = ny;
travelled += step;
points.push({ x: px, y: py });
}
return points;
}
