The pack
maths
function maths(): Pack<{ maths: MathsApi }>
The maths pack: puts mathsApi on game.maths. No columns, steps, hooks or options; requires nothing.
Helpers
geom
const geom: { distance: (ax: number, ay: number, bx: number, by: number) => number; angleTo: (ax: number, ay: number, bx: number, by: number) => number; pointInRect: (px: number, py: number, x: number, y: number, w: number, h: number) => boolean; ... 9 more ...; predictPath: (x: number, y: number, vx: number, vy: number, gx: nu... …
2D geometry tests over points, segments, circles and polygon rings (Vec2[]). Rect tests take top-left x, y.
signedAngle
const signedAngle = (from: number, to: number)=> …
The shortest signed turn from one angle to another.
Functions
bezier
function bezier(x1: number, y1: number, x2: number, y2: number): EaseFn
A cubic bezier easing from its two control points (CSS cubic-bezier(x1, y1, x2, y2)).
ease
function ease(e: EaseName | EaseFn | readonly [number, number, number, number] | undefined): EaseFn
Resolve an easing given by name, function or bezier tuple. Unknown names are a loud error.
Constants
approach
const approach = (v: number, target: number, rate: number, dt: number)=> …
Exponential approach — frame-rate independent smoothing: v + (target - v) * (1 - e^(-rate·dt)).
clamp
const clamp = (v: number, min: number, max: number)=> …
v limited to min..max.
EASE
const EASE: typeof BASE & { quad: EaseFn; cubic: EaseFn; quart: EaseFn; quint: EaseFn; sine: EaseFn; expo: EaseFn; circ: EaseFn; back: EaseFn; bounce: EaseFn; elastic: EaseFn }
The named easing curves: ten easeIn/Out/InOut* families, linear and swing, plus short aliases where the short name is the family's "natural" curve.
hashCode
const hashCode = (s: string)=> …
FNV-1a: a string to a uint32, for turning a level code into an rng seed.
lerp
const lerp = (a: number, b: number, t: number)=> …
Linear blend between two numbers; t is not clamped.
mapRange
const mapRange = (v: number, inMin: number, inMax: number, outMin: number, outMax: number, clampOut = false)=> …
Remap v from inMin..inMax onto outMin..outMax; clampOut (default false) limits the result to the output range. A zero-width input range is treated as width 1.
mathsApi
const mathsApi: MathsApi
The single MathsApi instance; maths() attaches this same object.
roundTo
const roundTo = (v: number, precision = 0)=> …
Round to precision decimal places (default 0).
smoothstep
const smoothstep = (a: number, b: number, t: number)=> …
Hermite smoothstep: 0 at t <= a, 1 at t >= b, an S-curve between.
TAU
const TAU: number
2π: one full turn in radians.
toDeg
const toDeg = (rad: number)=> …
toRad
const toRad = (deg: number)=> …
toRad: degrees → radians. toDeg: radians → degrees.
turnToward
const turnToward = (from: number, to: number, maxStep: number)=> …
Turn from at most maxStep radians along the shortest arc toward to, landing exactly on to when within reach.
vec
const vec: { add: (a: Vec2, b: Vec2) => Vec2; sub: (a: Vec2, b: Vec2) => Vec2; scale: (a: Vec2, s: number) => Vec2; dot: (a: Vec2, b: Vec2) => number; ... 13 more ...; reflect: (v: Vec2, n: Vec2) => Vec2; } …
2D vector operations over plain { x, y }. Every function returns a new object; none mutates. Angles in radians.
wrapAngle
const wrapAngle = (rad: number)=> …
Wrap an angle to (−π, π].
Types
EaseFn
type EaseFn = (t: number) => number
An easing curve: t in 0..1 in, 0..1 out. Back and Elastic overshoot on purpose.
EaseName
type EaseName = keyof typeof EASE
Every key of EASE: the 30 easeIn/Out/InOut* names, linear, swing, and the ten short family aliases.
MathsApi
interface MathsApi
What game.maths presents: every exported function of this module, by the same name.
ease: typeof ease— Resolve an easing from a name, function or bezier tuple; throws on an unknown name.EASE: typeof EASE— The table of named easing curves.bezier: typeof bezier— A cubic bezier easing from two control points.vec: typeof vec— 2D vector operations.geom: typeof geom— 2D geometry tests.TAU: number— 2π.toRad: typeof toRad— Degrees → radians.toDeg: typeof toDeg— Radians → degrees.wrapAngle: typeof wrapAngle— Wrap an angle to (−π, π].signedAngle: typeof signedAngle— The shortest signed turn between two angles.turnToward: typeof turnToward— Turn at mostmaxStepradians along the shortest arc.lerp: typeof lerp— Linear blend,tunclamped.clamp: typeof clamp— Limit to a range.mapRange: typeof mapRange— Remap one range onto another.roundTo: typeof roundTo— Round to decimal places.smoothstep: typeof smoothstep— Hermite smoothstep.approach: typeof approach— Frame-rate-independent exponential approach.hashCode: typeof hashCode— FNV-1a string hash, for rng seeds.
Vec2
interface Vec2
A plain 2D vector. Every vec function takes and returns one of these and never mutates its inputs.
x: number— Horizontal component, in whatever unit the caller uses (pixels for positions).y: number— Vertical component; y grows downward in a 2D world.


