raea/math

Numbers through time: interpolation, easing, springs, curves and the vector types Bevy uses.

View as Markdown

interpolate maps a frame onto a range of values, with an easing and with clamping at either end. spring gives a physically damped value for a frame, and measureSpring says how long one takes to settle. Easing holds the standard curves; curveFn turns a declared curve into a function. The quaternion and affine helpers match Bevy's math types.

TS
import { Easing, interpolate, spring } from "raea/math";

const x = interpolate(30, [0, 45], [-200, 0], { easing: Easing.out(Easing.cubic), extrapolateRight: "clamp" });
const scale = spring({ frame: 30, fps: 30, config: { damping: 12 } });
TS
import { Affine2, curve, curveFn, Easing } from "raea/math"

raea/math exports 7 functions, 3 values, 7 types. Bevy's counterpart: bevy_math, bevy_curve. Source: sdk/raea/src/math.ts.

Functions

curveFn

A curve name as a function (the engine's easing::Easing::parse, the same names).

TS
function curveFn(name: string, fps?: number): (t: number) => number

hash01

0..1 from integers, the same on every run

TS
function hash01(...xs: number[]): number

interpolate

x mapped from the input range onto the output range, piece by piece: input rises, and the two have the same length. options.easing shapes each piece; extrapolateLeft and extrapolateRight say what happens outside the range (extend, the default, carries on; clamp holds the end value; identity returns x; wrap repeats).

TS
function interpolate(x: number, input: readonly number[], output: readonly number[], options?: InterpolateOptions): number

measureSpring

How many frames a spring with these settings takes to settle (before any stretching).

TS
function measureSpring(opts: { fps: number; config?: SpringConfig; durationRestThreshold?: number; }): number

quatAxis

The quaternion [x, y, z, w] turning angle radians about axis, as Bevy's Quat::from_axis_angle.

TS
function quatAxis(axis: Vec3, angle: number): Quat

quatMul

The product a × b of two quaternions [x, y, z, w], b applied first, as Bevy's Quat * Quat.

TS
function quatMul(a: Quat, b: Quat): Quat

spring

A damped spring's value at frame: from from (0) to to (1), started delay frames late, by its config (mass 1, damping 10, stiffness 100). It overshoots and settles unless overshootClamping is set; durationInFrames stretches it to end at that frame. measureSpring says how long one takes to settle.

TS
function spring(opts: SpringOptions): number

Values

Affine2

glam's Affine2 as its parts: scaled, then turned angle radians, then moved by translation (from_scale_angle_translation). glam's Affine2 constructors, by their Rust names in camelCase.

Used in: 2d/mesh2d_repeated_texture.

curve

Curve helpers: curve.bezier(0.05, 0.7, 0.1, 1), curve.spring({ damping: 15, stiffness: 120 }).

Easing

The easing curves, each a function of progress 0..1: linear, quad, cubic, poly(n), sin, circle, exp, back, elastic, bounce and bezier(x1, y1, x2, y2), eased in as they are; Easing.out(f) and Easing.inOut(f) turn one round. Pass one as interpolate's easing.

Types

Curve

TS
type Curve = CurveName | (string & {})

CurveName

Curve names. Any of these, or bezier(x1,y1,x2,y2), steps(n), spring(mass,damping,stiffness), reverse(<curve>).

TS
type CurveName = | "linear" | "ease" | `${"in" | "out" | "in-out"}-${"quad" | "cubic" | "quart" | "quint" | "sine" | "expo" | "circ" | "back" | "elastic" | "bounce"}`

EasingFn

TS
type EasingFn = (p: number) => number

ExtrapolateMode

TS
type ExtrapolateMode = "extend" | "clamp" | "identity" | "wrap"

InterpolateOptions

FieldTypeWhat it is
easingEasingFn | EasingFn[]the curve between keyframes: one for all, or one a segment
extrapolateLeftExtrapolateModebelow the first keyframe: extend the line (default), clamp to it, identity (the input itself) or wrap
extrapolateRightExtrapolateModepast the last keyframe: extend (default), clamp, identity or wrap

SpringConfig

FieldTypeWhat it is
massnumberhow heavy the spring's weight is (1): heavier is slower and swings further
dampingnumberhow fast the swing dies (10): lower bounces more
stiffnessnumberhow hard the spring pulls (100): stiffer is quicker
overshootClampingbooleanstop at to the first time it gets there, with no overshoot (false)

SpringOptions

FieldTypeWhat it is
frame (required)numberthe frame to sample, usually useCurrentFrame()
fps (required)numberframes a second, from useVideoConfig()
fromnumberthe value it starts at (0)
tonumberthe value it settles at (1)
configSpringConfigthe spring: mass, damping, stiffness, overshootClamping
durationInFramesnumberstretch or squeeze the spring to settle in this many frames
durationRestThresholdnumberhow close to to, as a share of the travel, counts as settled when timing it (0.005)
delaynumberframes before it starts (0)
reversebooleanplay it backwards: from to back to from