APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.math

public Class Matrix4

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.Serializable

Encapsulates a column major 4 by 4 matrix. Like the Vector3 class it allows the chaining of methods by returning a reference to itself. For example:
Matrix4 mat = new Matrix4().trn(position).mul(camera.combined);
Author
[email protected]

Field Summary

Modifier and TypeField and Description
pack-priv static final Vector3
pack-priv static final Vector3
pack-priv static final Vector3
public static final int
M00

XX: Typically the unrotated X component for scaling, also the cosine of the angle when rotated on the Y and/or Z axis.

public static final int
M01

XY: Typically the negative sine of the angle when rotated on the Z axis.

public static final int
M02

XZ: Typically the sine of the angle when rotated on the Y axis.

public static final int
M03

XW: Typically the translation of the X component.

public static final int
M10

YX: Typically the sine of the angle when rotated on the Z axis.

public static final int
M11

YY: Typically the unrotated Y component for scaling, also the cosine of the angle when rotated on the X and/or Z axis.

public static final int
M12

YZ: Typically the negative sine of the angle when rotated on the X axis.

public static final int
M13

YW: Typically the translation of the Y component.

public static final int
M20

ZX: Typically the negative sine of the angle when rotated on the Y axis.

public static final int
M21

ZY: Typical the sine of the angle when rotated on the X axis.

public static final int
M22

ZZ: Typically the unrotated Z component for scaling, also the cosine of the angle when rotated on the X and/or Y axis.

public static final int
M23

ZW: Typically the translation of the Z component.

public static final int
M30

WX: Typically the value zero.

public static final int
M31

WY: Typically the value zero.

public static final int
M32

WZ: Typically the value zero.

public static final int
M33

WW: Typically the value one.

pack-priv static final Quaternion
pack-priv static final Quaternion
pack-priv static final Vector3
private static final long
pack-priv static final Vector3
pack-priv static final Matrix4
pack-priv static final Vector3
pack-priv static final Vector3
public final float[]

Constructor Summary

AccessConstructor and Description
public
Matrix4()

Constructs an identity matrix

public
Matrix4(Matrix4
The matrix to copy. (This matrix is not modified)
matrix
)

Constructs a matrix from the given matrix.

public
Matrix4(float[]
The float array to copy. Remember that this matrix is in column major order. (The float array is not modified)
values
)

Constructs a matrix from the given float array.

public
Matrix4(Quaternion
The quaternion to be copied. (The quaternion is not modified)
quaternion
)

Constructs a rotation matrix from the given Quaternion.

public
Matrix4(Vector3
The translation
position
,
Quaternion
The rotation, must be normalized
rotation
,
Vector3
The scale
scale
)

Construct a matrix from the given translation, rotation and scale.

Method Summary

Modifier and TypeMethod and Description
public Matrix4

Returns:

This matrix for chaining
avg
(Matrix4
The other transform
other
,
float
Weight of this transform; weight of the other transform is (1 - w)
w
)

Averages the given transform with this one and stores the result in this matrix.

public Matrix4

Returns:

This matrix for chaining
avg
(Matrix4[]
List of transforms
t
)

Averages the given transforms and stores the result in this matrix.

public Matrix4

Returns:

This matrix for chaining
avg
(Matrix4[]
List of transforms
t
,
float[]
List of weights
w
)

Averages the given transforms with the given weights and stores the result in this matrix.

public Matrix4
cpy()

Returns: a copy of this matrix

public float
det()

Returns: The determinant of this matrix

public static float

Returns:

the determinante.
det
(float[]
the matrix values.
values
)

Computes the determinante of the given matrix.

public float
det3x3()

Returns: The determinant of the 3x3 upper left matrix

public void
extract4x3Matrix(float[]
the destination matrix
dst
)

Copies the 4x3 upper-left sub-matrix into float array.

public Quaternion

Returns:

