Class Matrix


  • public class Matrix
    extends Object
    4x4 matrix affine matrix implementation. Matrix is represented in column major order: | m0 m4 m8 m12 | | m1 m5 m9 m13 | | m2 m6 m10 m14 | | m3 m7 m11 m15 |
    • Field Summary

      Fields 
      Modifier and Type Field Description
      float[] _matrix  
    • Constructor Summary

      Constructors 
      Constructor Description
      Matrix()
      Constructor for an identity matrix.
      Matrix​(float[] source)
      Same as this(data, true).
      Matrix​(float[] source, boolean columnMajorOrder)
      Sets the matrix contents from the 16 entry source float array given in columnMajorOrder or row major order (if columnMajorOrder is false).
      Matrix​(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8, float m9, float m10, float m11, float m12, float m13, float m14, float m15)
      Same as this(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, true).
      Matrix​(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8, float m9, float m10, float m11, float m12, float m13, float m14, float m15, boolean columnMajorOrder)
      Same as set(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, columnMajorOrder).
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void apply​(Matrix matrix)
      Multiply this matrix by matrix.
      Matrix copy()
      Returns a copy of this matrix.
      float determinant()  
      float[] get​(float[] target)
      Same as return get(target, true).
      float[] get​(float[] target, boolean columnMajorOrder)
      Copies the matrix contents into a 16 entry target float array.
      static Matrix hudProjection​(int width, int height)
      Returns a hud (heads-up-display) projection matrix according to the window width and height dimensions.
      static Matrix hudView​(int width, int height)
      Returns a hud (heads-up-display) view according to the window width and height dimensions.
      static Matrix inverse​(Matrix matrix)
      Returns the inverse of matrix.
      boolean invert()
      Invert this matrix.
      boolean invert​(Matrix matrix)
      Invert this matrix into matrix, i.e., doesn't modify this matrix.
      float m00()
      Sets the 1st-row, 1st-column matrix entry.
      float m01()
      Returns the 1st-row, 2nd-column matrix entry.
      float m02()
      Returns the 1st-row, 3rd-column matrix entry.
      float m03()
      Returns the 1st-row, 4th-column matrix entry.
      float m10()
      Returns the 2nd-row, 1st-column matrix entry.
      float m11()
      Returns the 2nd-row, 2nd-column matrix entry.
      float m12()
      Returns the 2nd-row, 3rd-column matrix entry.
      float m13()
      Returns the 2nd-row, 4th-column matrix entry.
      float m20()
      Returns the 3rd-row, 1st-column matrix entry.
      float m21()
      Returns the 3rd-row, 2nd-column matrix entry.
      float m22()
      Returns the 3rd-row, 3rd-column matrix entry.
      float m23()
      Returns the 3rd-row, 4th-column matrix entry.
      float m30()
      Returns the 4th-row, 1st-column matrix entry.
      float m31()
      Returns the 4th-row, 2nd-column matrix entry.
      float m32()
      Returns the 4th-row, 3rd-column matrix entry.
      float m33()
      Returns the 4th-row, 4th-column matrix entry.
      boolean matches​(Matrix matrix)
      Returns whether or not this matrix matches other.
      float[] multiply​(float[] source, float[] target)
      Multiply a three or four element vector source against this matrix and store the result in target.
      static Matrix multiply​(Matrix a, Matrix b)
      Returns a x b.
      static void multiply​(Matrix a, Matrix b, Matrix c)
      Define c as a x b.
      Vector multiply​(Vector source)
      Same as return multiply(source, null).
      Vector multiply​(Vector source, Vector target)
      Multiply this matrix by the source vector and stores the result in the target vector which is then returned.
      static Matrix orthographic​(float width, float height, float zNear, float zFar)
      Returns an orthographic projection matrix from the given parameters.
      static Matrix perspective​(float magnitude, float aspectRatio, float zNear, float zFar)
      Returns a perspective projection matrix from the given parameters.
      void reset()
      Sets the identity matrix.
      void rotate​(float angle)
      Same as _rotateZ(angle).
      void rotate​(float angle, float v0, float v1, float v2)
      Multiply this matrix by the rotation matrix defined from the (v0,v1,v2) and angle.
      void rotateX​(float angle)
      Multiply this matrix by the rotation around x-axis matrix defined from angle.
      void rotateY​(float angle)
      Multiply this matrix by the rotation around y-axis matrix defined from angle.
      void rotateZ​(float angle)
      Multiply this matrix by the rotation around z-axis matrix defined from angle.
      void scale​(float s)
      Multiply this matrix by the scaling matrix defined from s.
      void scale​(float sx, float sy)
      Multiply this matrix by the scaling matrix defined from sx and sy.
      void scale​(float x, float y, float z)
      Multiply this matrix by the scaling matrix defined from x, y and z.
      void set​(float[] source)
      Same as set(source, true).
      void set​(float[] source, boolean columnMajorOrder)
      Sets the matrix contents from the 16 entry float source array.
      void set​(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8, float m9, float m10, float m11, float m12, float m13, float m14, float m15)  
      void set​(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8, float m9, float m10, float m11, float m12, float m13, float m14, float m15, boolean columnMajorOrder)
      Sets the column-major matrix contents from the given 16 consecutive values.
      void set​(Matrix matrix)
      Sets the matrix contents from the matrix contents.
      void setM00​(float value)
      Sets the 1st-row, 1st-column matrix entry.
      void setM01​(float value)
      Sets the 1st-row, 2nd-column matrix entry.
      void setM02​(float value)
      Sets the 1st-row, 3rd-column matrix entry.
      void setM03​(float value)
      Sets the 1st-row, 4th-column matrix entry.
      void setM10​(float value)
      Sets the 2nd-row, 1st-column matrix entry.
      void setM11​(float value)
      Sets the 2nd-row, 2nd-column matrix entry.
      void setM12​(float value)
      Sets the 2nd-row, 3rd-column matrix entry.
      void setM13​(float value)
      Sets the 2nd-row, 4th-column matrix entry.
      void setM20​(float value)
      Sets the 3rd-row, 1st-column matrix entry.
      void setM21​(float value)
      Sets the 3rd-row, 2nd-column matrix entry.
      void setM22​(float value)
      Sets the 3rd-row, 3rd-column matrix entry.
      void setM23​(float value)
      Sets the 3rd-row, 4th-column matrix entry.
      void setM30​(float value)
      Sets the 4th-row, 1st-column matrix entry.
      void setM31​(float value)
      Sets the 4th-row, 2nd-column matrix entry.
      void setM32​(float value)
      Sets the 4th-row, 3rd-column matrix entry.
      void setM33​(float value)
      Sets the 4th-row, 4th-column matrix entry.
      void shearX​(float angle)
      Multiply this matrix by the x-shearing matrix defined from angle.
      void shearY​(float angle)
      Multiply this matrix by the y-shearing matrix defined from angle.
      String toString()
      Return this matrix components as a String.
      void translate​(float tx, float ty)
      Same as translate(tx, ty, 0.
      void translate​(float tx, float ty, float tz)
      Multiply this matrix by the translation matrix defined from tx, ty and tz.
      void transpose()
      Transpose this matrix.
      static Matrix view​(Vector position, Quaternion orientation)
      Returns the inverse of the matrix associated with the eye position and orientation.
    • Field Detail

      • _matrix

        public float[] _matrix
    • Method Detail

      • matches

        public boolean matches​(Matrix matrix)
        Returns whether or not this matrix matches other.
        Parameters:
        matrix - other matrix
      • setM00

        public void setM00​(float value)
        Sets the 1st-row, 1st-column matrix entry.
      • setM01

        public void setM01​(float value)
        Sets the 1st-row, 2nd-column matrix entry.
      • setM02

        public void setM02​(float value)
        Sets the 1st-row, 3rd-column matrix entry.
      • setM03

        public void setM03​(float value)
        Sets the 1st-row, 4th-column matrix entry.
      • setM10

        public void setM10​(float value)
        Sets the 2nd-row, 1st-column matrix entry.
      • setM11

        public void setM11​(float value)
        Sets the 2nd-row, 2nd-column matrix entry.
      • setM12

        public void setM12​(float value)
        Sets the 2nd-row, 3rd-column matrix entry.
      • setM13

        public void setM13​(float value)
        Sets the 2nd-row, 4th-column matrix entry.
      • setM20

        public void setM20​(float value)
        Sets the 3rd-row, 1st-column matrix entry.
      • setM21

        public void setM21​(float value)
        Sets the 3rd-row, 2nd-column matrix entry.
      • setM22

        public void setM22​(float value)
        Sets the 3rd-row, 3rd-column matrix entry.
      • setM23

        public void setM23​(float value)
        Sets the 3rd-row, 4th-column matrix entry.
      • setM30

        public void setM30​(float value)
        Sets the 4th-row, 1st-column matrix entry.
      • setM31

        public void setM31​(float value)
        Sets the 4th-row, 2nd-column matrix entry.
      • setM32

        public void setM32​(float value)
        Sets the 4th-row, 3rd-column matrix entry.
      • setM33

        public void setM33​(float value)
        Sets the 4th-row, 4th-column matrix entry.
      • m00

        public float m00()
        Sets the 1st-row, 1st-column matrix entry.
      • m01

        public float m01()
        Returns the 1st-row, 2nd-column matrix entry.
      • m02

        public float m02()
        Returns the 1st-row, 3rd-column matrix entry.
      • m03

        public float m03()
        Returns the 1st-row, 4th-column matrix entry.
      • m10

        public float m10()
        Returns the 2nd-row, 1st-column matrix entry.
      • m11

        public float m11()
        Returns the 2nd-row, 2nd-column matrix entry.
      • m12

        public float m12()
        Returns the 2nd-row, 3rd-column matrix entry.
      • m13

        public float m13()
        Returns the 2nd-row, 4th-column matrix entry.
      • m20

        public float m20()
        Returns the 3rd-row, 1st-column matrix entry.
      • m21

        public float m21()
        Returns the 3rd-row, 2nd-column matrix entry.
      • m22

        public float m22()
        Returns the 3rd-row, 3rd-column matrix entry.
      • m23

        public float m23()
        Returns the 3rd-row, 4th-column matrix entry.
      • m30

        public float m30()
        Returns the 4th-row, 1st-column matrix entry.
      • m31

        public float m31()
        Returns the 4th-row, 2nd-column matrix entry.
      • m32

        public float m32()
        Returns the 4th-row, 3rd-column matrix entry.
      • m33

        public float m33()
        Returns the 4th-row, 4th-column matrix entry.
      • reset

        public void reset()
        Sets the identity matrix.
      • copy

        public Matrix copy()
        Returns a copy of this matrix.
      • get

        public float[] get​(float[] target)
        Same as return get(target, true).
        See Also:
        get(float[], boolean)
      • get

        public float[] get​(float[] target,
                           boolean columnMajorOrder)
        Copies the matrix contents into a 16 entry target float array. If target is null (or not the correct size), a new array will be created. Column or row major order is defined by the columnMajorOrder boolean parameter.
      • set

        public void set​(Matrix matrix)
        Sets the matrix contents from the matrix contents.
      • set

        public void set​(float[] source,
                        boolean columnMajorOrder)
        Sets the matrix contents from the 16 entry float source array. Column or row major order is defined by the columnMajorOrder boolean parameter.
      • set

        public void set​(float m0,
                        float m1,
                        float m2,
                        float m3,
                        float m4,
                        float m5,
                        float m6,
                        float m7,
                        float m8,
                        float m9,
                        float m10,
                        float m11,
                        float m12,
                        float m13,
                        float m14,
                        float m15)
      • set

        public void set​(float m0,
                        float m1,
                        float m2,
                        float m3,
                        float m4,
                        float m5,
                        float m6,
                        float m7,
                        float m8,
                        float m9,
                        float m10,
                        float m11,
                        float m12,
                        float m13,
                        float m14,
                        float m15,
                        boolean columnMajorOrder)
        Sets the column-major matrix contents from the given 16 consecutive values.
      • translate

        public void translate​(float tx,
                              float ty)
        Same as translate(tx, ty, 0.
      • translate

        public void translate​(float tx,
                              float ty,
                              float tz)
        Multiply this matrix by the translation matrix defined from tx, ty and tz.
      • rotate

        public void rotate​(float angle)
        Same as _rotateZ(angle).
        See Also:
        rotateZ(float)
      • rotateX

        public void rotateX​(float angle)
        Multiply this matrix by the rotation around x-axis matrix defined from angle.
      • rotateY

        public void rotateY​(float angle)
        Multiply this matrix by the rotation around y-axis matrix defined from angle.
      • rotateZ

        public void rotateZ​(float angle)
        Multiply this matrix by the rotation around z-axis matrix defined from angle.
      • rotate

        public void rotate​(float angle,
                           float v0,
                           float v1,
                           float v2)
        Multiply this matrix by the rotation matrix defined from the (v0,v1,v2) and angle.
      • scale

        public void scale​(float s)
        Multiply this matrix by the scaling matrix defined from s.
      • scale

        public void scale​(float sx,
                          float sy)
        Multiply this matrix by the scaling matrix defined from sx and sy.
      • scale

        public void scale​(float x,
                          float y,
                          float z)
        Multiply this matrix by the scaling matrix defined from x, y and z.
      • shearX

        public void shearX​(float angle)
        Multiply this matrix by the x-shearing matrix defined from angle.
      • shearY

        public void shearY​(float angle)
        Multiply this matrix by the y-shearing matrix defined from angle.
      • apply

        public void apply​(Matrix matrix)
        Multiply this matrix by matrix.
      • multiply

        public static void multiply​(Matrix a,
                                    Matrix b,
                                    Matrix c)
        Define c as a x b.
      • multiply

        public Vector multiply​(Vector source,
                               Vector target)
        Multiply this matrix by the source vector and stores the result in the target vector which is then returned.
      • multiply

        public float[] multiply​(float[] source,
                                float[] target)
        Multiply a three or four element vector source against this matrix and store the result in target. If target is null or not length 3 or 4, a new float array (length 3) will be returned.
      • transpose

        public void transpose()
        Transpose this matrix.
      • invert

        public boolean invert​(Matrix matrix)
        Invert this matrix into matrix, i.e., doesn't modify this matrix.

        matrix should be non-null.

      • inverse

        public static Matrix inverse​(Matrix matrix)
        Returns the inverse of matrix. Throws a runtime exception if matrix isn't invertible.
      • invert

        public boolean invert()
        Invert this matrix.
        Returns:
        true if successful
      • determinant

        public float determinant()
        Returns:
        the determinant of the matrix
      • toString

        public String toString()
        Return this matrix components as a String.
        Overrides:
        toString in class Object
      • perspective

        public static Matrix perspective​(float magnitude,
                                         float aspectRatio,
                                         float zNear,
                                         float zFar)
        Returns a perspective projection matrix from the given parameters.

        Compute the magnitude as tan(field-of-view / 2) if you want to set the matrix from the field-of-view.

        All parameter values should be positive, but magnitude which may be negative in case you want to invert the projected image across the eye y-axis.

        See Also:
        orthographic(float, float, float, float), view(Vector, Quaternion)
      • orthographic

        public static Matrix orthographic​(float width,
                                          float height,
                                          float zNear,
                                          float zFar)
        Returns an orthographic projection matrix from the given parameters.

        All parameter values should be positive, but height which may be negative in case you want to invert the projected image across the eye y-axis.

        See Also:
        perspective(float, float, float, float), view(Vector, Quaternion)