Class Vector4f

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

public class Vector4f extends Object implements Cloneable
A 3D Vector implementation (Quaternion) using floats.

Describing this as a Quaternion allows for faster transformation in 3D space using an appropriate Matrix4f. A vector can either be a direction or a position (point in space). A direction has by definition no origin, while a position is relative to the origin at (0,0,0).

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

Uses a FloatBuffer which is compatible with OpenGL and similar implementations.

  • Field Summary

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

    Constructors
    Constructor
    Description
    Create a new empty vector with all values set to 0.
    Vector4f(float x, float y, float z, boolean isPosition)
    Creates a new vector with the given values.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(float x, float y, float z)
    Adds the given values to this vector.
    add(Vector4f other)
    Adds the given vector to this vector.
    addX(float x)
    Adds a specific value to the x component of this vector.
    addY(float y)
    Adds a specific value to the y component of this vector.
    addZ(float z)
    Adds a specific value to the z component of this vector.
    Returns the underlying FloatBuffer for direct access.
     
    cross(Vector4f other)
    Calculates the cross product between this vector and another.
    float
    dot(Vector4f other)
    Calculates the dot product between this vector and another.
    boolean
     
    boolean
     
    float
    Returns the X component of this vector.
    float
    Returns the Y component of this vector.
    float
    Returns the Z component of this vector.
    int
     
    boolean
    Checks if this vector is a directional vector.
    boolean
    Checks if this vector is a position (point in space) vector.
    float
    Returns the length of this vector.
    float
    Returns the squared length of this vector.
    Normalizes this vector to a length of 1.
    scale(float magnitude)
    Scales this vector by the given magnitude.
    set(float[] data)
    Sets all values of this vector to those of a given float array.
    set(float x, float y, float z)
    Sets the vector to the given values without changing the W value.
    set(Vector4f other)
    Copy constructor.
    Sets this vector to be a directional vector.
    Sets this vector to be a position (point in space) vector.
    protected Vector4f
    setUnchecked(float[] data)
    Internal function to set the data of this vector without checking it.
    setX(float x)
    Sets the x value of this vector.
    setY(float y)
    Sets the y value of this vector.
    setZ(float z)
    Sets the z value of this vector.
    Sets the x,y,z content of this vector to all zero.
    Sets the x,y,z content of this vector to all zero and turns it into a directional vector.
    Sets the x,y,z content of this vector to all zero and turns it into a position (point in space).
    shear(float x, float y, float z)
    Shears this vector with the given vector components.
    shearX(float x)
    Shears this vector on the x-axis only.
    shearY(float y)
    Shears this vector on the y-axis only.
    shearZ(float z)
    Shears this vector on the z-axis only.
    sub(float x, float y, float z)
    Subtracts the given values to this vector.
    sub(Vector4f other)
    Subtracts the given vector to this vector.
    subX(float x)
    Subtracts a specific value to the x component of this vector.
    subY(float y)
    Subtracts a specific value to the y component of this vector.
    subZ(float z)
    Subtracts a specific value to the z component of this vector.
     
    Transforms this vector using the given matrix.
    Translates this vector using the given matrix.

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • Vector4f

      public Vector4f()
      Create a new empty vector with all values set to 0.
    • Vector4f

      public Vector4f(float x, float y, float z, boolean isPosition)
      Creates a new vector with the given values.
      Parameters:
      x - any float
      y - any float
      z - any float
      isPosition - determines if the vector is a position (point in space) or direction.
  • Method Details

    • set

      public Vector4f set(Vector4f other)
      Copy constructor. Creates a new Vector4f with all values set to other.
      Parameters:
      other - any other Vector4f
      Returns:
      a new Vector4f with the same values as other.
    • set

      public Vector4f set(float x, float y, float z)
      Sets the vector to the given values without changing the W value.
      Parameters:
      x - any float
      y - any float
      z - any float
      Returns:
      this vector.
    • setX

      public Vector4f setX(float x)
      Sets the x value of this vector.
      Parameters:
      x - any float
      Returns:
      this vector.
    • setY

      public Vector4f setY(float y)
      Sets the y value of this vector.
      Parameters:
      y - any float
      Returns:
      this vector.
    • setZ

      public Vector4f setZ(float z)
      Sets the z value of this vector.
      Parameters:
      z - any float
      Returns:
      this vector.
    • set

      public Vector4f set(float[] data)
      Sets all values of this vector to those of a given float array.

      This can be used to copy external data into a Vector4f.

      Parameters:
      data - any array of exactly 4 floats.
      Returns:
      this vector, with all data set to the external values.
    • setUnchecked

      protected Vector4f setUnchecked(float[] data)
      Internal function to set the data of this vector without checking it.
      Parameters:
      data - any array of exactly 4 floats.
      Returns:
      this vector, with all data set to the new values.
    • setZero

      public Vector4f setZero()
      Sets the x,y,z content of this vector to all zero.
      Returns:
      this vector.
    • setZeroPosition

      public Vector4f setZeroPosition()
      Sets the x,y,z content of this vector to all zero and turns it into a position (point in space).
      Returns:
      this vector.
    • setZeroDirection

      public Vector4f setZeroDirection()
      Sets the x,y,z content of this vector to all zero and turns it into a directional vector.
      Returns:
      this vector.
    • isDirection

      public boolean isDirection()
      Checks if this vector is a directional vector.
      Returns:
      true if it is a directional vector, false otherwise.
    • isPosition

      public boolean isPosition()
      Checks if this vector is a position (point in space) vector.
      Returns:
      true if it is a position vector, false otherwise.
    • setIsDirection

      public Vector4f setIsDirection()
      Sets this vector to be a directional vector.
      Returns:
      this vector.
    • setIsPosition

      public Vector4f setIsPosition()
      Sets this vector to be a position (point in space) vector.
      Returns:
      this vector.
    • lengthSquared

      public float lengthSquared()
      Returns the squared length of this vector.
      Returns:
      the squared length of this vector.
    • length

      public float length()
      Returns the length of this vector.
      Returns:
      the length of this vector.
    • normalize

      public Vector4f normalize()
      Normalizes this vector to a length of 1.
      Returns:
      this vector.
    • add

      public Vector4f add(float x, float y, float z)
      Adds the given values to this vector.
      Parameters:
      x - any float
      y - any float
      z - any float
      Returns:
      this vector.
    • add

      public Vector4f add(Vector4f other)
      Adds the given vector to this vector.
      Parameters:
      other - any other Vector4f.
      Returns:
      this vector.
    • addX

      public Vector4f addX(float x)
      Adds a specific value to the x component of this vector.
      Parameters:
      x - any float
      Returns:
      this vector.
    • addY

      public Vector4f addY(float y)
      Adds a specific value to the y component of this vector.
      Parameters:
      y - any float
      Returns:
      this vector.
    • addZ

      public Vector4f addZ(float z)
      Adds a specific value to the z component of this vector.
      Parameters:
      z - any float
      Returns:
      this vector.
    • sub

      public Vector4f sub(float x, float y, float z)
      Subtracts the given values to this vector.
      Parameters:
      x - any float
      y - any float
      z - any float
      Returns:
      this vector.
    • sub

      public Vector4f sub(Vector4f other)
      Subtracts the given vector to this vector.
      Parameters:
      other - any other Vector4f.
      Returns:
      this vector.
    • subX

      public Vector4f subX(float x)
      Subtracts a specific value to the x component of this vector.
      Parameters:
      x - any float
      Returns:
      this vector.
    • subY

      public Vector4f subY(float y)
      Subtracts a specific value to the y component of this vector.
      Parameters:
      y - any float
      Returns:
      this vector.
    • subZ

      public Vector4f subZ(float z)
      Subtracts a specific value to the z component of this vector.
      Parameters:
      z - any float
      Returns:
      this vector.
    • shear

      public Vector4f shear(float x, float y, float z)
      Shears this vector with the given vector components.
      Parameters:
      x - any float
      y - any float
      z - any float
      Returns:
      this vector.
    • shearX

      public Vector4f shearX(float x)
      Shears this vector on the x-axis only.
      Parameters:
      x - any float.
      Returns:
      this vector.
    • shearY

      public Vector4f shearY(float y)
      Shears this vector on the y-axis only.
      Parameters:
      y - any float.
      Returns:
      this vector.
    • shearZ

      public Vector4f shearZ(float z)
      Shears this vector on the z-axis only.
      Parameters:
      z - any float.
      Returns:
      this vector.
    • scale

      public Vector4f scale(float magnitude)
      Scales this vector by the given magnitude.
      Parameters:
      magnitude - any float.
      Returns:
      this vector.
    • dot

      public float dot(Vector4f other)
      Calculates the dot product between this vector and another.
      Parameters:
      other - any other Vector4f
      Returns:
      this vector.
    • cross

      public Vector4f cross(Vector4f other)
      Calculates the cross product between this vector and another.
      Parameters:
      other - any other Vector4f
      Returns:
      this vector.
    • transform

      public Vector4f transform(Matrix4f matrix)
      Transforms this vector using the given matrix.
      Parameters:
      matrix - any Matrix4f
      Returns:
      this vector.
    • translate

      public Vector4f translate(Matrix4f matrix)
      Translates this vector using the given matrix.

      Note that this has no effect if this vector is a directional vector.

      Parameters:
      matrix - any Matrix4f
      Returns:
      this vector.
    • buffer

      public FloatBuffer buffer()
      Returns the underlying FloatBuffer for direct access.
      Returns:
      the underlying FloatBuffer.
    • getX

      public float getX()
      Returns the X component of this vector.
      Returns:
      the X component of this vector.
    • getY

      public float getY()
      Returns the Y component of this vector.
      Returns:
      the Y component of this vector.
    • getZ

      public float getZ()
      Returns the Z component of this vector.
      Returns:
      the Z component of this vector.
    • hashCode

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

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

      public boolean equals(Vector4f other)
    • clone

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

      public String toString()
      Overrides:
      toString in class Object