Extended Math API

This API provides vectors, quaternions, matrices, math constants, and helper functions found in modern game engines.


📘 Contents

  • Math Constants

  • Global Math Helpers

  • vector2

  • vector3

  • quaternion

  • matrix4x4

  • mat4 Namespace


🔢 Math Constants

These are available globally:

Name
Value

M_PI

3.141592653589793

M_TAU

6.283185307179586 (2π)

M_PI_2

π/2

M_PI_4

π/4

DEG2RAD

π/180

RAD2DEG

180/π

PI_OVER_180

π/180

INV_PI_OVER_180

180/π

M_ZERO

0

M_ONE

1

M_EPSILON

0.000001

Usage:


🛠 Global Math Helpers

✔ clamp(x, min, max)

Clamps a value between two bounds.

✔ saturate(x)

Clamps value to [0,1].

✔ sign(x)

Returns -1, 0, or 1.

✔ round(x), round_up(x), round_down(x)

Standard rounding helpers.

✔ fract(x)

Fractional part (always 0–1, even for negatives).

✔ wrap(x, min, max)

Wraps a number into a cyclic range.

✔ lerp(a, b, t)

Linear interpolation.

✔ inverse_lerp(a, b, v)

Inverse of lerp.

✔ remap(a1, b1, a2, b2, v)

Maps a value from one range to another.

✔ smoothstep(edge0, edge1, x)

Smooth Hermite interpolation.

✔ step(edge, x)

Returns 0 if x < edge, else 1.

✔ is_nan(x), is_inf(x)

Checks numeric state.


🟦 vector2

A 2D double-precision vector.

✔ Create

✔ Fields

  • v.x

  • v.y

✔ Operators

Operation
Example

add

v1 + v2

subtract

v1 - v2

multiply (scalar)

v * 2

divide (scalar)

v / 2

unary minus

-v

equality

v1 == v2

✔ Methods

✔ Memory I/O


🟩 vector3

A 3D double-precision vector.

✔ Create

✔ Fields

v.x, v.y, v.z

✔ Operators

Same as vector2, plus:

  • v:dot(other)

  • v:cross_product(other)

✔ Methods

✔ Memory I/O

Same pattern as vector2.


🟪 quaternion

A 4D double-precision quaternion (x, y, z, w).

✔ Create

✔ Fields

q.x, q.y, q.z, q.w

✔ Operators

Operation
Example

multiply quaternion

q1 * q2

multiply scalar

q * 2

divide scalar

q / 2

add

q1 + q2

subtract

q1 - q2

negate

-q

equality

q1 == q2

✔ Methods

✔ Memory I/O


🟥 matrix4x4

A 4×4 float matrix (row-major), used for transforms and 3D math.

✔ Create

✔ Indexing

✔ Operators

✔ Methods (instance)

✔ tostring()

Pretty-prints matrix for debugging.


🟧 mat4 Namespace

Convenient static constructors:


📘 Examples

TRS (Translate → Rotate → Scale)

Quaternion Rotation

Remapping Values

Full API Test

Last updated