Skip to main content
Version: Phaser v4.0.0

Matrix4

A 4x4 column-major matrix stored as a Float32Array of 16 values. Used for 3D transformations including projection, view, and model matrices. This is the primary matrix class for WebGL rendering operations in Phaser, supporting perspective/orthographic projection, look-at camera setup, and affine transforms (translate, rotate, scale).

Adapted from gl-matrix by toji and vecmath by mattdesl

Constructor

new Matrix4([m])

Parameters

nametypeoptionaldescription
mPhaser.Math.Matrix4YesOptional Matrix4 to copy values from.

Scope: static

Source: src/math/Matrix4.js#L15
Since: 3.0.0

Public Members

val

val: Float32Array

Description:

The matrix values.

Source: src/math/Matrix4.js#L39
Since: 3.0.0


Public Methods

adjoint

<instance> adjoint()

Description:

Calculate the adjoint, or adjugate, of this Matrix.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L422
Since: 3.0.0


clone

<instance> clone()

Description:

Make a clone of this Matrix4.

Returns: Phaser.Math.Matrix4 - A clone of this Matrix4.

Source: src/math/Matrix4.js#L60
Since: 3.0.0


copy

<instance> copy(src)

Description:

Copy the values of a given Matrix into this Matrix.

Parameters:

nametypeoptionaldescription
srcPhaser.Math.Matrix4NoThe Matrix to copy the values from.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L137
Since: 3.0.0


determinant

<instance> determinant()

Description:

Calculate the determinant of this Matrix.

Returns: number - The determinant of this Matrix.

Source: src/math/Matrix4.js#L474
Since: 3.0.0


fromArray

<instance> fromArray(a)

Description:

Set the values of this Matrix from the given array.

Parameters:

nametypeoptionaldescription
aArray.<number>NoThe array to copy the values from. Must have at least 16 elements.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L154
Since: 3.0.0


fromQuat

<instance> fromQuat(q)

Description:

Set the values of this Matrix from the given Quaternion.

Parameters:

nametypeoptionaldescription
qPhaser.Math.QuaternionNoThe Quaternion to set the values of this Matrix from.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L1109
Since: 3.0.0


fromRotationTranslation

<instance> fromRotationTranslation(q, v)

Description:

Set the values of this Matrix from the given rotation Quaternion and translation Vector.

Parameters:

nametypeoptionaldescription
qPhaser.Math.QuaternionNoThe Quaternion to set rotation from.
vPhaser.Math.Vector3NoThe Vector to set translation from.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L1051
Since: 3.0.0


fromRotationXYTranslation

<instance> fromRotationXYTranslation(rotation, position, translateFirst)

Description:

Takes the rotation and position vectors and builds this Matrix4 from them.

Parameters:

nametypeoptionaldescription
rotationPhaser.Math.Vector3NoThe rotation vector.
positionPhaser.Math.Vector3NoThe position vector.
translateFirstbooleanNoShould the operation translate then rotate (true), or rotate then translate? (false)

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L1677
Since: 3.50.0


frustum

<instance> frustum(left, right, bottom, top, near, far)

Description:

Generate a frustum matrix with the given bounds.

Parameters:

nametypeoptionaldescription
leftnumberNoThe left bound of the frustum.
rightnumberNoThe right bound of the frustum.
bottomnumberNoThe bottom bound of the frustum.
topnumberNoThe top bound of the frustum.
nearnumberNoThe near bound of the frustum.
farnumberNoThe far bound of the frustum.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L1165
Since: 3.0.0


getInverse

<instance> getInverse(m)

Description:

Copies the given Matrix4 into this Matrix and then inverses it.

Parameters:

nametypeoptionaldescription
mPhaser.Math.Matrix4NoThe Matrix4 to invert into this Matrix4.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L328
Since: 3.50.0


getMaxScaleOnAxis

<instance> getMaxScaleOnAxis()

Description:

Returns the maximum axis scale from this Matrix4.

Returns: number - The maximum axis scale.

Source: src/math/Matrix4.js#L1748
Since: 3.50.0


identity

<instance> identity()

Description:

Reset this Matrix to an identity (default) matrix.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L280
Since: 3.0.0


invert

<instance> invert()

Description:

Invert this Matrix.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L345
Since: 3.0.0


lookAt

<instance> lookAt(eye, center, up)

Description:

Generate a look-at matrix with the given eye position, focal point, and up axis.

Parameters:

nametypeoptionaldescription
eyePhaser.Math.Vector3NoPosition of the viewer
centerPhaser.Math.Vector3NoPoint the viewer is looking at
upPhaser.Math.Vector3NoA Vector3 pointing up, used to orient the camera roll.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L1400
Since: 3.0.0


lookAtRH

<instance> lookAtRH(eye, target, up)

