Matrices

Luanti uses 4x4 matrices to represent linear transformations of 3D vectors. For this, 3D vectors are embedded into 4D space, typically using homogenous coordinates (for positions) or by adding a fourth component of zero (for directions). This class was added in Luanti 5.19.0.

The matrices use column-major conventions. Vectors are treated as column vectors. This means the first column is the image of the vector (1, 0, 0, 0), the second column is the image of (0, 1, 0, 0), and so on. Thus the translation is in the last column.

The arithmetic precision of matrix computations may change (improve) in the future. Matrices currently use 32-bit floats (less precision than the Lua number type).

Matrices are very suitable for constructing, composing and applying linear 4D transformations (which are equivalent to 3D projective transformations when using homogenous coordinates).

Matrices should be avoided for exact storage of TRS (translation, rotation, scale) transformations if the properties need to be handled separately: Decomposition into rotation and scale will be expensive and inexact. You should instead store the translation, rotation and scale.

Constructors

  • Matrix4.new(r1c1, r1c2, ..., r4c4): Constructs a matrix from the given 16 numbers in row-major order.
  • Matrix4.identity(): Constructs an identity matrix.
  • Matrix4.full(number): Constructs a matrix where all entries are the given number.
  • Matrix4.translation(vec): Constructs a matrix that translates vectors by the given vector.
  • Matrix4.rotation(rot): Constructs a matrix that applies the given Rotation to vectors.
  • Matrix4.scale(s): Constructs a matrix that applies the given component-wise scaling factors to vectors. s can be a vector or a number.
  • Matrix4.trs([t], [r], [s]): Shorthand for Matrix4.compose(Matrix4.translation(t), Matrix4.rotation(r), Matrix4.scale(s)). All parameters are optional and default to identity transforms.
  • Matrix4.reflection(normal): Constructs a matrix that reflects vectors at the plane with the given plane normal vector (which need not be normalized).
  • Matrix4.compose(...): Returns the composition of the given matrices.
  • Matrix4.compose() is equivalent to Matrix4.identity().
  • Matrix4.compose(mat) copies the matrix.
  • Matrix4.compose(...) for at least two matrices composes the given matrices in right-to-left order. This means that Matrix4.compose(second, first):transform_4d(x, y, z, w) is equivalent to second:transform_4d(first:transform_4d(x, y, z, w)): The composed matrix first applies first, then second to a vector.

Container utilities:

For all of the below methods, row and column indices range from 1 to 4.

  • mat:get(row, col): Returns the number in the given row and column.
  • mat:set(row, col, number): Set the entry in the given row and column to the given number.
  • x, y, z, w = mat:get_row(row): Get the 4 numbers in the given row.
  • mat:set_row(row, x, y, z, w): Set the 4 numbers in the given row.
  • x, y, z, w = mat:get_col(col): Get the 4 numbers in the given column.
  • mat:set_col(col, x, y, z, w): Set the 4 numbers in the given column.
  • mat:copy(): Returns a new matrix containing the same numbers.
  • r1c1, r1c2, ..., r4c4 = mat:unpack(): Get the 16 numbers in the matrix in row-major order. Matrix4.new(mat:unpack()) is thus equivalent to mat:copy().

Linear algebra:

  • Vector transformations:
  • x, y, z, w = mat:transform_4d(x, y, z, w): Apply the matrix to a 4d vector.
  • mat:transform_pos(pos):
    • Apply the matrix to a vector representing a position.
    • Applies the transformation as if w = 1 and discards the resulting w component.
  • mat:transform_dir(dir):
    • Apply the matrix to a vector representing a direction.
    • Ignores the fourth row and column; does not apply the translation (w = 0).
  • mat:compose(...): Shorthand for Matrix4.compose(mat, ...).
  • mat:determinant(): Returns the determinant.
  • mat:invert(): Returns a newly created inverse, or nil if the matrix is (close to being) singular.
  • mat:transpose(): Returns a transposed copy of the matrix.
  • mat:equals(other, [tolerance = 0]): Returns whether all components differ in absolute value at most by the given tolerance.
  • m1 == m2: Returns whether m1 and m2 are identical (tolerance = 0).
  • mat:is_affine_transform([tolerance = 0]): Whether the matrix is an affine transformation in 3d space, meaning it is a 3d linear transformation plus a translation. (This is the case if the last row is approximately 0, 0, 0, 1.)

For working with affine transforms, the following methods are available:

  • mat:get_translation(): Returns the translation as a vector.
  • mat:set_translation(vec): Sets (overwrites) the translation in the last column.

For TRS transforms specifically, let mat = Matrix4.compose(Matrix4.translation(t), Matrix4.rotation(r), Matrix4.scale(s)). Then we can decompose mat further. Note that mat must not shear or reflect.

  • rotation, scale = mat:get_rotation_scale(): Extracts a Rotation equivalent to r, along with the corresponding component-wise scaling factors as a vector.

Operators

Similar to vectors, matrices define some element-wise arithmetic operators:

  • m1 + m2: Returns the sum of both matrices.
  • m1 - m2: Shorthand for m1 + (-m2).
  • -m: Returns the additive inverse.
  • m * s or s * m: Returns the matrix m scaled by the scalar s.
  • Note: All entries are scaled, including the last column: The matrix may not be an affine transform afterwards.

Matrices also define a __tostring metamethod. This is only intended for human readability and not for serialization.