Vectors and Matrices

Adama has built-in linear algebra through the Vector and Matrix global objects. If you're building a game, doing physics simulations, or working with spatial data, this is where you look.

Vector Types

Three vector types:

Type Components Description
vec2 x, y 2D vector
vec3 x, y, z 3D vector
vec4 x, y, z, w 4D vector

Vector Operations

Arithmetic

Function Description Returns
Vector.add(a, b) Component-wise addition vecN
Vector.sub(a, b) Component-wise subtraction vecN
Vector.scale(s, v) Scalar multiplication vecN
Vector.cmult(a, b) Component-wise multiply vecN
vec2 a = @vec2(1.0, 2.0);
vec2 b = @vec2(3.0, 4.0);

vec2 sum = Vector.add(a, b);        // (4.0, 6.0)
vec2 diff = Vector.sub(a, b);       // (-2.0, -2.0)
vec2 scaled = Vector.scale(2.0, a); // (2.0, 4.0)
vec2 product = Vector.cmult(a, b);  // (3.0, 8.0)

All of these work on vec2, vec3, and vec4.

Products

Function Description Returns
Vector.dot(a, b) Dot product double
Vector.cross(a, b) (3D) Cross product vec3
Vector.cross(a, b) (2D) 2D cross product (z component) double
vec3 a = @vec3(1.0, 0.0, 0.0);
vec3 b = @vec3(0.0, 1.0, 0.0);

double d = Vector.dot(a, b);      // 0.0
vec3 c = Vector.cross(a, b);      // (0.0, 0.0, 1.0)

Length and Normalization

Function Description Returns
Vector.length(v) Magnitude of vector double
Vector.normalize(v) Unit vector in same dir maybe<vecN>
vec2 v = @vec2(3.0, 4.0);

double len = Vector.length(v);              // 5.0
maybe<vec2> unit = Vector.normalize(v);     // (0.6, 0.8)

normalize returns maybe because you can't normalize the zero vector -- there's no direction to preserve.

Utility

Function Description Returns
Vector.flipToUp(v) Flip 3D vector to point upward vec3

Matrix Types

Type Size Description
matrix2 2x2 2D rotation/scale
matrix3 3x3 3D rotation/scale
matrix4 4x4 Full 3D transformation
matrixh4 4x4 Homogeneous (rotation + translation)

Matrix Operations

Transformation

Function Description Returns
Matrix.transform(m, v) Apply matrix to vector vecN
matrix3 rot = Matrix.to_rotation_z(1.5708);  // ~90 degrees
vec3 v = @vec3(1.0, 0.0, 0.0);
vec3 rotated = Matrix.transform(rot, v);      // ~(0.0, 1.0, 0.0)

Multiplication

Function Description Returns
Matrix.multiply(a, b) Matrix multiplication matrixN
matrix3 r1 = Matrix.to_rotation_z(0.5);
matrix3 r2 = Matrix.to_rotation_z(0.5);
matrix3 combined = Matrix.multiply(r1, r2);  // rotation by 1.0 radian

Rotation Matrices

Function Description Returns
Matrix.to_rotation(radians) 2D rotation matrix matrix2
Matrix.to_rotation_x(radians) Rotation around X axis matrix3
Matrix.to_rotation_y(radians) Rotation around Y axis matrix3
Matrix.to_rotation_z(radians) Rotation around Z axis matrix3
Matrix.to_rotation_around(rad, axis) Rotation around arbitrary axis matrix3
Matrix.rotate_around(axis, rad) Same, argument order swapped matrix3
Matrix.to_matrix(complex) Complex number to 2D rotation matrix2

All rotation functions also accept maybe<double> for the radians parameter.

matrix2 rot2d = Matrix.to_rotation(Math.PI() / 4.0);  // 45 degrees
matrix3 rotX = Matrix.to_rotation_x(1.0);              // 1 radian around X
matrix3 rotY = Matrix.to_rotation_y(0.5);              // 0.5 radians around Y

Inverse

Function Description Returns
Matrix.inverse(m) Compute matrix inverse maybe<matrixN>

Returns maybe because singular matrices don't have inverses. That's just how math works.

matrix2 m = Matrix.to_rotation(1.0);
maybe<matrix2> inv = Matrix.inverse(m);  // inverse rotation

Conversion

Function Description Returns
Matrix.to_3(m2) Extend matrix2 to matrix3 matrix3
Matrix.to_4(m2) or to_4(m3) Extend to matrix4 matrix4
Matrix.to_4(mh4) Convert matrixh4 to matrix4 matrix4
Matrix.combine(rotation, translation) Create matrixh4 from rotation + offset matrixh4

Method Summary

Vector

Function Input Types Returns
Vector.add(a, b) vec2/3/4 vecN
Vector.sub(a, b) vec2/3/4 vecN
Vector.scale(s, v) double, vecN vecN
Vector.cmult(a, b) vec2/3/4 vecN
Vector.dot(a, b) vec2/3/4 double
Vector.cross(a, b) vec3 (or vec2) vec3/double
Vector.length(v) vec2/3/4 double
Vector.normalize(v) vec2/3 maybe<vecN>
Vector.flipToUp(v) vec3 vec3

Matrix

Function Returns
Matrix.transform(m, v) vecN
Matrix.multiply(a, b) matrixN
Matrix.to_rotation(rad) matrix2
Matrix.to_rotation_x(rad) matrix3
Matrix.to_rotation_y(rad) matrix3
Matrix.to_rotation_z(rad) matrix3
Matrix.to_rotation_around(rad, axis) matrix3
Matrix.rotate_around(axis, rad) matrix3
Matrix.to_matrix(complex) matrix2
Matrix.inverse(m) maybe<matrixN>
Matrix.to_3(m2) / to_4(mN) matrixN
Matrix.combine(rot, trans) matrixh4
Previous Utilities
Next Rxhtml