The provided Quaternion for chaining.
getRotation
(Quaternion
The Quaternion to receive the rotation
rotation
,
boolean
True to normalize the axes, necessary when the matrix might also include scaling.
normalizeAxes
)

Gets the rotation of this matrix.

public Quaternion

Returns:

The provided Quaternion for chaining.
getRotation
(Quaternion
The Quaternion to receive the rotation
rotation
)

Gets the rotation of this matrix.

public Vector3
getScale(Vector3
The vector which will receive the (non-negative) scale components on each axis.
scale
)

Returns: The provided vector for chaining.

public float
getScaleX()

Returns: the scale factor on the X axis (non-negative)

public float
getScaleXSquared()

Returns: the squared scale factor on the X axis

public float
getScaleY()

Returns: the scale factor on the Y axis (non-negative)

public float
getScaleYSquared()

Returns: the squared scale factor on the Y axis

public float
getScaleZ()

Returns: the scale factor on the X axis (non-negative)

public float
getScaleZSquared()

Returns: the squared scale factor on the Z axis

public Vector3
public float[]
getValues()

Returns: the backing float array

public boolean
hasRotationOrScaling()

Returns: True if this matrix has any rotation or scaling, false otherwise

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
idt
()

Sets the matrix to an identity matrix.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
inv
()

Inverts the matrix.

public static boolean

Returns:

false in case the inverse could not be calculated, true otherwise.
inv
(float[]
the matrix values.
values
)

Computes the inverse of the given matrix.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
lerp
(Matrix4
the matrix
matrix
,
float
the alpha value in the range [0,1]
alpha
)

Linearly interpolates between this matrix and the given matrix mixing by alpha

public Matrix4

Returns:

This matrix for the purpose of chaining operations together.
mul
(Matrix4
The other matrix to multiply by.
matrix
)

Postmultiplies this matrix with the given matrix, storing the result in this matrix.

public static void
mul(float[]
the first matrix.
mata
,
float[]
the second matrix.
matb
)

Multiplies the matrix mata with matrix matb, storing the result in mata.

public Matrix4

Returns:

This matrix for the purpose of chaining operations together.
mulLeft
(Matrix4
The other matrix to multiply by.
matrix
)

Premultiplies this matrix with the given matrix, storing the result in this matrix.

public static native void
mulVec(float[]
the matrix
mat
,
float[]
the vectors
vecs
,
int
the offset into the vectors array
offset
,
int
the number of vectors
numVecs
,
int
the stride between vectors in floats
stride
)

Multiplies the vectors with the given matrix.

public static void
mulVec(float[]
the matrix
mat
,
float[]
the vector.
vec
)

Multiplies the vector with the given matrix.

public static native void
prj(float[]
the matrix
mat
,
float[]
the vectors
vecs
,
int
the offset into the vectors array
offset
,
int
the number of vectors
numVecs
,
int
the stride between vectors in floats
stride
)

Multiplies the vectors with the given matrix, , performing a division by w.

public static void
prj(float[]
the matrix
mat
,
float[]
the vector.
vec
)

Multiplies the vector with the given matrix, performing a division by w.

public static native void
rot(float[]
the matrix
mat
,
float[]
the vectors
vecs
,
int
the offset into the vectors array
offset
,
int
the number of vectors
numVecs
,
int
the stride between vectors in floats
stride
)

Multiplies the vectors with the top most 3x3 sub-matrix of the given matrix.

public static void
rot(float[]
the matrix
mat
,
float[]
the vector.
vec
)

Multiplies the vector with the top most 3x3 sub-matrix of the given matrix.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
rotate
(Vector3
The vector axis to rotate around.
axis
,
float
The angle in degrees.
degrees
)

Postmultiplies this matrix with a (counter-clockwise) rotation matrix.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
rotate
(float
The x-axis component of the vector to rotate around.
axisX
,
float
The y-axis component of the vector to rotate around.
axisY
,
float
The z-axis component of the vector to rotate around.
axisZ
,
float
The angle in degrees
degrees
)

