Class VectorUtils

Object
VectorUtils

public class VectorUtils extends Object
Contains methods for manipulating Vector2fs.
Since:
1.7
Author:
LazyWizard
  • Method Details

    • getFacing

      public static float getFacing(Vector2f vector)
      Returns the facing (angle) of a Vector2f in degrees. Accurate to within ~0.29 degrees. If you need more accuracy, use getFacingStrict(Vector2f).
      Parameters:
      vector - The vector to get the facing of.
      Returns:
      The facing (angle) of vector in degrees, or 0 if the vector has no length.
      Since:
      1.7
      See Also:
    • getFacingStrict

      public static float getFacingStrict(Vector2f vector)
      Returns the facing (angle) of a Vector2f in degrees. This method uses the slower but more accurate behavior of pre-2.3 getFacing(Vector2f).
      Parameters:
      vector - The vector to get the facing of.
      Returns:
      The facing (angle) of vector in degrees, or 0 if the vector has no length.
      Since:
      2.3
    • getAngle

      public static float getAngle(Vector2f from, Vector2f to)
      Returns the angle between two Vector2fs in degrees. Accurate to within ~0.29 degrees. If you need more accuracy, use getAngleStrict(Vector2f, Vector2f).
      Parameters:
      from - The source Vector2f.
      to - The Vector2f to get the angle to.
      Returns:
      The angle pointing from from to to, in degrees.
      Since:
      1.7
      See Also:
    • getAngleStrict

      public static float getAngleStrict(Vector2f from, Vector2f to)
      Returns the angle between two Vector2fs in degrees. This method uses the slower but more accurate behavior of pre-2.3 getAngle(Vector2f, Vector2f).
      Parameters:
      from - The source Vector2f.
      to - The Vector2f to get the angle to.
      Returns:
      The angle pointing from from to to.
      Since:
      2.3
    • getDirectionalVector

      public static Vector2f getDirectionalVector(Vector2f source, Vector2f destination)
      Returns a normalized Vector2f pointing from source to destination.
      Parameters:
      source - The origin of the vector.
      destination - The location to point at.
      Returns:
      A normalized Vector2f pointing at destination.
      Since:
      1.8
    • getCrossProduct

      public static float getCrossProduct(Vector2f vector1, Vector2f vector2)
      Returns the cross product of two Vector2fs.
      Parameters:
      vector1 - The first Vector2f.
      vector2 - The second Vector2f.
      Returns:
      The cross product of the two vectors.
      Since:
      1.7
    • isZeroVector

      public static boolean isZeroVector(Vector2f vector)
      Tests whether a vector is a zero vector (coordinates of {0, 0}).
      Parameters:
      vector - The vector to test.
      Returns:
      true if both of vector's coordinates are 0, false otherwise.
      Since:
      2.3
    • resize

      public static Vector2f resize(Vector2f vector, float length, Vector2f dest)
      Scales a vector to the requested length and stores the result in a destination vector. Note: if the vector's length is 0, no scaling will be performed.
      Parameters:
      vector - The vector to be resized. Will not be modified; instead the result will be placed in dest.
      length - The new total length of the destination vector.
      dest - The destination Vector2f. Can be vector.
      Returns:
      dest, scaled based on vector, returned for easier chaining of methods.
      Since:
      2.3
    • resize

      public static Vector2f resize(Vector2f vector, float length)
      Scales a vector to the requested length. Note: if the vector's length is 0, no scaling will be performed.
      Parameters:
      vector - The vector to be modified.
      length - The new total length of vector.
      Returns:
      The modified vector, returned for easier chaining of methods.
      Since:
      2.3
      See Also:
    • clampLength

      public static Vector2f clampLength(Vector2f toClamp, float maxLength, Vector2f dest)
      Reduces a vector's length if it is higher than the passed in argument and stores the result in a destination vector.
      Parameters:
      toClamp - The vector whose length should be clamped. Will not be modified; instead the result will be placed in dest.
      maxLength - The maximum length of the destination vector. If its current length is higher, it will be reduced to this amount.
      dest - The destination Vector2f. Can be toClamp.
      Returns:
      dest, clamped based on toClamp, returned for easier chaining of methods.
      Since:
      2.3
    • clampLength

      public static Vector2f clampLength(Vector2f toClamp, float maxLength)
      Reduces a vector's length if it is higher than the passed in argument.
      Parameters:
      toClamp - The vector whose length should be clamped.
      maxLength - The maximum length of toClamp. If its current length is higher, it will be reduced to this amount.
      Returns:
      The modified toClamp, returned for easier chaining of methods.
      Since:
      2.3
      See Also:
    • clampLength

      public static Vector2f clampLength(Vector2f toClamp, float minLength, float maxLength, Vector2f dest)
      Ensures a vector's length is within the given parameters and stores the result in a destination vector.
      Parameters:
      toClamp - The vector whose length should be clamped. Will not be modified; instead the result will be placed in dest.
      minLength - The minimum length of the destination vector. If its current length is lower, it will be increased to this amount.
      maxLength - The maximum length of the destination vector. If its current length is higher, it will be reduced to this amount.
      dest - The destination Vector2f. Can be toClamp.
      Returns:
      dest, clamped based on toClamp, returned for easier chaining of methods.
      Since:
      2.3
    • clampLength

      public static Vector2f clampLength(Vector2f toClamp, float minLength, float maxLength)
      Ensures a vector's length is within the given parameters and stores the result in a destination vector.
      Parameters:
      toClamp - The vector whose length should be clamped. Will not be modified; instead the result will be placed in dest.
      minLength - The minimum length of toClamp. If its current length is lower, it will be increased to this amount.
      maxLength - The maximum length of toClamp. If its current length is higher, it will be reduced to this amount.
      Returns:
      The modified toClamp, returned for easier chaining of methods.
      Since:
      2.3
      See Also:
    • rotate

      public static Vector2f rotate(Vector2f toRotate, float angle, Vector2f dest)
      Rotates a Vector2f by a specified amount and stores the result in a destination vector.
      Parameters:
      toRotate - The Vector2f to rotate. Will not be modified; instead the result will be placed in dest.
      angle - How much to rotate the destination vector, in degrees.
      dest - The destination Vector2f. Can be toRotate.
      Returns:
      dest, rotated based on toRotate, returned for easier chaining of methods.
      Since:
      1.7
    • rotate

      public static Vector2f rotate(Vector2f toRotate, float angle)
      Rotates a Vector2f by a specified amount.
      Parameters:
      toRotate - The Vector2f to rotate.
      angle - How much to rotate toRotate, in degrees.
      Returns:
      The modified toRotate, returned for easier chaining of methods.
      Since:
      2.3
      See Also:
    • rotate

      public static List<Vector2f> rotate(List<Vector2f> toRotate, float angle)
      Rotates a List of Vector2fs by a specified amount. Much more efficient than rotating each point individually.
      Parameters:
      toRotate - The List of Vector2fs to rotate.
      angle - How much to rotate toRotate, in degrees.
      Returns:
      A List of Vector2fs that have been rotated.
      Since:
      2.0
    • rotateAroundPivot

      public static Vector2f rotateAroundPivot(Vector2f toRotate, Vector2f pivotPoint, float angle, Vector2f dest)
      Rotates a Vector2f by a specified amount around a pivot point and stores the result in a destination vector.
      Parameters:
      toRotate - The Vector2f to rotate. Will not be modified; instead the result will be placed in dest.
      pivotPoint - The central point to pivot around.
      angle - How much to rotate the destination vector, in degrees.
      dest - The destination Vector2f. Can be toRotate.
      Returns:
      dest, rotated based on toRotate around pivotPoint, returned for easier chaining of methods.
      Since:
      1.7
    • rotateAroundPivot

      public static Vector2f rotateAroundPivot(Vector2f toRotate, Vector2f pivotPoint, float angle)
      Rotates a Vector2f by a specified amount around a pivot point.
      Parameters:
      toRotate - The Vector2f to rotate.
      pivotPoint - The central point to pivot around.
      angle - How much to rotate toRotate, in degrees.
      Returns:
      The modified toRotate, returned for easier chaining of methods.
      Since:
      2.3
      See Also:
    • rotateAroundPivot

      public static List<Vector2f> rotateAroundPivot(List<Vector2f> toRotate, Vector2f pivotPoint, float angle)
      Rotates a List of Vector2fs by a specified amount around a pivot point. Much more efficient than rotating each point individually.
      Parameters:
      toRotate - The List of Vector2fs to rotate.
      pivotPoint - The central point to pivot around.
      angle - How much to rotate toRotate, in degrees.
      Returns:
      A List of Vector2fs that have been rotated around pivotPoint.
      Since:
      2.0
    • toVectorList

      public static List<Vector2f> toVectorList(float[] coordinates)
      Converts a float array of vertices into a List of Vector2fs.
      Parameters:
      coordinates - An array of floats representing a list of x and y coordinates. The length of coordinates must be even or this method will throw a RuntimeException!.
      Returns:
      A List of Vector2fs that contains all coordinates passed in.
      Since:
      3.0
    • toFloatArray

      public static float[] toFloatArray(List<Vector2f> vectors)
      Converts a List of Vector2fs into a float array of vertices for use with FloatBuffers in OpenGL.
      Parameters:
      vectors - A List of Vector2fs to convert into a float array.
      Returns:
      A float array containing the coordinates contained in vectors.
      Since:
      3.0