Math module

Matrix4

structSource

A 4x4 matrix, row-major: _11 to _44, also v[row] as Vector4 rows, coef[row][col], floats[16] and mat4x3 for the top three rows. It is the type for model, view and projection transforms: vectors are columns, m * v transforms v, a * b applies b first, and the translation lives in _14, _24, _34. Matrix4 * Vector4 gives a Vector4.

Upload it to a graphics API as it is: the 16 floats in memory are row 1 first, which a column-major API reads as the transpose, so transpose it or tell the API the data is row-major.

ExampleOpen in playground ↗
t := make_translation_matrix4(xyz(1, 2, 3));
s := make_scale_matrix4(xyz(2, 2, 2));
m := t * s; // scale first, then translate
assert(m * xyzw(1, 1, 1, 1) == xyzw(3, 4, 5, 1));
assert(m.v[0] == xyzw(2, 0, 0, 1));

Fields

_11: float
_12: float
_13: float
_14: float
_21: float
_22: float
_23: float
_24: float
_31: float
_32: float
_33: float
_34: float
_41: float
_42: float
_43: float
_44: float
v: [4] Vector4
coef: [4][4] float
floats: [16] float
mat4x3: Matrix4x3

See also