Postmultiplies this matrix with a (counter-clockwise) rotation matrix.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
rotate
(Quaternion rotation)

Postmultiplies this matrix with a (counter-clockwise) rotation matrix.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together
rotate
(final Vector3
The base vector
v1
,
final Vector3
The target vector
v2
)

Postmultiplies this matrix by the rotation between two vectors.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
rotateRad
(Vector3
The vector axis to rotate around.
axis
,
float
The angle in radians.
radians
)

Postmultiplies this matrix with a (counter-clockwise) rotation matrix.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
rotateRad
(float
The x-axis component of the vector to rotate around.
axisX
,
float
The y-axis component of the vector to rotate around.
axisY
,
float
The z-axis component of the vector to rotate around.
axisZ
,
float
The angle in radians
radians
)

Postmultiplies this matrix with a (counter-clockwise) rotation matrix.

public Matrix4

Returns:

This matrix for chaining
rotateTowardDirection
(final Vector3
direction to rotate toward
direction
,
final Vector3
up vector
up
)

Post-multiplies this matrix by a rotation toward a direction.

public Matrix4

Returns:

This matrix for chaining
rotateTowardTarget
(final Vector3
the target to rotate to
target
,
final Vector3
the up vector
up
)

Post-multiplies this matrix by a rotation toward a target.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
scale
(float
The scale in the x-axis.
scaleX
,
float
The scale in the y-axis.
scaleY
,
float
The scale in the z-axis.
scaleZ
)

Postmultiplies this matrix with a scale matrix.

public Matrix4
scl(Vector3 scale)

public Matrix4
scl(float x, float y, float z)

public Matrix4
scl(float scale)

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
set
(Matrix4
The matrix that is to be copied. (The given matrix is not modified)
matrix
)

Sets the matrix to the given matrix.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
set
(float[]
The matrix, in float form, that is to be copied. Remember that this matrix is in column major order.
values
)

Sets the matrix to the given matrix as a float array.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
set
(Quaternion
The quaternion that is to be used to set this matrix.
quaternion
)

Sets the matrix to a rotation matrix representing the quaternion.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
set
(float
The X component of the quaternion that is to be used to set this matrix.
quaternionX
,
float
The Y component of the quaternion that is to be used to set this matrix.
quaternionY
,
float
The Z component of the quaternion that is to be used to set this matrix.
quaternionZ
,
float
The W component of the quaternion that is to be used to set this matrix.
quaternionW
)

Sets the matrix to a rotation matrix representing the quaternion.

public Matrix4

Returns:

This matrix for chaining
set
(Vector3
The translation
position
,
Quaternion
The rotation, must be normalized
orientation
)

Set this matrix to the specified translation and rotation.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
set
(float
The X component of the translation that is to be used to set this matrix.
translationX
,
float
The Y component of the translation that is to be used to set this matrix.
translationY
,
float
The Z component of the translation that is to be used to set this matrix.
translationZ
,
float
The X component of the quaternion that is to be used to set this matrix.
quaternionX
,
float
The Y component of the quaternion that is to be used to set this matrix.
quaternionY
,
float
The Z component of the quaternion that is to be used to set this matrix.
quaternionZ
,
float
The W component of the quaternion that is to be used to set this matrix.
quaternionW
)

Sets the matrix to a rotation matrix representing the translation and quaternion.

public Matrix4

Returns:

This matrix for chaining
set
(Vector3
The translation
position
,
Quaternion
The rotation, must be normalized
orientation
,
Vector3
The scale
scale
)

Set this matrix to the specified translation, rotation and scale.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
set
(float
The X component of the translation that is to be used to set this matrix.
translationX
,
float
The Y component of the translation that is to be used to set this matrix.
translationY
,
float
The Z component of the translation that is to be used to set this matrix.
translationZ
,
float
The X component of the quaternion that is to be used to set this matrix.
quaternionX
,
float
The Y component of the quaternion that is to be used to set this matrix.
quaternionY
,
float
The Z component of the quaternion that is to be used to set this matrix.
quaternionZ
,
float
The W component of the quaternion that is to be used to set this matrix.
quaternionW
,
float
The X component of the scaling that is to be used to set this matrix.
scaleX
,
float
The Y component of the scaling that is to be used to set this matrix.
scaleY
,
float
The Z component of the scaling that is to be used to set this matrix.
scaleZ
)

