Skip to content

Math

Compiles

The $math natives cover what the operators don’t. Positions and directions are vec3, and z is up.

let a: vec3 = vec3(0, 0, 0)
let b: vec3 = vec3(300, 400, 0)
let far: float = $math.distance(a, b)
let close_enough: bool = far < 500.0
let from: vec3 = vec3(0, 0, 0)
let to: vec3 = vec3(10, 0, 0)
let direction: vec3 = $math.normalize(to - from)

normalize makes a vector length 1, so it’s pure direction.

$math.lerp(a, b, t) gives a when t is 0, b when t is 1, and in between for anything between:

let halfway: float = $math.lerp(0.0, 100.0, 0.5)
let spot: vec3 = $math.lerp(vec3(0, 0, 0), vec3(0, 0, 200), 0.25)
let requested: float = 140.0
let volume: float = $math.clamp(requested, 0.0, 100.0)
let fraction: float = $math.saturate(1.4) // clamp to 0..1

Turn a distance of 0–1000 into a strength of 1–0:

let distance: float = 250.0
let strength: float = $math.map_range(0.0, 1000.0, 1.0, 0.0, distance)
let roll: int = $math.randi(1, 6) // 1 to 6, both included
let jitter: float = $math.randf(-5.0, 5.0)

The engine works in radians. Convert, and build or read rotations:

let radians: float = $math.deg2rad(90.0)
let turned: vec4 = $math.rotation_from_axis_angle(vec3(0, 0, 1), radians)
let angles: vec3 = $math.eulers_from_rotation(turned)
let yaw: float = angles.z

eulers_from_rotation gives roll, pitch and yaw as x, y and z.

Waves (sin, cos), rounding (floori, ceili, roundi), min, max, abs, dot, cross, slerp for rotations: see the math reference.