Description:

Generate a right-handed look-at matrix with the given eye position, target and up axis.

Parameters:

nametypeoptionaldescription
eyePhaser.Math.Vector3NoPosition of the viewer.
targetPhaser.Math.Vector3NoPoint the viewer is looking at.
upPhaser.Math.Vector3NoA Vector3 pointing up, used to orient the camera roll.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L1340
Since: 3.50.0


makeRotationAxis

<instance> makeRotationAxis(axis, angle)

Description:

Derive a rotation matrix around the given axis.

Parameters:

nametypeoptionaldescription
axisPhaser.Math.Vector3 | Phaser.Math.Vector4NoThe rotation axis.
anglenumberNoThe rotation angle in radians.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L818
Since: 3.0.0


multiply

<instance> multiply(src)

Description:

Multiply this Matrix by the given Matrix.

Parameters:

nametypeoptionaldescription
srcPhaser.Math.Matrix4NoThe Matrix to multiply this Matrix by.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L523
Since: 3.0.0


multiplyLocal

<instance> multiplyLocal(src)

Description:

Multiply the values of this Matrix4 by those given in the src argument.

Parameters:

nametypeoptionaldescription
srcPhaser.Math.Matrix4NoThe source Matrix4 that this Matrix4 is multiplied by.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L603
Since: 3.0.0


multiplyMatrices

<instance> multiplyMatrices(a, b)

Description:

Multiplies the two given Matrix4 objects and stores the results in this Matrix.

Parameters:

nametypeoptionaldescription
aPhaser.Math.Matrix4NoThe first Matrix4 to multiply.
bPhaser.Math.Matrix4NoThe second Matrix4 to multiply.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L658
Since: 3.50.0


multiplyToMat4

<instance> multiplyToMat4(src, out)

Description:

Multiplies this Matrix4 by the given src Matrix4 and stores the results in the out Matrix4.

Parameters:

nametypeoptionaldescription
srcPhaser.Math.Matrix4NoThe Matrix4 to multiply with this one.
outPhaser.Math.Matrix4NoThe receiving Matrix.

Returns: Phaser.Math.Matrix4 - This out Matrix4.

Source: src/math/Matrix4.js#L1604
Since: 3.50.0


ortho

<instance> ortho(left, right, bottom, top, near, far)

Description:

Generate an orthogonal projection matrix with the given bounds.

Parameters:

nametypeoptionaldescription
leftnumberNoThe left bound of the frustum.
rightnumberNoThe right bound of the frustum.
bottomnumberNoThe bottom bound of the frustum.
topnumberNoThe top bound of the frustum.
nearnumberNoThe near bound of the frustum.
farnumberNoThe far bound of the frustum.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L1291
Since: 3.0.0


perspective

<instance> perspective(fovy, aspect, near, far)

Description:

Generate a perspective projection matrix with the given bounds.

Parameters:

nametypeoptionaldescription
fovynumberNoVertical field of view in radians
aspectnumberNoAspect ratio. Typically viewport width / height.
nearnumberNoNear bound of the frustum.
farnumberNoFar bound of the frustum.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L1209
Since: 3.0.0


perspectiveLH

<instance> perspectiveLH(width, height, near, far)

Description:

Generate a left-handed perspective projection matrix with the given frustum dimensions.

Unlike perspective, this method takes explicit pixel width and height for the frustum rather than a field-of-view angle and aspect ratio.

Parameters:

nametypeoptionaldescription
widthnumberNoThe width of the frustum.
heightnumberNoThe height of the frustum.
nearnumberNoNear bound of the frustum.
farnumberNoFar bound of the frustum.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L1250
Since: 3.0.0


premultiply

<instance> premultiply(m)

Description:

Multiplies the given Matrix4 object with this Matrix.

This is the same as calling multiplyMatrices(m, this).

Parameters:

nametypeoptionaldescription
mPhaser.Math.Matrix4NoThe Matrix4 to multiply with this one.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L641
Since: 3.50.0


rotate

<instance> rotate(rad, axis)

Description:

Apply a rotation transformation to this Matrix.

Parameters:

nametypeoptionaldescription
radnumberNoThe angle in radians to rotate by.
axisPhaser.Math.Vector3NoThe axis to rotate upon.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L850
Since: 3.0.0


rotateX

<instance> rotateX(rad)

Description:

Rotate this matrix on its X axis.

Parameters:

nametypeoptionaldescription
radnumberNoThe angle in radians to rotate by.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L934
Since: 3.0.0


rotateY

<instance> rotateY(rad)

Description:

Rotate this matrix on its Y axis.

Parameters:

nametypeoptionaldescription
radnumberNoThe angle to rotate by, in radians.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L973
Since: 3.0.0


rotateZ

<instance> rotateZ(rad)

