Math & Vectors

Source: resources/modules/xs/math.wren


Class Math ↗

Mathematical utility functions for 2D game development

static pi -> Num ↗

The mathematical constant pi

static lerp(a: Num, b: Num, t: Num) -> Num ↗

Linear interpolation between two values

Returns a value between a and b based on parameter t (0.0 to 1.0)

t=0 returns a, t=1 returns b, t=0.5 returns midpoint

static damp(a: Num, b: Num, lambda: Num, dt: Num) -> Num ↗

Smooth damping interpolation using exponential decay

Useful for camera following and smooth movement - gives frame-rate independent smoothing

Higher lambda values = faster convergence (try values like 5.0 to 20.0)

static min(l: Num, r: Num) -> Num ↗

Returns the minimum of two values

static max(l: Num, r: Num) -> Num ↗

Returns the maximum of two values

static atan2(y: Num, x: Num) -> Num ↗

Returns the arctangent of y/x in radians

static invLerp(a: Num, b: Num, v: Num) -> Num ↗

Inverse linear interpolation - returns t such that lerp(a, b, t) = v

static remap(iF: Num, iT: Num, oF: Num, oT: Num, v: Num) -> Num ↗

Remaps a value from one range to another

static radians(deg: Num) -> Num ↗

Converts degrees to radians

static degrees(rad: Num) -> Num ↗

Converts radians to degrees

static mod(x: Num, m: Num) -> Num ↗

Modulo operation that always returns positive results

static fmod(x: Num, m: Num) -> Num ↗

Floating-point modulo operation

static clamp(a: Num, f: Num, t: Num) -> Num ↗

Clamps a value between min (f) and max (t)

static slerp(a: Num, b: Num, t: Num) -> Num ↗

Spherical linear interpolation for angles

static vslerp(a: Vec2, b: Vec2, t: Num) -> Vec2 ↗

Vector spherical linear interpolation

static arc(a: Num, b: Num) -> Num ↗

Returns the shortest angular distance between two angles

static sdamp(a: Num, b: Num, lambda: Num, dt: Num) -> Num ↗

Smooth damping for angles (spherical)

static quadraticBezier(a: Num, b: Num, c: Num, t: Num) -> Num ↗

Quadratic Bezier curve interpolation

static cubicBezier(a: Num, b: Num, c: Num, d: Num, t: Num) -> Num ↗

Cubic Bezier curve interpolation

static catmullRom(a: Num, b: Num, c: Num, d: Num, t: Num) -> Num ↗

Catmull-Rom spline interpolation


Class Bits ↗

Utility functions for bitwise operations and bit flags

static switchOnBitFlag(flags: Num, bit: Num) -> Num ↗

Turns on a specific bit flag

static switchOffBitFlag(flags: Num, bit: Num) -> Num ↗

Turns off a specific bit flag

static checkBitFlag(flags: Num, bit: Num) -> Bool ↗

Checks if all bits in 'bit' are set in 'flags'

static checkBitFlagOverlap(flag0: Num, flag1: Num) -> Bool ↗

Checks if any bits overlap between two flags


Class Vec2 ↗

2D vector class for position, velocity, and direction calculations

Supports standard vector operations: addition, subtraction, scaling, dot product, cross product, rotation

construct new() ↗

Creates a zero vector (0, 0)

construct new(x: Num, y: Num) ↗

Creates a vector with the given x and y components

construct new(other: Vec2) ↗

Creates a vector with the same components as another vector

x -> Num ↗

Gets the x component

y -> Num ↗

Gets the y component

x=(v: Num) ↗

Sets the x component

y=(v: Num) ↗

Sets the y component

+(other: Vec2) -> Vec2 ↗

Adds two vectors

- -> Vec2 ↗

Negates the vector

-(other: Vec2) -> Vec2 ↗

Subtracts two vectors

*(v: Num) -> Vec2 ↗

Multiplies vector by a scalar

/(v: Num) -> Vec2 ↗

Divides vector by a scalar

==(other) -> Bool ↗

