Math & Vectors

Source: assets/modules/xs_math.wren


Class Math

Mathematical utility functions for 2D game development

static pi

The mathematical constant pi

static lerp(a, b, t)

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, b, lambda, dt)

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, r)

Returns the minimum of two values

static max(l, r)

Returns the maximum of two values

static atan2(y, x)

Returns the arctangent of y/x in radians

static invLerp(a, b, v)

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

static remap(iF, iT, oF, oT, v)

Remaps a value from one range to another

static radians(deg)

Converts degrees to radians

static degrees(rad)

Converts radians to degrees

static mod(x, m)

Modulo operation that always returns positive results

static fmod(x, m)

Floating-point modulo operation

static clamp(a, f, t)

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

static slerp(a, b, t)

Spherical linear interpolation for angles

static vslerp(a, b, t)

Vector spherical linear interpolation

static arc(a, b)

Returns the shortest angular distance between two angles

static sdamp(a, b, lambda, dt)

Smooth damping for angles (spherical)

static quadraticBezier(a, b, c, t)

Quadratic Bezier curve interpolation

static cubicBezier(a, b, c, d, t)

Cubic Bezier curve interpolation

static catmullRom(a, b, c, d, t)

Catmull-Rom spline interpolation


Class Bits

Utility functions for bitwise operations and bit flags

static switchOnBitFlag(flags, bit)

Turns on a specific bit flag

static switchOffBitFlag(flags, bit)

Turns off a specific bit flag

static checkBitFlag(flags, bit)

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

static checkBitFlagOverlap(flag0, flag1)

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, y)

Creates a vector with the given x and y components

x

Gets the x component

y

Gets the y component

x=(v)

Sets the x component

y=(v)

Sets the y component

+(other)

Adds two vectors

-

Negates the vector

-(other)

Subtracts two vectors

*(v)

Multiplies vector by a scalar

/(v)

Divides vector by a scalar

==(other)

Checks if two vectors are equal

!=(other)

Checks if two vectors are not equal

magnitude

Gets the length of the vector

magnitudeSq

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

normal

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

normalize()

Normalizes this vector in place

dot(other)

Computes the dot product with another vector

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

cross(other)

Computes the 2D cross product (returns scalar)

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

rotate(a)

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

rotated(a)

Returns a rotated copy of this vector

perp

Returns a perpendicular vector rotated 90 degrees counter-clockwise

Useful for calculating normals and perpendicular directions

clear()

Sets the vector to (0, 0)

atan2

Returns the angle of this vector in radians

clamp(min, max)

Clamps this vector's components between min and max

toString

Returns a string representation of this vector

serialize

Serializes the vector to a list

construct deserialize(data)

Creates a vector from serialized data

static distance(a, b)

Computes the distance between two vectors

static distanceSq(a, b)

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

static randomDirection()

Returns a random unit direction vector

static reflect(incident, normal)

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, b)

Projects vector a onto vector b


Class Geom

Geometric utility functions

static distanceSegmentToPoint(a, b, c)

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, g, b, a)

Creates a color with RGBA components (0-255)

construct new(r, g, b)

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

a

Gets the alpha component

r

Gets the red component

g

Gets the green component

b

Gets the blue component

a=(v)

Sets the alpha component

r=(v)

Sets the red component

g=(v)

Sets the green component

b=(v)

Sets the blue component

+(other)

Adds two colors component-wise

-(other)

Subtracts two colors component-wise

*(other)

Multiplies color by another color or scalar

toNum

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

static fromNum(v)

Creates a color from a 32-bit integer

static add(x, y)

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

static mul(x, y)

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

toString

Returns a string representation of this color