Description:

Rotate this matrix on its Z axis.

Parameters:

nametypeoptionaldescription
radnumberNoThe angle to rotate by, in radians.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L1012
Since: 3.0.0


scale

<instance> scale(v)

Description:

Apply a scale transformation to this Matrix.

Uses the x, y and z components of the given Vector to scale the Matrix.

Parameters:

nametypeoptionaldescription
vPhaser.Math.Vector3 | Phaser.Math.Vector4NoThe Vector to scale this Matrix with.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L767
Since: 3.0.0


scaleXYZ

<instance> scaleXYZ(x, y, z)

Description:

Apply a scale transformation to this Matrix.

Parameters:

nametypeoptionaldescription
xnumberNoThe x component.
ynumberNoThe y component.
znumberNoThe z component.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L784
Since: 3.16.0


scaling

<instance> scaling(x, y, z)

Description:

Set the scaling values of this Matrix.

Parameters:

nametypeoptionaldescription
xnumberNoThe x scaling value.
ynumberNoThe y scaling value.
znumberNoThe z scaling value.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L254
Since: 3.0.0


set

<instance> set(src)

Description:

This method is an alias for Matrix4.copy.

Parameters:

nametypeoptionaldescription
srcPhaser.Math.Matrix4NoThe Matrix to set the values of this Matrix from.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L73
Since: 3.0.0


setValues

<instance> setValues(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33)

Description:

Sets all values of this Matrix4.

Parameters:

nametypeoptionaldescription
m00numberNoThe m00 value.
m01numberNoThe m01 value.
m02numberNoThe m02 value.
m03numberNoThe m03 value.
m10numberNoThe m10 value.
m11numberNoThe m11 value.
m12numberNoThe m12 value.
m13numberNoThe m13 value.
m20numberNoThe m20 value.
m21numberNoThe m21 value.
m22numberNoThe m22 value.
m23numberNoThe m23 value.
m30numberNoThe m30 value.
m31numberNoThe m31 value.
m32numberNoThe m32 value.
m33numberNoThe m33 value.

Returns: Phaser.Math.Matrix4 - This Matrix4 instance.

Source: src/math/Matrix4.js#L88
Since: 3.50.0


setWorldMatrix

<instance> setWorldMatrix(rotation, position, scale, [viewMatrix], [projectionMatrix])

Description:

Generate a world matrix from the given rotation, position, scale, view matrix and projection matrix.

Parameters:

nametypeoptionaldescription
rotationPhaser.Math.Vector3NoThe rotation of the world matrix.
positionPhaser.Math.Vector3NoThe position of the world matrix.
scalePhaser.Math.Vector3NoThe scale of the world matrix.
viewMatrixPhaser.Math.Matrix4YesThe view matrix.
projectionMatrixPhaser.Math.Matrix4YesThe projection matrix.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L1567
Since: 3.0.0


transform

<instance> transform(position, scale, rotation)

Description:

Generates a transform matrix based on the given position, scale and rotation.

Parameters:

nametypeoptionaldescription
positionPhaser.Math.Vector3NoThe position vector.
scalePhaser.Math.Vector3NoThe scale vector.
rotationPhaser.Math.QuaternionNoThe rotation quaternion.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L184
Since: 3.50.0


translate

<instance> translate(v)

Description:

Translate this Matrix using the given Vector.

Parameters:

nametypeoptionaldescription
vPhaser.Math.Vector3 | Phaser.Math.Vector4NoThe Vector to translate this Matrix with.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L728
Since: 3.0.0


translateXYZ

<instance> translateXYZ(x, y, z)

Description:

Translate this Matrix using the given values.

Parameters:

nametypeoptionaldescription
xnumberNoThe x component.
ynumberNoThe y component.
znumberNoThe z component.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L743
Since: 3.16.0


transpose

<instance> transpose()

Description:

Transpose this Matrix.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L293
Since: 3.0.0


xyz

<instance> xyz(x, y, z)

Description:

Set the x, y and z values of this Matrix.

Parameters:

nametypeoptionaldescription
xnumberNoThe x value.
ynumberNoThe y value.
znumberNoThe z value.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L229
Since: 3.0.0


yawPitchRoll

<instance> yawPitchRoll(yaw, pitch, roll)

Description:

Set the values of this matrix from the given yaw, pitch and roll values.

Parameters:

nametypeoptionaldescription
yawnumberNoThe yaw angle, in radians.
pitchnumberNoThe pitch angle, in radians.
rollnumberNoThe roll angle, in radians.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L1506
Since: 3.0.0


zero

<instance> zero()

Description:

Reset this Matrix.

Sets all values to 0.

Returns: Phaser.Math.Matrix4 - This Matrix4.

Source: src/math/Matrix4.js#L169
Since: 3.0.0