gmath
This package contains all of the most important math functions used for game development purposes. It provides a comprehensive library of mathematical primitives and utilities. Contains linear algebra, geometry and random number generation.
Features:
- Linear algebra: Robust support for vectors (
Vector2/Int->Vector4/Int) and matrices (Matrix4) including the most important operations likedot,normalize,direction. - Geometry: Contains tools for manipulating shapes, including
RectangleandCircle. Allows to position, scale, pivot, and check collision between shapes. - Randomness: Utilities for generating random numbers (
randomRange), normalized floats (randomFloatNormalized) and picking random array elements viarandomElement. - Math utilities: Essential game math functions including
lerp,ease,remapandclamp. - Color handling: Helpers to convert hex strings or values into
Colorstructs (hexToColor). Contains generic color constants inbonsai:core/gmath/colors.
Circle
Section titled “Circle”Circle :: struct { position: Vector2, radius: f32,}A Circle defined by a center position and a radius.
Color :: Vector4Color is an alias for a standard 4D Float Vector.
E :: math.EConstant e value. Approximately 2.718281828.
Easing
Section titled “Easing”Easing :: enum { Linear, InSine, OutSine, InOutSine, InQuad, OutQuad, InOutQuad, InCubic, OutCubic, InOutCubic, InQuart, OutQuart, InOutQuart, InQuint, OutQuint, InOutQuint, InExpo, OutExpo, InOutExpo, InCirc, OutCirc, InOutCirc, InBack, OutBack, InOutBack, InElastic, OutElastic, InOutElastic, InBounce, OutBounce, InOutBounce,}Standard easing functions matching https://easings.net
Matrix4
Section titled “Matrix4”Matrix4 :: linalg.Matrix4f324x4 Float Matrix (Column-Major).
PI :: math.PIConstant π value.
Pivot :: enum { bottomLeft, bottomCenter, bottomRight, centerLeft, centerCenter, centerRight, topLeft, topCenter, topRight,}Enum representing the 9 cardinal points of a rectangle.
Rectangle
Section titled “Rectangle”Rectangle :: Vector4An Axis-Aligned Bounding Box (AABB) stored as a Vector4.
Format: { minX, minY, maxX, maxY }
This corresponds to (Left, Bottom, Right, Top).
TAU :: math.TAUConstant τ value. Equal to 2π.
Vector2
Section titled “Vector2”Vector2 :: linalg.Vector2f32Standard 2D Float Vector (compatible with Odin’s core:math/linalg package)
Vector2Int
Section titled “Vector2Int”Vector2Int :: [2]i322D Integer Vector (32-bit).
Vector3
Section titled “Vector3”Vector3 :: linalg.Vector3f32Standard 3D Float Vector.
Vector3Int
Section titled “Vector3Int”Vector3Int :: [3]i323D Integer Vector (32-bit).
Vector4
Section titled “Vector4”Vector4 :: linalg.Vector4f32Standard 4D Float Vector.
Vector4Int
Section titled “Vector4Int”Vector4Int :: [4]i324D Integer Vector (32-bit).
abs :: proc ( input: $T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the absolute value of input.
- For scalars: Returns the non-negative value.
- For vectors: Returns a new vector where every component is positive.
almostEquals
Section titled “almostEquals”almostEquals :: proc ( a, b: $T, epsilon: $E,) -> bool where (intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)) && intrinsics.type_is_float(E)Checks if two floats are equal within a small margin of error (epsilon).
angleToVector
Section titled “angleToVector”angleToVector :: proc ( radians: $T,) -> Vector2 where intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns a normalized direction vector from an angle (in radians).
atan2 :: proc (y, x: $T) -> T where intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the angle in radians between the x-axis and the ray from {0,0} to {y,x}.
ceil :: proc ( input: $T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the smallest whole number greater than or equal to the input.
Accepts scalar and vector values as an argument.
clamp :: proc ( input: $T, minimum, maximum: $E,) -> T where (intrinsics.type_is_array(T) && (intrinsics.type_is_array(E) || intrinsics.type_is_float(E) || intrinsics.type_is_integer(E))) || ((intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)) && (intrinsics.type_is_float(E) || intrinsics.type_is_integer(E)))Clamps input between minimum and maximum.
cos :: proc ( angle: $T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the cosine of angle (in radians).
- For scalars: Standard trigonometric cos function.
- For vectors: Component-wise cosine.
direction
Section titled “direction”direction :: proc (start, end: $T) -> T where intrinsics.type_is_array(T)Returns a normalized direction vector pointing from start to end.
distance
Section titled “distance”distance :: proc ( a, b: [$N]$T,) -> T where intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the distance between points a and b.
dot :: proc (a, b: [$N]$T) -> T where intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the dot product between two vectors.
ease :: proc (name: Easing, t: f32) -> f32Applies the specified easing curve to a normalized value (0.0 to 1.0).
exp :: proc ( input: $T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns e raised to the power of input.
floor :: proc ( input: $T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the largest whole number less than or equal to the input.
Accepts scalar and vector values as an argument.
getRandomSeed
Section titled “getRandomSeed”getRandomSeed :: proc () -> u64Returns the current seed used by the random number generator.
getRectangleCenter
Section titled “getRectangleCenter”getRectangleCenter :: proc (rectangle: Rectangle) -> Vector2Returns the center point of the Rectangle.
getRectanglePivot
Section titled “getRectanglePivot”getRectanglePivot :: proc (rectangle: Rectangle, pivot: Pivot) -> Vector2Helper to get a position within a Rectangle
using a Pivot enum.
getRectangleSize
Section titled “getRectangleSize”getRectangleSize :: proc (rectangle: Rectangle) -> Vector2Returns the width and height of the Rectangle as a Vector2.
hexToColor
Section titled “hexToColor”hexToColor :: proc (value: u32) -> ColorConverts a packed 32-bit integer (0xRRGGBBAA) into a normalized Color.
Extracts bytes and divides by 255.0 to map to the 0.0-1.0 range.
length
Section titled “length”length :: proc ( input: [$N]$T,) -> T where intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the length (magnitude) of the vector input.
lengthSquared
Section titled “lengthSquared”lengthSquared :: proc ( input: [$N]$T,) -> T where intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the squared length of the vector input.
Faster than length because it doesn’t use the square root operation.
lerp :: proc (a, b: $T, t: $E) -> TLinearly interpolates between a and b by the fraction t.
Useful for smooth transitions, animations, or mixing colors.
Accepts scalar arguments, as well as Vector2, Vector3, Vector4 and Color.
ln :: proc ( input: $T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the natural logarithm (base e) of input.
- For scalars: Standard natural logarithm.
- For vectors: Component-wise natural logarithm.
log :: proc ( base: $T, input: T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the logarithm of input in the specified base
log10 :: proc ( input: $T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the base-10 logarithm of input.
matrixInverse
Section titled “matrixInverse”matrixInverse :: proc (mat: Matrix4) -> Matrix4Returns the inverse of the 4x4 matrix. Useful for creating view matrices and converting world space to screen space.
matrixOrtho3d
Section titled “matrixOrtho3d”matrixOrtho3d :: proc ( left, right, bottom, top, near, far: $T,) -> Matrix4 where intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Creates an orthographic projection matrix. This defines the viewing volume as a rectangular box. Objects inside this box are visible, objects outside are clipped.
matrixRotate
Section titled “matrixRotate”matrixRotate :: proc (rotation: Vector3) -> Matrix4Creates a rotation matrix from a rotation Vector3.
matrixRotateX
Section titled “matrixRotateX”matrixRotateX :: proc (radians: f32) -> Matrix4Creates a rotation matrix (X-axis) from an angle in radians.
matrixRotateY
Section titled “matrixRotateY”matrixRotateY :: proc (radians: f32) -> Matrix4Creates a rotation matrix (Y-axis) from an angle in radians.
matrixRotateZ
Section titled “matrixRotateZ”matrixRotateZ :: proc (radians: f32) -> Matrix4Creates a rotation matrix (Z-axis) from an angle in radians.
matrixScale
Section titled “matrixScale”matrixScale :: proc (scale: Vector2) -> Matrix4Creates a scaling matrix. Z-scale is locked to 1.0.
matrixTranslate
Section titled “matrixTranslate”matrixTranslate :: proc (position: Vector2) -> Matrix4Creates a translation matrix from a 2D position (Z is assumed to be 0).
max :: proc ( a, b: $T, rest: ..T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the largest value among all arguments. If arguments are vectors, returns a component-wise maximum vector. Accepts any number of arguments (minimum 2).
min :: proc ( a, b: $T, rest: ..T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the smallest value among all arguments. If arguments are vectors, returns a component-wise minimum vector. Accepts any number of arguments (minimum 2).
normalize
Section titled “normalize”normalize :: proc (input: $T) -> T where intrinsics.type_is_array(T)Returns the normalized direction of the vector input (with length of 1).
Returns {0, 0} if the input vector is zero to prevent nil errors.
pow :: proc ( base: $B, power: $P,) -> B where (intrinsics.type_is_array(B) && (intrinsics.type_is_array(P) || intrinsics.type_is_float(P) || intrinsics.type_is_integer(P))) || ((intrinsics.type_is_float(B) || intrinsics.type_is_integer(B)) && intrinsics.type_is_float(P) || intrinsics.type_is_integer(P))Returns the value of base raised to power.
Accepts scalars, Vectors, and Color as an argument.
randomCircleNormalized
Section titled “randomCircleNormalized”randomCircleNormalized :: proc () -> Vector2Returns a random Vector2 point in a circle of radius 1.0.
randomElement
Section titled “randomElement”randomElement :: proc (list: []$T) -> (T, bool)Returns a random element from a slice.
Returns false if the slice is empty.
randomFloatNormalized
Section titled “randomFloatNormalized”randomFloatNormalized :: proc () -> f32Returns a random float between 0.0 and 1.0.
randomRange
Section titled “randomRange”randomRange :: proc (min: f32, max: f32) -> f32Returns a random float between min and max.
randomRangeInt
Section titled “randomRangeInt”randomRangeInt :: proc (min: int, max: int) -> intReturns a random integer between min (inclusive) and max (inclusive).
rectangleContains
Section titled “rectangleContains”rectangleContains :: proc (rectangle: Rectangle, point: Vector2) -> boolChecks if a point lies inside the Rectangle.
Returns true if it does.
rectangleExpand
Section titled “rectangleExpand”rectangleExpand :: proc { _rectangleExpandScalar, _rectangleExpandVector2,}Expands the Rectangle boundaries outwards by amount on all sides.
A negative amount shrinks the Rectangle.
Accepts a scalar or Vector2 as an amount by which the rectangle is expanded.
rectangleIntersects
Section titled “rectangleIntersects”rectangleIntersects :: proc (a: Rectangle, b: Rectangle) -> boolChecks if two Axis-Aligned Bounding Boxes (AABB) intersect.
Returns true if they overlap.
rectangleMake
Section titled “rectangleMake”rectangleMake :: proc { _rectangleFromPositionSize, _rectangleFromSize,}Overload group for creating a Rectangle.
Accepts arguments:
positionasVector2(optional, default:gmath.Vector2{0, 0}).sizeasVector2.pivotasPivot(optional, default:gmath.Pivot.bottomLeft).
rectangleScale
Section titled “rectangleScale”rectangleScale :: proc { _rectangleScaleScalar, _rectangleScaleVector2,}Scales a Rectangle around its own center point.
Accepts a scalar or Vector2 as a scale.
remap :: proc ( input, inMin, inMax, outMin, outMax: $T,) -> T where intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Remaps input from the [inMin, inMax] range to the [outMin, outMax] range.
rotatePoint
Section titled “rotatePoint”rotatePoint :: proc (point, pivot: Vector2, radians: f32) -> Vector2Rotates a point around a pivot point by radians.
round :: proc ( input: $T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Rounds the value to the nearest whole number. Accepts scalar and vector values as an argument.
roundToInt
Section titled “roundToInt”roundToInt :: proc (input: [$N]$T) -> [N]int where intrinsics.type_is_float(T)Rounds the float input vector to the nearest integer vector, changing its type.
scaleFromPivot
Section titled “scaleFromPivot”scaleFromPivot :: proc (pivot: Pivot) -> Vector2Returns a normalized Vector2 (0.0 -> +1.0) corresponding to the Pivot enum.
setRandomSeed
Section titled “setRandomSeed”setRandomSeed :: proc (seed: u64)Initializes the random number generator with a specific seed.
shift :: proc { _circleShift, _rectangleShift,}Moves Circle or Rectangle by the delta vector.
sign :: proc ( input: $T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns:
- +1, if
inputis greater than zero. - 0, if
inputis equal to zero. - -1, if
inputis smaller than zero. For vectors returns a component-wise sign vector.
sin :: proc ( angle: $T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Retuns the sine of angle (in radians).
- For scalars: Standard trigonometric sin function.
- For vectors: Component-wise sine.
sqrt :: proc ( input: $T,) -> T where intrinsics.type_is_array(T) || intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Returns the square root of input value.
Works with Vectors, Matrices and scalars.
If an integer is passed as an argument, it’s rounded on return.
stringHexToColor
Section titled “stringHexToColor”stringHexToColor :: proc (hexString: string) -> ColorParses a hex string (e.g. “#FF0000” or “FF0000FF”) into a normalized Color.
Supports both 6-digit (assumes alpha = 1.0) and 8-digit formats.
Handles optional leading ’#‘.
toDegrees
Section titled “toDegrees”toDegrees :: proc ( radians: $T,) -> T where intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Converts radians to degrees.
toRadians
Section titled “toRadians”toRadians :: proc ( degrees: $T,) -> T where intrinsics.type_is_float(T) || intrinsics.type_is_integer(T)Converts degrees to radians.
transformPoint
Section titled “transformPoint”transformPoint :: proc (mat: Matrix4, point: Vector2) -> Vector2Multiplies a vector by a matrix, effectively transforming the point. Assumes z = 0 and w = 1.
vectorToAngle
Section titled “vectorToAngle”vectorToAngle :: atan2An alias for the atan2 function.