Class Vector2f

java.lang.Object
net.risingworld.api.utils.Vector2f

public class Vector2f extends Object
A Vector which holds two float values: X and Y
It can be used to represent a two dimensional value, like a horizontal position or screen coordinates.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Vector2i
    A vector with values 1, 1
    static final Vector2f
    A unit vector along the X axis
    static final Vector2f
    A unit vector along the Y axis
    float
    The X value of this vector
    float
    The Y value of this vector
    static final Vector2f
    A vector with values 0, 0
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new Vector2f with default values 0, 0.
    Vector2f(float x, float y)
    Creates a new Vector2f with the provided values.
    Creates a new Vector2f and copies the x and y values from the provided vector.
  • Method Summary

    Modifier and Type
    Method
    Description
    addLocal(float addX, float addY)
    Adds the provided values to the values of this vector.
    Adds the values of the provided vector to the values of this vector.
    float
    Gets the angle between this vector and another vector.
    Please note: Both vectors need to be unit vectors (normalized).
    Creates a copy of this Vector.
    float
    distance(float x, float y)
    Gets the distance between this vector and the provided coordinates.
    float
    Gets the distance between this vector and another vector.
    float
    distanceSquared(float x, float y)
    Gets the squared distance between this vector and the provided coordinates.
    float
    Gets the squared distance between this vector and another Vector2f.
    divideLocal(float scalar)
     
     
    float
    dot(float x, float y)
    Gets the dot product of this vector with the provided coordinates.
    float
    dot(Vector2f other)
    Gets the dot product of this vector with the provided vector.
    boolean
    equals(float x, float y)
     
    boolean
    Gets whether or not this vector is equal to another object.
    float
    Returns the x value of this vector.
    float
    Returns the y value of this vector.
    int
    Gets a unique* hash code for this vector, based on its values.
    boolean
    isOrthogonal(float x, float y)
    Checks if this vector is orthogonal to another vector (i.e the angle between both vectors is ~ 90° degree).
    boolean
    Checks if this vector is orthogonal to another vector (i.e the angle between both vectors is ~ 90° degree).
    boolean
    Checks if this vector is a unit vector.
    float
    Gets the length of this vector.
    float
    Gets the squared length of this vector.
    multLocal(float scalar)
    Multiplies the values of this vector with the provided scalar.
    multLocal(float multX, float multY)
    Multiplies the values of this vector with the provided values.
    Multiplies the values of this vector with the values of the provided vector.
    Negates all values of this vector.
    Normalizes this vector (i.e turn it into a unit vector).
    set(float x, float y)
    Sets the x, y and z values of this vector to the provided values.
    set(Vector2f vector)
    Sets the x and y values of this vector to the values of the provided vector.
    setX(float x)
    Sets the x value of this vector.
    setY(float y)
    Sets the y value of this vector.
    subtractLocal(float subtractX, float subtractY)
    Subtracts the provided values from the values of this vector.
    Subtracts the values of the provided vector from the values of this vector.
    Gets a String representation of this vector, for example (4.0, 5.0)
    Sets all values to 0 (same set set(0, 0);).

    Methods inherited from class java.lang.Object

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

    • ZERO

      public static final Vector2f ZERO
      A vector with values 0, 0
    • ONE

      public static final Vector2i ONE
      A vector with values 1, 1
    • UNIT_X

      public static final Vector2f UNIT_X
      A unit vector along the X axis
    • UNIT_Y

      public static final Vector2f UNIT_Y
      A unit vector along the Y axis
    • x

      public float x
      The X value of this vector
    • y

      public float y
      The Y value of this vector
  • Constructor Details

    • Vector2f

      public Vector2f()
      Creates a new Vector2f with default values 0, 0.
    • Vector2f

      public Vector2f(float x, float y)
      Creates a new Vector2f with the provided values.
      Parameters:
      x - the x value of the vector.
      y - the y value of the vector.
    • Vector2f

      public Vector2f(Vector2f copy)
      Creates a new Vector2f and copies the x and y values from the provided vector.
      Parameters:
      copy - the Vector2f to copy.
  • Method Details

    • copy

      public Vector2f copy()
      Creates a copy of this Vector.
      Returns:
      a new instance with the same values of this Vector.
    • set

      public Vector2f set(Vector2f vector)
      Sets the x and y values of this vector to the values of the provided vector.
      Parameters:
      vector - the Vector2f to copy.
      Returns:
      this vector.
    • set

      public Vector2f set(float x, float y)
      Sets the x, y and z values of this vector to the provided values.
      Parameters:
      x - the x value.
      y - the y value.
      Returns:
      this vector.
    • setX

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

      public Vector2f setY(float y)
      Sets the y value of this vector.
      Parameters:
      y - the new y value.
      Returns:
      this vector.
    • getX

      public float getX()
      Returns the x value of this vector.
      Returns:
      the current x value.
    • getY

      public float getY()
      Returns the y value of this vector.
      Returns:
      the current y value.
    • dot

      public float dot(Vector2f other)
      Gets the dot product of this vector with the provided vector.
      Parameters:
      other - the vector to calculate the dot product with.
      Returns:
      the resulting dot product.
    • dot

      public float dot(float x, float y)
      Gets the dot product of this vector with the provided coordinates.
      Parameters:
      x - the x coordinate.
      y - the y coordinate.
      Returns:
      the resulting dot product.
    • length

      public float length()
      Gets the length of this vector.
      Returns:
      the length of this vector.
      See Also:
    • lengthSquared

      public float lengthSquared()
      Gets the squared length of this vector. Use this function to avoid calculating the squareroot.
      Returns:
      the squared length of this vector.
      See Also:
    • distance

      public float distance(Vector2f other)
      Gets the distance between this vector and another vector.
      Parameters:
      other - the other vector.
      Returns:
      the distance between this vector and another vector.
      See Also:
    • distance

      public float distance(float x, float y)
      Gets the distance between this vector and the provided coordinates.
      Parameters:
      x - the other x coordinate.
      y - the other y coordinate.
      Returns:
      the distance between this vector and the provided coordinates.
      See Also:
    • distanceSquared

      public float distanceSquared(Vector2f other)
      Gets the squared distance between this vector and another Vector2f. Use this function to avoid calculating the squareroot.
      Parameters:
      other - the other Vector2f.
      Returns:
      the squared distance between this vector and the provided Vector2f.
    • distanceSquared

      public float distanceSquared(float x, float y)
      Gets the squared distance between this vector and the provided coordinates. Use this function to avoid calculating the squareroot.
      Parameters:
      x - the other x coordinate.
      y - the other y coordinate.
      Returns:
      the squared distance between this vector and the provided coordinates.
    • addLocal

      public Vector2f addLocal(Vector2f other)
      Adds the values of the provided vector to the values of this vector.
      Parameters:
      other - the vector to add.
      Returns:
      this vector.
    • addLocal

      public Vector2f addLocal(float addX, float addY)
      Adds the provided values to the values of this vector.
      Parameters:
      addX - the x value to add.
      addY - the y value to add.
      Returns:
      this vector.
    • subtractLocal

      public Vector2f subtractLocal(Vector2f other)
      Subtracts the values of the provided vector from the values of this vector.
      Parameters:
      other - the vector to subtract.
      Returns:
      this vector.
    • subtractLocal

      public Vector2f subtractLocal(float subtractX, float subtractY)
      Subtracts the provided values from the values of this vector.
      Parameters:
      subtractX - the x value to subtract.
      subtractY - the y value to subtract.
      Returns:
      this vector.
    • multLocal

      public Vector2f multLocal(float scalar)
      Multiplies the values of this vector with the provided scalar.
      Parameters:
      scalar - the value to multiply this vector by.
      Returns:
      this vector.
    • multLocal

      public Vector2f multLocal(Vector2f other)
      Multiplies the values of this vector with the values of the provided vector.
      Parameters:
      other - the vector to multiply.
      Returns:
      this vector.
    • multLocal

      public Vector2f multLocal(float multX, float multY)
      Multiplies the values of this vector with the provided values.
      Parameters:
      multX - the x value to multiply the x coordinate of this vector by.
      multY - the y value to multiply the y coordinate of this vector by.
      Returns:
      this vector.
    • divideLocal

      public Vector2f divideLocal(float scalar)
    • divideLocal

      public Vector2f divideLocal(Vector2f other)
    • negateLocal

      public Vector2f negateLocal()
      Negates all values of this vector.
      Returns:
      this vector.
    • normalizeLocal

      public Vector2f normalizeLocal()
      Normalizes this vector (i.e turn it into a unit vector).
      Returns:
      this vector (normalized).
    • angleBetween

      public float angleBetween(Vector2f other)
      Gets the angle between this vector and another vector.
      Please note: Both vectors need to be unit vectors (normalized).
      Parameters:
      other - the other vector (unit vector).
      Returns:
      the angle in degrees.
    • zero

      public Vector2f zero()
      Sets all values to 0 (same set set(0, 0);).
      Returns:
      this vector.
    • isUnitVector

      public boolean isUnitVector()
      Checks if this vector is a unit vector.
      Returns:
      true if this is a unit vector, false if not.
    • isOrthogonal

      public boolean isOrthogonal(Vector2f other)
      Checks if this vector is orthogonal to another vector (i.e the angle between both vectors is ~ 90° degree).
      Parameters:
      other - the other vector.
      Returns:
      true if this vector is orthogonal to the other vector, false if not.
    • isOrthogonal

      public boolean isOrthogonal(float x, float y)
      Checks if this vector is orthogonal to another vector (i.e the angle between both vectors is ~ 90° degree).
      Parameters:
      x - the x coordinate of the other vector.
      y - the y coordinate of the other vector.
      Returns:
      true if this vector is orthogonal to the other vector, false if not.
    • equals

      public boolean equals(Object o)
      Gets whether or not this vector is equal to another object. This function returns true, if the other object is a Vector2f and if it has exactly the same x and y values.
      Overrides:
      equals in class Object
      Parameters:
      o - the object to compare to equality.
      Returns:
      true if the other object is a Vector2f and has the same values as this vector, false if not.
    • equals

      public boolean equals(float x, float y)
    • hashCode

      public int hashCode()
      Gets a unique* hash code for this vector, based on its values. If two vectors have the same x and y values, they will return the same hash code.

      * Please keep in mind that the uniqueness is not fully reliable. There is always a very small chance that collisions occur (i.e two completely different vectors return the same hash code). But usually the reliability is sufficient.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code for this vector.
    • toString

      public String toString()
      Gets a String representation of this vector, for example (4.0, 5.0)
      Overrides:
      toString in class Object
      Returns:
      a string representing the values of this vector.