Class Matrix4f

java.lang.Object
de.arstwo.twotil.math.Matrix4f
All Implemented Interfaces:
Cloneable

public class Matrix4f extends Object implements Cloneable
A 3D Matrix implementation for Quaternions using floats.

Calculations are done in place, so no additional memory is allocated by operations.

This is compatible with OpenGL and other implementations, which use a column-major approach instead of the row-major that is intuitively used. This is only relevant if accessing the data directly. It's underlying FloatBuffer can therefore be directly inserted into OpenGL. You can use the constants below to access specific data points directly.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final FloatBuffer
     
    static final int
     
    protected final float[]
     
    protected static final Matrix4f
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
    Matrix constants for easier access: | xx xy xz xw | | yx yy yz yw | | zx zy zz zw | | wx wy wz ww | Note that OpenGL expects the matrix in format xy, yx, ..., zw, ww
    static final int
     
    static final int
     
    static final int
     
    static final int
    Matrix constants for easier access: | xx xy xz xw | | yx yy yz yw | | zx zy zz zw | | wx wy wz ww | Note that OpenGL expects the matrix in format xy, yx, ..., zw, ww
    static final int
     
    static final int
     
    static final int
     
    static final int
    Matrix constants for easier access: | xx xy xz xw | | yx yy yz yw | | zx zy zz zw | | wx wy wz ww | Note that OpenGL expects the matrix in format xy, yx, ..., zw, ww
    static final int
     
    static final int
     
    static final int
     
    static final int
    Matrix constants for easier access: | xx xy xz xw | | yx yy yz yw | | zx zy zz zw | | wx wy wz ww | Note that OpenGL expects the matrix in format xy, yx, ..., zw, ww
    static final int
     
    static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creats a new Matrix4f with all values setColumnMajor to 0.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Matrix4f other)
    Performs a matrix addition of this and other.
    Returns the internal float buffer for direct access.
     
    boolean
     
    boolean
    equals(Matrix4f other, float delta)
    Checks if this is equal to other within the given delta for precision
    boolean
     
    static Matrix4f
    Returns a new identity matrix.
    int
     
    Performs a matrix multiplication of this and other.
    rotate(float radians, float axisX, float axisY, float axisZ)
    Rotates this matrix around a given axis.
    rotate(float radians, Vector4f axis)
    Rotates this matrix around a given axis.
    set(Matrix4f other)
    Copy constructor.
    setColumnMajor(float[] data)
    Sets the data of this matrix to the given external data represented in that array.
    Sets this matrix to identity values.
    setLook(float positionX, float positionY, float positionZ, float rightX, float rightY, float rightZ, float upX, float upY, float upZ, float lookX, float lookY, float lookZ)
    Sets this matrix to a look matrix (an imaginary camera in 3D space looking into a specific direction) with the given float values.
    setLook(Vector4f position, Vector4f right, Vector4f up, Vector4f look)
    Sets this matrix to a look matrix (an imaginary camera in 3D space looking into a specific direction) with the given Vector4f values.
    Sets this matrix to be an orthogonal (2D) projection.
    setProjection(float fovY, float aspectRatio, float zNear, float zFar)
    Sets this matrix to be a projection matrix with the given values.
    setRotation(float radians, float axisX, float axisY, float axisZ)
    Sets this matrix to be a fixed rotation around a given axis.
    setRotation(float radians, Vector4f axis)
    Sets this matrix to be a fixed rotation around a given axis.
    setRowMajor(float[] data)
    Sets the data of this matrix to the given external data represented in that array, that is in a row-major format.
    setTranslate(float x, float y, float z)
    Sets the translation components of this matrix with the given values.
    Transposes this matrix (flips it diagonally).
    protected Matrix4f
    setUnchecked(float[] data)
    Internal function to setColumnMajor the data of this matrix without checking it.
    Sets the content of this matrix to all 0.
    sub(Matrix4f other)
    Performs a matrix subtraction of this and other.
     
    translate(float x, float y, float z)
    Translates this matrix by the given values.
    Translates this matrix by the given other matrix.
    translate(Vector4f direction)
    Translates this matrix by the given Vector4f.
    translateX(float x)
    Translates this matrix by the given x values.
    translateY(float y)
    Translates this matrix by the given y values.
    translateZ(float z)
    Translates this matrix by the given z values.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • Matrix4f

      public Matrix4f()
      Creats a new Matrix4f with all values setColumnMajor to 0.
  • Method Details

    • getIdentityMatrix

      public static Matrix4f getIdentityMatrix()
      Returns a new identity matrix.
      Returns:
      a new identity matrix.
    • set

      public Matrix4f set(Matrix4f other)
      Copy constructor. Generates a new matrix with all values set to other.
      Parameters:
      other - any other Matrix4f
      Returns:
      a new Matrix4f with all values setColumnMajor to other.
    • setColumnMajor

      public Matrix4f setColumnMajor(float[] data)
      Sets the data of this matrix to the given external data represented in that array.

      The expected format is column-major (xx yx zx wx...)

      Parameters:
      data - any compatible float array that holds data flattened by column then by row.
      Returns:
    • setRowMajor

      public Matrix4f setRowMajor(float[] data)
      Sets the data of this matrix to the given external data represented in that array, that is in a row-major format.

      The internal format is column-major (xx yx zx wx...), while the input data is row-major (xx, xy, xz, xw, ...).

      Parameters:
      data - any compatible float array that holds data flattened by row then by column.
      Returns:
    • setZero

      public Matrix4f setZero()
      Sets the content of this matrix to all 0.
      Returns:
      this matrix, with all content set to 0.
    • setUnchecked

      protected Matrix4f setUnchecked(float[] data)
      Internal function to setColumnMajor the data of this matrix without checking it.
      Parameters:
      data - any array of exactly 16 floats.
      Returns:
      this matrix, with all data setColumnMajor to the new values.
    • setIdentity

      public Matrix4f setIdentity()
      Sets this matrix to identity values.
      Returns:
      this matrix.
    • setTranspose

      public Matrix4f setTranspose()
      Transposes this matrix (flips it diagonally).
      Returns:
      this matrix.
    • setOrthogonalProjection

      public Matrix4f setOrthogonalProjection()
      Sets this matrix to be an orthogonal (2D) projection.
      Returns:
      this matrix.
    • setProjection

      public Matrix4f setProjection(float fovY, float aspectRatio, float zNear, float zFar)
      Sets this matrix to be a projection matrix with the given values.
      Parameters:
      fovY - Field of view
      aspectRatio - the aspect ratio of the plane
      zNear - near clipping range
      zFar - far clipping range
      Returns:
      this matrix.
    • setLook

      public Matrix4f setLook(Vector4f position, Vector4f right, Vector4f up, Vector4f look)
      Sets this matrix to a look matrix (an imaginary camera in 3D space looking into a specific direction) with the given Vector4f values.
      Parameters:
      position - the position of the camera
      right - what is considered the direction 'right' of the camera
      up - what is considered the direction 'up' of the camera
      look - what is considered the direction that the camera is 'looking' at
      Returns:
      this matrix.
    • setLook

      public Matrix4f setLook(float positionX, float positionY, float positionZ, float rightX, float rightY, float rightZ, float upX, float upY, float upZ, float lookX, float lookY, float lookZ)
      Sets this matrix to a look matrix (an imaginary camera in 3D space looking into a specific direction) with the given float values.
      Returns:
      this matrix.
      See Also:
    • add

      public Matrix4f add(Matrix4f other)
      Performs a matrix addition of this and other.
      Parameters:
      other - any other Matrix4f
      Returns:
      this matrix.
    • sub

      public Matrix4f sub(Matrix4f other)
      Performs a matrix subtraction of this and other.
      Parameters:
      other - any other Matrix4f
      Returns:
      this matrix.
    • multiply

      public Matrix4f multiply(Matrix4f other)
      Performs a matrix multiplication of this and other.
      Parameters:
      other - any other Matrix4f
      Returns:
      this matrix.
    • setTranslate

      public Matrix4f setTranslate(float x, float y, float z)
      Sets the translation components of this matrix with the given values.

      Note that all other values are kept as they were. If you want a pure translation matrix you need to setIdentity first.

      Parameters:
      x - any float
      y - any float
      z - any float
      Returns:
      this matrix.
    • translate

      public Matrix4f translate(Matrix4f other)
      Translates this matrix by the given other matrix.
      Parameters:
      other - any other matrix
      Returns:
      this matrix.
    • translate

      public Matrix4f translate(float x, float y, float z)
      Translates this matrix by the given values.
      Parameters:
      x - any float
      y - any float
      z - any float
      Returns:
      this matrix.
    • translate

      public Matrix4f translate(Vector4f direction)
      Translates this matrix by the given Vector4f.
      Parameters:
      direction - any Vector4f interpreted as a direction
      Returns:
      this matrix.
    • translateX

      public Matrix4f translateX(float x)
      Translates this matrix by the given x values.
      Parameters:
      x - any float
      Returns:
      this matrix.
    • translateY

      public Matrix4f translateY(float y)
      Translates this matrix by the given y values.
      Parameters:
      y - any float
      Returns:
      this matrix.
    • translateZ

      public Matrix4f translateZ(float z)
      Translates this matrix by the given z values.
      Parameters:
      z - any float
      Returns:
      this matrix.
    • rotate

      public Matrix4f rotate(float radians, Vector4f axis)
      Rotates this matrix around a given axis.
      Parameters:
      radians - radians to rotate around the axis
      axis - the axis to rotate around
      Returns:
      this matrix.
    • rotate

      public Matrix4f rotate(float radians, float axisX, float axisY, float axisZ)
      Rotates this matrix around a given axis.
      Parameters:
      radians - radians to rotate around the axis
      axisX - the x part of the axis to rotate around
      axisY - the y part of the axis to rotate around
      axisZ - the z part of the axis to rotate around
      Returns:
      this matrix.
    • setRotation

      public Matrix4f setRotation(float radians, Vector4f axis)
      Sets this matrix to be a fixed rotation around a given axis.
      Parameters:
      radians - radians to rotate around the axis
      axis - the axis to rotate around
      Returns:
      this matrix
    • setRotation

      public Matrix4f setRotation(float radians, float axisX, float axisY, float axisZ)
      Sets this matrix to be a fixed rotation around a given axis.
      Parameters:
      radians - radians to rotate around the axis
      axisX - the x part of the axis to rotate around
      axisY - the y part of the axis to rotate around
      axisZ - the z part of the axis to rotate around
      Returns:
      this matrix
    • buffer

      public FloatBuffer buffer()
      Returns the internal float buffer for direct access.
      Returns:
      the internal float buffer for direct access.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • equals

      public boolean equals(Matrix4f other)
    • equals

      public boolean equals(Matrix4f other, float delta)
      Checks if this is equal to other within the given delta for precision
      Parameters:
      other - any other Matrix4f
      delta - acceptable distance between two floats to count as equal
      Returns:
      true if those are equal within the given delta, false otherwise
    • clone

      public Matrix4f clone()
      Overrides:
      clone in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object