Sets the matrix to a rotation matrix representing the translation and quaternion.

public Matrix4
set(Vector3
The x-axis.
xAxis
,
Vector3
The y-axis.
yAxis
,
Vector3
The z-axis.
zAxis
,
Vector3
The translation vector.
pos
)

Sets the four columns of the matrix which correspond to the x-, y- and z-axis of the vector space this matrix creates as well as the 4th column representing the translation of any point that is multiplied by this matrix.

public Matrix4
set(Matrix3
the matrix
mat
)

Sets this matrix to the given 3x3 matrix.

public Matrix4

Returns:

This matrix for chaining
set
(Affine2
the affine matrix
affine
)

Sets this matrix to the given affine matrix.

public Matrix4

Returns:

This matrix for chaining
setAsAffine
(Affine2
the source matrix
affine
)

Assumes that this matrix is a 2D affine transformation, copying only the relevant components.

public Matrix4

Returns:

This matrix for chaining
setAsAffine
(Matrix4
the source matrix
mat
)

Assumes that both matrices are 2D affine transformations, copying only the relevant components.

public Matrix4

Returns:

This matrix
setFromEulerAngles
(float
the yaw in degrees
yaw
,
float
the pitch in degrees
pitch
,
float
the roll in degrees
roll
)

Sets this matrix to a rotation matrix from the given euler angles.

public Matrix4

Returns:

This matrix
setFromEulerAnglesRad
(float
the yaw in radians
yaw
,
float
the pitch in radians
pitch
,
float
the roll in radians
roll
)

Sets this matrix to a rotation matrix from the given euler angles.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToLookAt
(Vector3
The direction vector
direction
,
Vector3
The up vector
up
)

Sets the matrix to a look at matrix with a direction and an up vector.

public Matrix4

Returns:

This matrix
setToLookAt
(Vector3
the position
position
,
Vector3
the target
target
,
Vector3
the up vector
up
)

Sets this matrix to a look at matrix with the given position, target and up vector.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToOrtho
(float
The left clipping plane
left
,
float
The right clipping plane
right
,
float
The bottom clipping plane
bottom
,
float
The top clipping plane
top
,
float
The near clipping plane
near
,
float
The far clipping plane
far
)

Sets the matrix to an orthographic projection like glOrtho (http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml) following the OpenGL equivalent

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToOrtho2D
(float
The x-coordinate of the origin
x
,
float
The y-coordinate of the origin
y
,
float
The width
width
,
float
The height
height
)

Sets this matrix to an orthographic projection matrix with the origin at (x,y) extending by width and height.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToOrtho2D
(float
The x-coordinate of the origin
x
,
float
The y-coordinate of the origin
y
,
float
The width
width
,
float
The height
height
,
float
The near plane
near
,
float
The far plane
far
)

Sets this matrix to an orthographic projection matrix with the origin at (x,y) extending by width and height, having a near and far plane.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToProjection
(float
The near plane
near
,
float
The far plane
far
,
float
The field of view of the height in degrees
fovy
,
float
The "width over height" aspect ratio
aspectRatio
)

Sets the matrix to a projection matrix with a near- and far plane, a field of view in degrees and an aspect ratio.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToProjection
(float left, float right, float bottom, float top, float
The near plane
near
,
float
The far plane
far
)

Sets the matrix to a projection matrix with a near/far plane, and left, bottom, right and top specifying the points on the near plane that are mapped to the lower left and upper right corners of the viewport.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToRotation
(Vector3
The axis
axis
,
float
The angle in degrees
degrees
)

