Scalar math helpers used everywhere in game code. Free functions instead of methods so they're cheap to import individually and tree-shake when unused. Naming matches Unity / GLSL conventions to minimize friction for anyone bringing those mental models in. Angle conversion. Use these instead of inlining * (Math.PI / 180) — the constant is folded by the minifier and the named call documents intent.
HALF_PI
number
-
RAD_TO_DEG
number
-
TWO_PI
number
-
Approximately()
( a, b, epsilon? ) => boolean
Approximately-equal — defaults pick a tolerance comparable to single-precision float error scaled to the values involved (Number.EPSILON is too tight for any non-trivial arithmetic). Override epsilon for explicit tolerances.
Clamp()
( v, min, max ) => number
Constrain v to [min, max]. Branchless via Math.min/max — fastest in practice.
Clamp01()
(v) => number
-
DegToRad()
(deg) => number
Convenience converters — pair with TweenManager or any setter that takes radians/degrees.
DeltaAngle()
(a, b) => number
Shortest signed angular distance from a to b in radians, in [-PI, PI].
InverseLerp()
( a, b, v ) => number
Inverse of Lerp — returns the t such that Lerp(a, b, t) === v. Result is unclamped. Returns 0 when a === b (avoid divide-by-zero).
IsPowerOf2()
(v) => boolean
Power of 2 check / next power of 2. Often needed when allocating texture-backed buffers.
Lerp()
( a, b, t ) => number
Linear interpolation. t is NOT clamped — pass < 0 / > 1 for extrapolation.
Mod()
(a, n) => number
Mathematical modulo — result has the same sign as n (always non-negative for positive n). JS % operator gives the sign of a, which is wrong for negative dividends ((-1) % 4 === -1).
MoveTowards()
( current, target, maxDelta ) => number
Move current toward target by at most maxDelta. Snaps to target once within range. Frame-rate-aware version of current += sign(target-current) * speed * dt.
MoveTowardsAngle()
( current, target, maxDelta ) => number
Same idea as MoveTowards but angular — clockwise vs counter-clockwise is chosen by the shortest path. All angles in radians.
NextPowerOf2()
(v) => number
-
RadToDeg()
(rad) => number
-
Remap()
( v, fromMin, fromMax, toMin, toMax ) => number
Remap v from one range to another. Equivalent to Lerp(toMin, toMax, InverseLerp(fromMin, fromMax, v)).
Sign()
(v) => number
+1 / -1 / 0. JS Math.sign returns NaN for NaN; this mirrors that.
SmoothDamp()
( current, target, velocityRef, smoothTime, dt, maxSpeed? ) => number
Critically-damped follow — current chases target over a half-life-ish smoothTime. Unity-port. Stores carry between frames in velocityRef (single-element array, since JS lacks ref params). Useful for camera follow and any "soft pursuit" feel. const velRef : [number] = [0]; x = MathUtils.SmoothDamp(x, target, velRef, 0.15, dt); Pass maxSpeed to cap absolute velocity; leave undefined for uncapped.
Smootherstep()
( edge0, edge1, v ) => number
Tighter-derivative variant — zero first AND second derivative at the endpoints. Useful for camera follow and physics-feeling motion where smoothstep still has a visible kink.
Smoothstep()
( edge0, edge1, v ) => number
GLSL smoothstep — Hermite interpolation between edge0 and edge1. t clamps inside.
Step()
(edge, v) => number
GLSL step — hard cutoff at edge. Returns 0 below, 1 at/above.
WrapAngle()
(radians) => number
Normalize a radian angle to (-PI, PI]. Useful when subtracting two angles and you want the shortest signed difference (e.g. for camera pan, character facing).