Skip to content

Matrix

Client-side
Server-side
Shared

Category: Matrix

Matrices are one of the most powerful features of MTA OOP. We did have a presence of Matrices before with getElementMatrix, but we were given an ugly disgusting table to play with. Now, with the new Matrix class, we can make and magically manipulate Matrices.

OOP Methods Help! I don't understand this!

create

Default constructor for the Matrix class. Returns a Matrix object. Can be instantiated in multiple ways.

Matrix(Vector3 position[, Vector3 rotation])
  • position : Vector3 – The position vector of the matrix
  • rotation (optional): Vector3 – The rotation vector of the matrix
Matrix(Matrix matrixToClone)
  • matrixToClone : Matrix – A matrix you want to make a clone of
Matrix()

Initialize a zero matrix

transformPosition

Transforms a given position vector using the Matrix.

Vector3 Matrix:transformPosition(Vector3 position)
  • position: Vector3 – The position vector you want to transform

getPosition

Returns the position vector of the matrix.

Vector3 Matrix:getPosition()

getRotation

Returns the rotation vector of the matrix.

Vector3 Matrix:getRotation()

getForward

Returns the forward vector of the matrix.

Vector3 Matrix:getForward()

getRight

Returns the right vector of the matrix.

Vector3 Matrix:getRight()

getUp

Returns the up vector of the matrix.

Vector3 Matrix:getUp()

Code Examples

Say you wanted to create a bin - object 1337 - two units in front of a player. You don't want to manually do trigonometry and you don't want to play with the complicated tables. You just want to get the position two units in front of the player whilst taking into account the rotation of the player. You only need to use Matrix.getForward, Matrix.getPosition and getElementMatrix. It's just:

Object ( 1337, player.matrix.position + player.matrix.forward * 2 )

If you wanted the opposite of matrix.forward you'll still use matrix.forward but you minus it instead of matrix.backward which doesn't exist. Same applies with matrix.right and matrix.up. To make the bin object appear behind a player:

Object ( 1337, player.matrix.position - player.matrix.forward * 2 )

Say you'd like to get find the position underneath a vehicle. This is the position at any rotation. So if it was upside down, the Z value would be higher than the vehicle Z value. If the vehicle was the right way round, the Z value for underneath car would be less than the Z value for the car.

--
-- Instead of:
--
local matrix = getElementMatrix(vehicle)
local offX = 0 * matrix[1][1] + 0 * matrix[2][1] - 1 * matrix[3][1] + matrix[4][1]
local offY = 0 * matrix[1][2] + 0 * matrix[2][2] - 1 * matrix[3][2] + matrix[4][2]
local offZ = 0 * matrix[1][3] + 0 * matrix[2][3] - 1 * matrix[3][3] + matrix[4][3]
local pX, pY, pZ = getElementPosition(vehicle)
local positionBelow = {offX-pX, offY-pY, offZ-pZ}
--
-- You only have to do:
--
local positionBelow = vehicle.position - vehicle.matrix.up