Mathf
Scalar math helpers — the numbers-only functions you reach for inside a frame loop. Mathf is a
plain object of functions and constants; nothing to construct. Vector, quaternion, and matrix math
live on Vec2 / Vec3, Quat, and Mat4, not here.
At a glance
const camX = Mathf.smoothDamp(0, 0.15) // stateful follower — create ONCE, outside the loop
setLoop(dt => {
cam.x = camX(player.x, dt) // critically-damped camera follow
zoom = Mathf.damp(zoom, targetZoom, 10, dt) // stateless exponential ease
health = Mathf.clamp(health, 0, 100)
bar.width = Mathf.lerp(0, 200, Mathf.inverseLerp(0, maxCharge, charge))
})Constants & angle conversion
Mathf.PI // Math.PI
Mathf.TAU // 2π — a full turn in radians
Mathf.EPSILON // 1e-6
Mathf.DEG2RAD // π / 180
Mathf.RAD2DEG // 180 / π
Mathf.deg2rad(deg): number
Mathf.rad2deg(rad): numberDEG2RAD / RAD2DEG also exist as bare compile-time globals (no Mathf. prefix) —
see conventions for which APIs take degrees vs radians.
Ranges & interpolation
Mathf.clamp(v, min, max): number
Mathf.clamp01(v): number // clamp into [0, 1]
Mathf.lerp(a, b, t): number // a + (b − a)·t
Mathf.inverseLerp(a, b, v): number // where v sits in [a, b] as 0..1 (0 if a === b)
Mathf.remap(v, inMin, inMax, outMin, outMax): number // map v from one range onto another
Mathf.sign(x): number // -1, 0, or 1 (Math.sign)
Mathf.repeat(t, length): number // loop t into [0, length)
Mathf.pingPong(t, length): number // bounce t back and forth in [0, length]Easing toward a value
Mathf.moveTowards(a, b, maxDelta): number // step linearly, never overshoots
Mathf.damp(a, b, lambda, dt): number // exponential approach, frame-rate independentdamp is the frame-rate-safe replacement for a = lerp(a, b, 0.1) in a loop: lambda is the
rate (higher = snappier; good defaults 5..15), dt is the loop's seconds-per-frame. It is
stateless — pass last frame's value back in.
smoothDamp — critically-damped follow (Unity SmoothDamp)
Mathf.smoothDamp(initial: number, smoothTime, maxSpeed?): Damper<number>
Mathf.smoothDamp(initial: number[], smoothTime, maxSpeed?): Damper<number[]> // vec2/vec3 arrays
const damper = Mathf.smoothDamp([0, 0], 0.15)
damper(target, dt) // call every frame → the new eased value
damper.setValue(v) // snap internal state (teleport) — kills the rubber-bandReturns a stateful damper that tracks its own velocity, so motion eases in and out without
overshoot. smoothTime is roughly the settle time in seconds; maxSpeed (default Infinity)
caps the speed — for arrays the cap applies to the vector magnitude, not per component.
const follow = Mathf.smoothDamp([0, 0], 0.15)
setLoop(dt => { scene.camera.position = follow([player.x, player.y], dt) })create the damper once, outside the loop. A fresh damper every frame resets its
velocity and defeats the smoothing. On teleports call setValue so the follower doesn't chase
from the old spot.
Random
Mathf.random(min?, max?): number // float in [min, max); default 0..1
Mathf.randomInt(min, max): number // integer, INCLUSIVE of both endsSee also
- Vec2 & Vec3 — vector lerp / distance / normalize live on the vector types
- Conventions —
dtis seconds; degrees-vs-radians rules