Sets the matrix to a rotation matrix around the given axis.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToRotation
(float
The x-component of the axis
axisX
,
float
The y-component of the axis
axisY
,
float
The z-component of the axis
axisZ
,
float
The angle in degrees
degrees
)

Sets the matrix to a rotation matrix around the given axis.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together
setToRotation
(final Vector3
The base vector
v1
,
final Vector3
The target vector
v2
)

Set the matrix to a rotation matrix between two vectors.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together
setToRotation
(final float
The base vectors x value
x1
,
final float
The base vectors y value
y1
,
final float
The base vectors z value
z1
,
final float
The target vector x value
x2
,
final float
The target vector y value
y2
,
final float
The target vector z value
z2
)

Set the matrix to a rotation matrix between two vectors.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToRotationRad
(Vector3
The axis
axis
,
float
The angle in radians
radians
)

Sets the matrix to a rotation matrix around the given axis.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToRotationRad
(float
The x-component of the axis
axisX
,
float
The y-component of the axis
axisY
,
float
The z-component of the axis
axisZ
,
float
The angle in radians
radians
)

Sets the matrix to a rotation matrix around the given axis.

public Matrix4

Returns:

This matrix for chaining.
setToScaling
(Vector3
The scaling vector
vector
)

Sets this matrix to a scaling matrix

public Matrix4

Returns:

This matrix for chaining.
setToScaling
(float
The x-component of the scaling vector
x
,
float
The y-component of the scaling vector
y
,
float
The z-component of the scaling vector
z
)

Sets this matrix to a scaling matrix

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToTranslation
(Vector3
The translation vector
vector
)

Sets this matrix to a translation matrix, overwriting it first by an identity matrix and then setting the 4th column to the translation vector.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToTranslation
(float
The x-component of the translation vector.
x
,
float
The y-component of the translation vector.
y
,
float
The z-component of the translation vector.
z
)

Sets this matrix to a translation matrix, overwriting it first by an identity matrix and then setting the 4th column to the translation vector.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToTranslationAndScaling
(Vector3
The translation vector
translation
,
Vector3
The scaling vector
scaling
)

Sets this matrix to a translation and scaling matrix by first overwriting it with an identity and then setting the translation vector in the 4th column and the scaling vector in the diagonal.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setToTranslationAndScaling
(float
The x-component of the translation vector
translationX
,
float
The y-component of the translation vector
translationY
,
float
The z-component of the translation vector
translationZ
,
float
The x-component of the scaling vector
scalingX
,
float
The x-component of the scaling vector
scalingY
,
float
The x-component of the scaling vector
scalingZ
)

Sets this matrix to a translation and scaling matrix by first overwriting it with an identity and then setting the translation vector in the 4th column and the scaling vector in the diagonal.

public Matrix4
setToWorld(Vector3 position, Vector3 forward, Vector3 up)

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setTranslation
(Vector3
The translation vector
vector
)

Sets the 4th column to the translation vector.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
setTranslation
(float
The X coordinate of the translation vector
x
,
float
The Y coordinate of the translation vector
y
,
float
The Z coordinate of the translation vector
z
)

Sets the 4th column to the translation vector.

public Matrix4
toNormalMatrix()

removes the translational part and transposes the matrix.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
tra
()

Transposes the matrix.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
translate
(Vector3 translation)

Postmultiplies this matrix by a translation matrix.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
translate
(float
Translation in the x-axis.
x
,
float
Translation in the y-axis.
y
,
float
Translation in the z-axis.
z
)

Postmultiplies this matrix by a translation matrix.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
trn
(Vector3
The translation vector to add to the current matrix. (This vector is not modified)
vector
)

Adds a translational component to the matrix in the 4th column.

public Matrix4

Returns:

This matrix for the purpose of chaining methods together.
trn
(float
The x-component of the translation vector.
x
,
float
The y-component of the translation vector.
y
,
float
The z-component of the translation vector.
z
)

Adds a translational component to the matrix in the 4th column.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait