Skip to content

Variable: MathUtils ​

ts
const MathUtils: object;

Type Declaration ​

NameTypeDescription
DEG_TO_RADnumberScalar 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_PInumber-
RAD_TO_DEGnumber-
TWO_PInumber-
Approximately()( a, b, epsilon? ) => booleanApproximately-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 ) => numberConstrain v to [min, max]. Branchless via Math.min/max — fastest in practice.
Clamp01()(v) => number-
DegToRad()(deg) => numberConvenience converters — pair with TweenManager or any setter that takes radians/degrees.
DeltaAngle()(a, b) => numberShortest signed angular distance from a to b in radians, in [-PI, PI].
InverseLerp()( a, b, v ) => numberInverse 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) => booleanPower of 2 check / next power of 2. Often needed when allocating texture-backed buffers.
Lerp()( a, b, t ) => numberLinear interpolation. t is NOT clamped — pass < 0 / > 1 for extrapolation.
Mod()(a, n) => numberMathematical 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 ) => numberMove 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 ) => numberSame 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 ) => numberRemap 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? ) => numberCritically-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 ) => numberTighter-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 ) => numberGLSL smoothstep — Hermite interpolation between edge0 and edge1. t clamps inside.
Step()(edge, v) => numberGLSL step — hard cutoff at edge. Returns 0 below, 1 at/above.
WrapAngle()(radians) => numberNormalize 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).

© 2026 PlayAdGentic