Package arc.math.geom

Class Vec3

java.lang.Object
arc.math.geom.Vec3
All Implemented Interfaces:
Vector<Vec3>

public class Vec3 extends Object implements Vector<Vec3>
Encapsulates a 3D vector. Allows chaining operations by returning a reference to itself in all modification methods.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    float
    the x-component of this vector
    static final Vec3
     
    float
    the y-component of this vector
    static final Vec3
     
    float
    the z-component of this vector
    static final Vec3
     
    static final Vec3
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a vector at (0,0,0)
    Vec3(double x, double y, double z)
     
    Vec3(float[] values)
    Creates a vector from the given array.
    Vec3(float x, float y, float z)
    Creates a vector with the given components
    Vec3(Vec2 vector, float z)
    Creates a vector from the given vector and z-component
    Vec3(Vec3 vector)
    Creates a vector from the given vector
  • Method Summary

    Modifier and Type
    Method
    Description
    add(float values)
    Adds the given value to all three components of the vector.
    add(float x, float y, float z)
    Adds the given vector to this component
    add(Vec3 vector)
    Adds the given vector to this vector
    add(Vec3 vector, float scale)
     
    float
    angle(Vec3 vector)
     
    float
    angleRad(Vec3 vector)
     
    clamp(float min, float max)
    Clamps this vector's length to given min and max values
    cpy()
     
    cpy(Vec3 dest)
     
    crs(float x, float y, float z)
    Sets this vector to the cross product between it and the other vector.
    crs(Vec3 vector)
    Sets this vector to the cross product between it and the other vector.
    div(Vec3 other)
    Inverse of scl()
    float
    dot(float x, float y, float z)
    Returns the dot product between this and the given vector.
    static float
    dot(float x1, float y1, float z1, float x2, float y2, float z2)
     
    float
    dot(Vec3 vector)
     
    float
    dst(float x, float y, float z)
     
    static float
    dst(float x1, float y1, float z1, float x2, float y2, float z2)
     
    float
    dst(Vec3 vector)
     
    float
    dst2(float x, float y, float z)
    Returns the squared distance between this point and the given point
    static float
    dst2(float x1, float y1, float z1, float x2, float y2, float z2)
     
    float
    dst2(Vec3 point)
    This method is faster than Vector.dst(Vector) because it avoids calculating a square root.
    boolean
    epsilonEquals(float x, float y, float z)
    Compares this vector with the other vector using Mathf.FLOAT_ROUNDING_ERROR for fuzzy equality testing
    boolean
    epsilonEquals(float x, float y, float z, float epsilon)
    Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
    boolean
    Compares this vector with the other vector using Mathf.FLOAT_ROUNDING_ERROR for fuzzy equality testing
    boolean
    epsilonEquals(Vec3 other, float epsilon)
    Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
    boolean
     
    Sets this Vec3 to the value represented by the specified string according to the format of toString().
    int
     
    boolean
     
    boolean
     
    boolean
    idt(Vec3 vector)
     
    interpolate(Vec3 target, float alpha, Interp interpolator)
    Interpolates between this vector and the given target vector by alpha (within range [0,1]) using the given Interpolation method.
    boolean
     
    boolean
    isCollinear(Vec3 other, float epsilon)
     
    boolean
     
    boolean
    isCollinearOpposite(Vec3 other, float epsilon)
     
    boolean
    isOnLine(Vec3 other)
     
    boolean
    isOnLine(Vec3 other, float epsilon)
     
    boolean
     
    boolean
    isPerpendicular(Vec3 vector, float epsilon)
     
    boolean
     
    boolean
    isUnit(float margin)
     
    boolean
     
    boolean
    isZero(float margin)
     
    float
    len()
     
    static float
    len(float x, float y, float z)
     
    float
    This method is faster than Vector.len() because it avoids calculating a square root.
    static float
    len2(float x, float y, float z)
     
    lerp(Vec3 target, float alpha)
    Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1].
    limit(float limit)
    Limits the length of this vector, based on the desired maximum length.
    limit2(float limit2)
    Limits the length of this vector, based on the desired maximum length squared.
    mul(Mat matrix)
    Left-multiplies the vector by the given matrix.
    mul4x3(float[] matrix)
    Left-multiplies the vector by the given 4x3 column major matrix.
    mulAdd(Vec3 vec, float scalar)
    First scale a supplied vector, then add it to this vector.
    mulAdd(Vec3 vec, Vec3 mulVec)
    First scale a supplied vector, then add it to this vector.
    nor()
    Normalizes this vector.
    rotate(Vec3 axis, float degrees)
    Rotates this vector by the given angle in degrees around the given axis.
    scl(float scalar)
    Scales this vector by a scalar
    scl(float vx, float vy, float vz)
    Scales this vector by the given values
    scl(Vec3 other)
    Scales this vector by another vector
    set(float[] values)
    Sets the components from the array.
    set(float[] values, int offset)
     
    set(float x, float y, float z)
    Sets the vector to the given components
    set(Vec2 vector, float z)
    Sets the components of the given vector and z-component
    set(Vec3 vector)
    Sets this vector from the given vector
    setFromSpherical(float azimuthalAngle, float polarAngle)
    Sets the components from the given spherical coordinate
    setLength(float len)
    Sets the length of this vector.
    setLength2(float len2)
    Sets the length of this vector, based on the square of the desired length.
    Sets this vector to the unit vector with a random direction
     
    Sets the components of this vector to 0
    slerp(Vec3 target, float alpha)
    Spherically interpolates between this vector and the target vector by alpha which is in the range [0,1].
    sub(float value)
    Subtracts the given value from all components of this vector
    sub(float x, float y, float z)
    Subtracts the other vector from this vector.
    sub(Vec3 a_vec)
    Subtracts the given vector from this vector.
    sub(Vec3 vector, float scale)
     
    Converts this Vec3 to a string in the format (x,y,z).
    traMul(Mat matrix)
    Multiplies the vector by the transpose of the given matrix.
    boolean
    within(Vec3 v, float dst)
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface arc.math.geom.Vector

    minus, plus, times, unaryMinus
  • Field Details

    • X

      public static final Vec3 X
    • Y

      public static final Vec3 Y
    • Z

      public static final Vec3 Z
    • Zero

      public static final Vec3 Zero
    • x

      public float x
      the x-component of this vector
    • y

      public float y
      the y-component of this vector
    • z

      public float z
      the z-component of this vector
  • Constructor Details

    • Vec3

      public Vec3()
      Constructs a vector at (0,0,0)
    • Vec3

      public Vec3(float x, float y, float z)
      Creates a vector with the given components
      Parameters:
      x - The x-component
      y - The y-component
      z - The z-component
    • Vec3

      public Vec3(double x, double y, double z)
    • Vec3

      public Vec3(Vec3 vector)
      Creates a vector from the given vector
      Parameters:
      vector - The vector
    • Vec3

      public Vec3(float[] values)
      Creates a vector from the given array. The array must have at least 3 elements.
      Parameters:
      values - The array
    • Vec3

      public Vec3(Vec2 vector, float z)
      Creates a vector from the given vector and z-component
      Parameters:
      vector - The vector
      z - The z-component
  • Method Details

    • len

      public static float len(float x, float y, float z)
      Returns:
      The euclidean length
    • len2

      public static float len2(float x, float y, float z)
      Returns:
      The squared euclidean length
    • dst

      public static float dst(float x1, float y1, float z1, float x2, float y2, float z2)
      Returns:
      The euclidean distance between the two specified vectors
    • dst2

      public static float dst2(float x1, float y1, float z1, float x2, float y2, float z2)
      Returns:
      the squared distance between the given points
    • dot

      public static float dot(float x1, float y1, float z1, float x2, float y2, float z2)
      Returns:
      The dot product between the two vectors
    • set

      public Vec3 set(float x, float y, float z)
      Sets the vector to the given components
      Parameters:
      x - The x-component
      y - The y-component
      z - The z-component
      Returns:
      this vector for chaining
    • div

      public Vec3 div(Vec3 other)
      Description copied from interface: Vector
      Inverse of scl()
      Specified by:
      div in interface Vector<Vec3>
    • set

      public Vec3 set(Vec3 vector)
      Description copied from interface: Vector
      Sets this vector from the given vector
      Specified by:
      set in interface Vector<Vec3>
      Parameters:
      vector - The vector
      Returns:
      This vector for chaining
    • set

      public Vec3 set(float[] values)
      Sets the components from the array. The array must have at least 3 elements
      Parameters:
      values - The array
      Returns:
      this vector for chaining
    • set

      public Vec3 set(float[] values, int offset)
    • set

      public Vec3 set(Vec2 vector, float z)
      Sets the components of the given vector and z-component
      Parameters:
      vector - The vector
      z - The z-component
      Returns:
      This vector for chaining
    • setFromSpherical

      public Vec3 setFromSpherical(float azimuthalAngle, float polarAngle)
      Sets the components from the given spherical coordinate
      Parameters:
      azimuthalAngle - The angle between x-axis in radians [0, 2pi]
      polarAngle - The angle between z-axis in radians [0, pi]
      Returns:
      This vector for chaining
    • setToRandomDirection

      public Vec3 setToRandomDirection()
      Description copied from interface: Vector
      Sets this vector to the unit vector with a random direction
      Specified by:
      setToRandomDirection in interface Vector<Vec3>
      Returns:
      This vector for chaining
    • setToRandomDirection

      public Vec3 setToRandomDirection(Rand rand)
    • cpy

      public Vec3 cpy()
      Specified by:
      cpy in interface Vector<Vec3>
      Returns:
      a copy of this vector
    • add

      public Vec3 add(Vec3 vector)
      Description copied from interface: Vector
      Adds the given vector to this vector
      Specified by:
      add in interface Vector<Vec3>
      Parameters:
      vector - The vector
      Returns:
      This vector for chaining
    • cpy

      public Vec3 cpy(Vec3 dest)
    • add

      public Vec3 add(Vec3 vector, float scale)
    • sub

      public Vec3 sub(Vec3 vector, float scale)
    • add

      public Vec3 add(float x, float y, float z)
      Adds the given vector to this component
      Parameters:
      x - The x-component of the other vector
      y - The y-component of the other vector
      z - The z-component of the other vector
      Returns:
      This vector for chaining.
    • add

      public Vec3 add(float values)
      Adds the given value to all three components of the vector.
      Parameters:
      values - The value
      Returns:
      This vector for chaining
    • sub

      public Vec3 sub(Vec3 a_vec)
      Description copied from interface: Vector
      Subtracts the given vector from this vector.
      Specified by:
      sub in interface Vector<Vec3>
      Parameters:
      a_vec - The vector
      Returns:
      This vector for chaining
    • sub

      public Vec3 sub(float x, float y, float z)
      Subtracts the other vector from this vector.
      Parameters:
      x - The x-component of the other vector
      y - The y-component of the other vector
      z - The z-component of the other vector
      Returns:
      This vector for chaining
    • sub

      public Vec3 sub(float value)
      Subtracts the given value from all components of this vector
      Parameters:
      value - The value
      Returns:
      This vector for chaining
    • scl

      public Vec3 scl(float scalar)
      Description copied from interface: Vector
      Scales this vector by a scalar
      Specified by:
      scl in interface Vector<Vec3>
      Parameters:
      scalar - The scalar
      Returns:
      This vector for chaining
    • scl

      public Vec3 scl(Vec3 other)
      Description copied from interface: Vector
      Scales this vector by another vector
      Specified by:
      scl in interface Vector<Vec3>
      Returns:
      This vector for chaining
    • scl

      public Vec3 scl(float vx, float vy, float vz)
      Scales this vector by the given values
      Parameters:
      vx - X value
      vy - Y value
      vz - Z value
      Returns:
      This vector for chaining
    • mulAdd

      public Vec3 mulAdd(Vec3 vec, float scalar)
      Description copied from interface: Vector
      First scale a supplied vector, then add it to this vector.
      Specified by:
      mulAdd in interface Vector<Vec3>
      Parameters:
      vec - addition vector
      scalar - for scaling the addition vector
    • mulAdd

      public Vec3 mulAdd(Vec3 vec, Vec3 mulVec)
      Description copied from interface: Vector
      First scale a supplied vector, then add it to this vector.
      Specified by:
      mulAdd in interface Vector<Vec3>
      Parameters:
      vec - addition vector
      mulVec - vector by whose values the addition vector will be scaled
    • len

      public float len()
      Specified by:
      len in interface Vector<Vec3>
      Returns:
      The euclidean length
    • len2

      public float len2()
      Description copied from interface: Vector
      This method is faster than Vector.len() because it avoids calculating a square root. It is useful for comparisons, but not for getting exact lengths, as the return value is the square of the actual length.
      Specified by:
      len2 in interface Vector<Vec3>
      Returns:
      The squared euclidean length
    • idt

      public boolean idt(Vec3 vector)
      Parameters:
      vector - The other vector
      Returns:
      Whether this and the other vector are equal
    • dst

      public float dst(Vec3 vector)
      Specified by:
      dst in interface Vector<Vec3>
      Parameters:
      vector - The other vector
      Returns:
      the distance between this and the other vector
    • dst

      public float dst(float x, float y, float z)
      Returns:
      the distance between this point and the given point
    • dst2

      public float dst2(Vec3 point)
      Description copied from interface: Vector
      This method is faster than Vector.dst(Vector) because it avoids calculating a square root. It is useful for comparisons, but not for getting accurate distances, as the return value is the square of the actual distance.
      Specified by:
      dst2 in interface Vector<Vec3>
      Parameters:
      point - The other vector
      Returns:
      the squared distance between this and the other vector
    • dst2

      public float dst2(float x, float y, float z)
      Returns the squared distance between this point and the given point
      Parameters:
      x - The x-component of the other point
      y - The y-component of the other point
      z - The z-component of the other point
      Returns:
      The squared distance
    • within

      public boolean within(Vec3 v, float dst)
    • nor

      public Vec3 nor()
      Description copied from interface: Vector
      Normalizes this vector. Does nothing if it is zero.
      Specified by:
      nor in interface Vector<Vec3>
      Returns:
      This vector for chaining
    • dot

      public float dot(Vec3 vector)
      Specified by:
      dot in interface Vector<Vec3>
      Parameters:
      vector - The other vector
      Returns:
      The dot product between this and the other vector
    • angleRad

      public float angleRad(Vec3 vector)
      Returns:
      the angle to the other vector, in radians.
    • angle

      public float angle(Vec3 vector)
      Returns:
      the angle to the other vector, in degrees.
    • dot

      public float dot(float x, float y, float z)
      Returns the dot product between this and the given vector.
      Parameters:
      x - The x-component of the other vector
      y - The y-component of the other vector
      z - The z-component of the other vector
      Returns:
      The dot product
    • crs

      public Vec3 crs(Vec3 vector)
      Sets this vector to the cross product between it and the other vector.
      Parameters:
      vector - The other vector
      Returns:
      This vector for chaining
    • crs

      public Vec3 crs(float x, float y, float z)
      Sets this vector to the cross product between it and the other vector.
      Parameters:
      x - The x-component of the other vector
      y - The y-component of the other vector
      z - The z-component of the other vector
      Returns:
      This vector for chaining
    • mul4x3

      public Vec3 mul4x3(float[] matrix)
      Left-multiplies the vector by the given 4x3 column major matrix. The matrix should be composed by a 3x3 matrix representing rotation and scale plus a 1x3 matrix representing the translation.
      Parameters:
      matrix - The matrix
      Returns:
      This vector for chaining
    • mul

      public Vec3 mul(Mat matrix)
      Left-multiplies the vector by the given matrix.
      Parameters:
      matrix - The matrix
      Returns:
      This vector for chaining
    • traMul

      public Vec3 traMul(Mat matrix)
      Multiplies the vector by the transpose of the given matrix.
      Parameters:
      matrix - The matrix
      Returns:
      This vector for chaining
    • rotate

      public Vec3 rotate(Vec3 axis, float degrees)
      Rotates this vector by the given angle in degrees around the given axis.
      Parameters:
      axis - the axis
      degrees - the angle in degrees
      Returns:
      This vector for chaining
    • isUnit

      public boolean isUnit()
      Specified by:
      isUnit in interface Vector<Vec3>
      Returns:
      Whether this vector is a unit length vector
    • isUnit

      public boolean isUnit(float margin)
      Specified by:
      isUnit in interface Vector<Vec3>
      Returns:
      Whether this vector is a unit length vector within the given margin.
    • isZero

      public boolean isZero()
      Specified by:
      isZero in interface Vector<Vec3>
      Returns:
      Whether this vector is a zero vector
    • isZero

      public boolean isZero(float margin)
      Specified by:
      isZero in interface Vector<Vec3>
      Returns:
      Whether the length of this vector is smaller than the given margin
    • isOnLine

      public boolean isOnLine(Vec3 other, float epsilon)
      Specified by:
      isOnLine in interface Vector<Vec3>
      Returns:
      true if this vector is in line with the other vector (either in the same or the opposite direction)
    • isOnLine

      public boolean isOnLine(Vec3 other)
      Specified by:
      isOnLine in interface Vector<Vec3>
      Returns:
      true if this vector is in line with the other vector (either in the same or the opposite direction)
    • isCollinear

      public boolean isCollinear(Vec3 other, float epsilon)
      Specified by:
      isCollinear in interface Vector<Vec3>
      Returns:
      true if this vector is collinear with the other vector (Vector.isOnLine(Vector, float) && Vector.hasSameDirection(Vector)).
    • isCollinear

      public boolean isCollinear(Vec3 other)
      Specified by:
      isCollinear in interface Vector<Vec3>
      Returns:
      true if this vector is collinear with the other vector (Vector.isOnLine(Vector) && Vector.hasSameDirection(Vector)).
    • isCollinearOpposite

      public boolean isCollinearOpposite(Vec3 other, float epsilon)
      Specified by:
      isCollinearOpposite in interface Vector<Vec3>
      Returns:
      true if this vector is opposite collinear with the other vector (Vector.isOnLine(Vector, float) && Vector.hasOppositeDirection(Vector)).
    • isCollinearOpposite

      public boolean isCollinearOpposite(Vec3 other)
      Specified by:
      isCollinearOpposite in interface Vector<Vec3>
      Returns:
      true if this vector is opposite collinear with the other vector (Vector.isOnLine(Vector) && Vector.hasOppositeDirection(Vector)).
    • isPerpendicular

      public boolean isPerpendicular(Vec3 vector)
      Specified by:
      isPerpendicular in interface Vector<Vec3>
      Returns:
      Whether this vector is perpendicular with the other vector. True if the dot product is 0.
    • isPerpendicular

      public boolean isPerpendicular(Vec3 vector, float epsilon)
      Specified by:
      isPerpendicular in interface Vector<Vec3>
      epsilon - a positive small number close to zero
      Returns:
      Whether this vector is perpendicular with the other vector. True if the dot product is 0.
    • hasSameDirection

      public boolean hasSameDirection(Vec3 vector)
      Specified by:
      hasSameDirection in interface Vector<Vec3>
      Returns:
      Whether this vector has similar direction compared to the other vector. True if the normalized dot product is > 0.
    • hasOppositeDirection

      public boolean hasOppositeDirection(Vec3 vector)
      Specified by:
      hasOppositeDirection in interface Vector<Vec3>
      Returns:
      Whether this vector has opposite direction compared to the other vector. True if the normalized dot product is < 0.
    • lerp

      public Vec3 lerp(Vec3 target, float alpha)
      Description copied from interface: Vector
      Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1]. The result is stored in this vector.
      Specified by:
      lerp in interface Vector<Vec3>
      Parameters:
      target - The target vector
      alpha - The interpolation coefficient
      Returns:
      This vector for chaining.
    • interpolate

      public Vec3 interpolate(Vec3 target, float alpha, Interp interpolator)
      Description copied from interface: Vector
      Interpolates between this vector and the given target vector by alpha (within range [0,1]) using the given Interpolation method. the result is stored in this vector.
      Specified by:
      interpolate in interface Vector<Vec3>
      Parameters:
      target - The target vector
      alpha - The interpolation coefficient
      interpolator - An Interpolation object describing the used interpolation method
      Returns:
      This vector for chaining.
    • slerp

      public Vec3 slerp(Vec3 target, float alpha)
      Spherically interpolates between this vector and the target vector by alpha which is in the range [0,1]. The result is stored in this vector.
      Parameters:
      target - The target vector
      alpha - The interpolation coefficient
      Returns:
      This vector for chaining.
    • toString

      public String toString()
      Converts this Vec3 to a string in the format (x,y,z).
      Overrides:
      toString in class Object
      Returns:
      a string representation of this object.
    • fromString

      public Vec3 fromString(String v)
      Sets this Vec3 to the value represented by the specified string according to the format of toString().
      Parameters:
      v - the string.
      Returns:
      this vector for chaining
    • limit

      public Vec3 limit(float limit)
      Description copied from interface: Vector
      Limits the length of this vector, based on the desired maximum length.
      Specified by:
      limit in interface Vector<Vec3>
      Parameters:
      limit - desired maximum length for this vector
      Returns:
      this vector for chaining
    • limit2

      public Vec3 limit2(float limit2)
      Description copied from interface: Vector
      Limits the length of this vector, based on the desired maximum length squared.

      This method is slightly faster than limit().

      Specified by:
      limit2 in interface Vector<Vec3>
      Parameters:
      limit2 - squared desired maximum length for this vector
      Returns:
      this vector for chaining
      See Also:
    • setLength

      public Vec3 setLength(float len)
      Description copied from interface: Vector
      Sets the length of this vector. Does nothing if this vector is zero.
      Specified by:
      setLength in interface Vector<Vec3>
      Parameters:
      len - desired length for this vector
      Returns:
      this vector for chaining
    • setLength2

      public Vec3 setLength2(float len2)
      Description copied from interface: Vector
      Sets the length of this vector, based on the square of the desired length. Does nothing if this vector is zero.

      This method is slightly faster than setLength().

      Specified by:
      setLength2 in interface Vector<Vec3>
      Parameters:
      len2 - desired square of the length for this vector
      Returns:
      this vector for chaining
      See Also:
    • clamp

      public Vec3 clamp(float min, float max)
      Description copied from interface: Vector
      Clamps this vector's length to given min and max values
      Specified by:
      clamp in interface Vector<Vec3>
      Parameters:
      min - Min length
      max - Max length
      Returns:
      This vector for chaining
    • hashCode

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

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

      public boolean epsilonEquals(Vec3 other, float epsilon)
      Description copied from interface: Vector
      Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
      Specified by:
      epsilonEquals in interface Vector<Vec3>
      Returns:
      whether the vectors have fuzzy equality.
    • epsilonEquals

      public boolean epsilonEquals(float x, float y, float z, float epsilon)
      Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
      Returns:
      whether the vectors are the same.
    • epsilonEquals

      public boolean epsilonEquals(Vec3 other)
      Compares this vector with the other vector using Mathf.FLOAT_ROUNDING_ERROR for fuzzy equality testing
      Parameters:
      other - other vector to compare
      Returns:
      true if vector are equal, otherwise false
    • epsilonEquals

      public boolean epsilonEquals(float x, float y, float z)
      Compares this vector with the other vector using Mathf.FLOAT_ROUNDING_ERROR for fuzzy equality testing
      Parameters:
      x - x component of the other vector to compare
      y - y component of the other vector to compare
      z - z component of the other vector to compare
      Returns:
      true if vector are equal, otherwise false
    • setZero

      public Vec3 setZero()
      Description copied from interface: Vector
      Sets the components of this vector to 0
      Specified by:
      setZero in interface Vector<Vec3>
      Returns:
      This vector for chaining