Skip to content

Math

Math primitives for 3D vector and quaternion operations. Accessed via snaptrude.core.math.

Vectors and quaternions are opaque value handles (Vec3Handle, QuatHandle) held by the host — every math operation is an async host call that takes handles in and returns a new handle out (values are immutable; no operation ever mutates its inputs). Always await each call, and read primitive components back with each module's components method.

Modules

Vec3

3D vector operations for positions, directions, and displacements — creation and arithmetic (add, subtract, scale, dot, cross, normalize, lerp, …).

ts
const { vec3 } = snaptrude.core.math;
const a = await vec3.new(1, 2, 3);
const b = await vec3.new(4, 5, 6);
const sum = await vec3.add(a, b);
const { x, y, z } = await vec3.components(sum);
// x === 5, y === 7, z === 9

Quat

Quaternion creation and rotation operations. Quaternions avoid gimbal lock and provide smooth interpolation compared to Euler angles.

ts
const { vec3, quat } = snaptrude.core.math;
// 90° rotation around the Y axis
const up = await vec3.new(0, 1, 0);
const rot = await quat.fromAxisAngle(up, Math.PI / 2);
const { x, y, z, w } = await quat.components(rot);