Package arc.math

Class Mat

java.lang.Object
arc.math.Mat

public class Mat extends Object
A 3x3 column major matrix; useful for 2D transforms.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    float[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Mat()
     
    Mat(float[] values)
    Constructs a matrix from the given float array.
    Mat(Mat matrix)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    float
    det()
     
    float
     
    float
     
    getScale(Vec2 scale)
     
    getTranslation(Vec2 position)
     
    float[]
    Get the values in this matrix.
    idt()
    Sets this matrix to the identity matrix
    inv()
    Inverts this matrix given that the determinant is != 0.
    mul(Mat m)
    Postmultiplies this matrix with the provided matrix and stores the result in this matrix.
    Premultiplies this matrix with the provided matrix and stores the result in this matrix.
    rotate(float degrees)
    Postmultiplies this matrix with a (counter-clockwise) rotation matrix.
    rotateRad(float radians)
    Postmultiplies this matrix with a (counter-clockwise) rotation matrix.
    scale(float scaleX, float scaleY)
    Postmultiplies this matrix with a scale matrix.
    scale(Vec2 scale)
    Postmultiplies this matrix with a scale matrix.
    scl(float scale)
    Scale the matrix in the both the x and y components by the scalar value.
    scl(Vec2 scale)
    Scale this matrix using the x and y components of the vector but leave the rest of the matrix alone.
    scl(Vec3 scale)
    Scale this matrix using the x and y components of the vector but leave the rest of the matrix alone.
    set(float[] values)
    Sets the matrix to the given matrix as a float array.
    set(Affine2 affine)
    Copies the values from the provided affine matrix to this matrix.
    set(Mat mat)
    Copies the values from the provided matrix to this matrix.
    setOrtho(float x, float y, float width, float height)
    Sets this matrix to an orthographic projection.
    setToRotation(float degrees)
    Sets this matrix to a rotation matrix that will rotate any vector in counter-clockwise direction around the z-axis.
    setToRotation(Vec3 axis, float degrees)
     
    setToRotation(Vec3 axis, float cos, float sin)
     
    setToRotationRad(float radians)
    Sets this matrix to a rotation matrix that will rotate any vector in counter-clockwise direction around the z-axis.
    setToScaling(float scaleX, float scaleY)
    Sets this matrix to a scaling matrix.
    Sets this matrix to a scaling matrix.
    setToTranslation(float x, float y)
    Sets this matrix to a translation matrix.
    setToTranslation(Vec2 translation)
    Sets this matrix to a translation matrix.
     
    translate(float x, float y)
    Postmultiplies this matrix by a translation matrix.
    translate(Vec2 translation)
    Postmultiplies this matrix by a translation matrix.
    Transposes the current matrix.
    trn(float x, float y)
    Adds a translational component to the matrix in the 3rd column.
    trn(Vec2 vector)
    Adds a translational component to the matrix in the 3rd column.
    trn(Vec3 vector)
    Adds a translational component to the matrix in the 3rd column.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • Mat

      public Mat()
    • Mat

      public Mat(Mat matrix)
    • Mat

      public Mat(float[] values)
      Constructs a matrix from the given float array. The array must have at least 9 elements; the first 9 will be copied.
      Parameters:
      values - The float array to copy. Remember that this matrix is in column major order. (The float array is not modified.)
  • Method Details

    • setOrtho

      public Mat setOrtho(float x, float y, float width, float height)
      Sets this matrix to an orthographic projection.
    • idt

      public Mat idt()
      Sets this matrix to the identity matrix
      Returns:
      This matrix for the purpose of chaining operations.
    • mul

      public Mat mul(Mat m)
      Postmultiplies this matrix with the provided matrix and stores the result in this matrix. For example:
       A.mul(B) results in A := AB
       
      Parameters:
      m - Matrix to multiply by.
      Returns:
      This matrix for the purpose of chaining operations together.
    • mulLeft

      public Mat mulLeft(Mat m)
      Premultiplies this matrix with the provided matrix and stores the result in this matrix. For example:
       A.mulLeft(B) results in A := BA
       
      Parameters:
      m - The other Matrix to multiply by
      Returns:
      This matrix for the purpose of chaining operations.
    • setToRotation

      public Mat setToRotation(float degrees)
      Sets this matrix to a rotation matrix that will rotate any vector in counter-clockwise direction around the z-axis.
      Parameters:
      degrees - the angle in degrees.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToRotationRad

      public Mat setToRotationRad(float radians)
      Sets this matrix to a rotation matrix that will rotate any vector in counter-clockwise direction around the z-axis.
      Parameters:
      radians - the angle in radians.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToRotation

      public Mat setToRotation(Vec3 axis, float degrees)
    • setToRotation

      public Mat setToRotation(Vec3 axis, float cos, float sin)
    • setToTranslation

      public Mat setToTranslation(float x, float y)
      Sets this matrix to a translation matrix.
      Parameters:
      x - the translation in x
      y - the translation in y
      Returns:
      This matrix for the purpose of chaining operations.
    • setToTranslation

      public Mat setToTranslation(Vec2 translation)
      Sets this matrix to a translation matrix.
      Parameters:
      translation - The translation vector.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToScaling

      public Mat setToScaling(float scaleX, float scaleY)
      Sets this matrix to a scaling matrix.
      Parameters:
      scaleX - the scale in x
      scaleY - the scale in y
      Returns:
      This matrix for the purpose of chaining operations.
    • setToScaling

      public Mat setToScaling(Vec2 scale)
      Sets this matrix to a scaling matrix.
      Parameters:
      scale - The scale vector.
      Returns:
      This matrix for the purpose of chaining operations.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • det

      public float det()
      Returns:
      The determinant of this matrix
    • inv

      public Mat inv()
      Inverts this matrix given that the determinant is != 0.
      Returns:
      This matrix for the purpose of chaining operations.
      Throws:
      ArcRuntimeException - if the matrix is singular (not invertible)
    • set

      public Mat set(Mat mat)
      Copies the values from the provided matrix to this matrix.
      Parameters:
      mat - The matrix to copy.
      Returns:
      This matrix for the purposes of chaining.
    • set

      public Mat set(Affine2 affine)
      Copies the values from the provided affine matrix to this matrix. The last row is set to (0, 0, 1).
      Parameters:
      affine - The affine matrix to copy.
      Returns:
      This matrix for the purposes of chaining.
    • set

      public Mat set(float[] values)
      Sets the matrix to the given matrix as a float array. The float array must have at least 9 elements; the first 9 will be copied.
      Parameters:
      values - The matrix, in float form, that is to be copied. Remember that this matrix is in column major order.
      Returns:
      This matrix for the purpose of chaining methods together.
    • trn

      public Mat trn(Vec2 vector)
      Adds a translational component to the matrix in the 3rd column. The other columns are untouched.
      Parameters:
      vector - The translation vector.
      Returns:
      This matrix for the purpose of chaining.
    • trn

      public Mat trn(float x, float y)
      Adds a translational component to the matrix in the 3rd column. The other columns are untouched.
      Parameters:
      x - The x-component of the translation vector.
      y - The y-component of the translation vector.
      Returns:
      This matrix for the purpose of chaining.
    • trn

      public Mat trn(Vec3 vector)
      Adds a translational component to the matrix in the 3rd column. The other columns are untouched.
      Parameters:
      vector - The translation vector. (The z-component of the vector is ignored because this is a 3x3 matrix)
      Returns:
      This matrix for the purpose of chaining.
    • translate

      public Mat translate(float x, float y)
      Postmultiplies this matrix by a translation matrix. Postmultiplication is also used by OpenGL ES' 1.x glTranslate/glRotate/glScale.
      Parameters:
      x - The x-component of the translation vector.
      y - The y-component of the translation vector.
      Returns:
      This matrix for the purpose of chaining.
    • translate

      public Mat translate(Vec2 translation)
      Postmultiplies this matrix by a translation matrix. Postmultiplication is also used by OpenGL ES' 1.x glTranslate/glRotate/glScale.
      Parameters:
      translation - The translation vector.
      Returns:
      This matrix for the purpose of chaining.
    • rotate

      public Mat rotate(float degrees)
      Postmultiplies this matrix with a (counter-clockwise) rotation matrix. Postmultiplication is also used by OpenGL ES' 1.x glTranslate/glRotate/glScale.
      Parameters:
      degrees - The angle in degrees
      Returns:
      This matrix for the purpose of chaining.
    • rotateRad

      public Mat rotateRad(float radians)
      Postmultiplies this matrix with a (counter-clockwise) rotation matrix. Postmultiplication is also used by OpenGL ES' 1.x glTranslate/glRotate/glScale.
      Parameters:
      radians - The angle in radians
      Returns:
      This matrix for the purpose of chaining.
    • scale

      public Mat scale(float scaleX, float scaleY)
      Postmultiplies this matrix with a scale matrix. Postmultiplication is also used by OpenGL ES' 1.x glTranslate/glRotate/glScale.
      Parameters:
      scaleX - The scale in the x-axis.
      scaleY - The scale in the y-axis.
      Returns:
      This matrix for the purpose of chaining.
    • scale

      public Mat scale(Vec2 scale)
      Postmultiplies this matrix with a scale matrix. Postmultiplication is also used by OpenGL ES' 1.x glTranslate/glRotate/glScale.
      Parameters:
      scale - The vector to scale the matrix by.
      Returns:
      This matrix for the purpose of chaining.
    • getValues

      public float[] getValues()
      Get the values in this matrix.
      Returns:
      The float values that make up this matrix in column-major order.
    • getTranslation

      public Vec2 getTranslation(Vec2 position)
    • getScale

      public Vec2 getScale(Vec2 scale)
    • getRotation

      public float getRotation()
    • getRotationRad

      public float getRotationRad()
    • scl

      public Mat scl(float scale)
      Scale the matrix in the both the x and y components by the scalar value.
      Parameters:
      scale - The single value that will be used to scale both the x and y components.
      Returns:
      This matrix for the purpose of chaining methods together.
    • scl

      public Mat scl(Vec2 scale)
      Scale this matrix using the x and y components of the vector but leave the rest of the matrix alone.
      Parameters:
      scale - The Vec3 to use to scale this matrix.
      Returns:
      This matrix for the purpose of chaining methods together.
    • scl

      public Mat scl(Vec3 scale)
      Scale this matrix using the x and y components of the vector but leave the rest of the matrix alone.
      Parameters:
      scale - The Vec3 to use to scale this matrix. The z component will be ignored.
      Returns:
      This matrix for the purpose of chaining methods together.
    • transpose

      public Mat transpose()
      Transposes the current matrix.
      Returns:
      This matrix for the purpose of chaining methods together.