Checks if two vectors are equal

!=(other) -> Bool ↗

Checks if two vectors are not equal

magnitude -> Num ↗

Gets the length of the vector

magnitudeSq -> Num ↗

Gets the squared length of the vector (faster than magnitude)

normal -> Vec2 ↗

Returns a normalized copy of the vector (length = 1)

normalized -> Vec2 ↗

Returns a normalized copy of the vector (length = 1)

normalize() ↗

Normalizes this vector in place

dot(other: Vec2) -> Num ↗

Computes the dot product with another vector

Returns a scalar: positive if vectors point same direction, negative if opposite, 0 if perpendicular

cross(other: Vec2) -> Num ↗

Computes the 2D cross product (returns scalar)

Returns the z-component of the 3D cross product - useful for determining rotation direction

rotate(a: Num) ↗

Rotates this vector by angle a (in radians) in place

rotated(a: Num) -> Vec2 ↗

Returns a rotated copy of this vector

perp -> Vec2 ↗

Returns a perpendicular vector rotated 90 degrees counter-clockwise

Useful for calculating normals and perpendicular directions

clear() ↗

Sets the vector to (0, 0)

atan2 -> Num ↗

Returns the angle of this vector in radians

clamp(min: Vec2, max: Vec2) ↗

Clamps this vector's components between min and max

toString -> String ↗

Returns a string representation of this vector

serialize -> List ↗

Serializes the vector to a list

construct deserialize(data: List) ↗

Creates a vector from serialized data

static distance(a: Vec2, b: Vec2) -> Num ↗

Computes the distance between two vectors

static distanceSq(a: Vec2, b: Vec2) -> Num ↗

Computes the squared distance between two vectors (faster than distance)

static randomDirection() -> Vec2 ↗

Returns a random unit direction vector

static reflect(incident: Vec2, normal: Vec2) -> Vec2 ↗

Reflects an incident vector off a surface with the given normal vector

The normal should be normalized (unit length) for correct results

static project(a: Vec2, b: Vec2) -> Vec2 ↗

Projects vector a onto vector b

static fromAngle(angle: Num) -> Vec2 ↗

Creates a unit vector from an angle in radians

static lerp(a: Vec2, b: Vec2, t: Num) -> Vec2 ↗

Linear interpolation between two vectors


Class Geom ↗

Geometric utility functions

static distanceSegmentToPoint(a: Vec2, b: Vec2, c: Vec2) -> Num ↗

Computes the distance from a line segment (a to b) to a point c

Based on https://stackoverflow.com/questions/1073336/circle-line-segment-collision-detection-algorithm


Class Color ↗

RGBA color class with utility functions

Color components are integers from 0-255

Supports arithmetic operations and conversion to/from 32-bit integers

construct new(r: Num, g: Num, b: Num, a: Num) ↗

Creates a color with RGBA components (0-255)

construct new(r: Num, g: Num, b: Num) ↗

Creates a color with RGB components (alpha defaults to 255)

a -> Num ↗

Gets the alpha component

r -> Num ↗

Gets the red component

g -> Num ↗

Gets the green component

b -> Num ↗

Gets the blue component

a=(v: Num) ↗

Sets the alpha component

r=(v: Num) ↗

Sets the red component

g=(v: Num) ↗

Sets the green component

b=(v: Num) ↗

Sets the blue component

+(other: Color) -> Color ↗

Adds two colors component-wise

-(other: Color) -> Color ↗

Subtracts two colors component-wise

*(other) ↗

Multiplies color by another color or scalar

toNum -> Num ↗

Converts the color to a 32-bit integer (0xRRGGBBAA format)

static fromNum(v: Num) -> Color ↗

Creates a color from a 32-bit integer

static add(x: Num, y: Num) -> Num ↗

Adds two colors represented as 32-bit integers (including alpha channel)

static mul(x: Num, y: Num) -> Num ↗

Multiplies two colors represented as 32-bit integers (including alpha channel)

toString -> String ↗

Returns a string representation of this color