Class Vector2

  • All Implemented Interfaces:
    Vector<Vector2>, java.io.Serializable

    public class Vector2
    extends java.lang.Object
    implements java.io.Serializable, Vector<Vector2>
    Encapsulates a 2D vector. Allows chaining methods by returning a reference to itself
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      float x
      the x-component of this vector
      static Vector2 X  
      float y
      the y-component of this vector
      static Vector2 Y  
      static Vector2 Zero  
    • Constructor Summary

      Constructors 
      Constructor Description
      Vector2()
      Constructs a new vector at (0,0)
      Vector2​(float x, float y)
      Constructs a vector with the given components
      Vector2​(Vector2 v)
      Constructs a vector from the given vector
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      Vector2 add​(float x, float y)
      Adds the given components to this vector
      Vector2 add​(Vector2 v)
      Adds the given vector to this vector
      float angle()
      Deprecated.
      use angleDeg() instead.
      float angle​(Vector2 reference)
      Deprecated.
      use angleDeg(Vector2) instead.
      float angleDeg()  
      float angleDeg​(Vector2 reference)  
      float angleRad()  
      float angleRad​(Vector2 reference)  
      Vector2 clamp​(float min, float max)
      Clamps this vector's length to given min and max values
      Vector2 cpy()  
      float crs​(float x, float y)
      Calculates the 2D cross product between this and the given vector.
      float crs​(Vector2 v)
      Calculates the 2D cross product between this and the given vector.
      float dot​(float ox, float oy)  
      static float dot​(float x1, float y1, float x2, float y2)  
      float dot​(Vector2 v)  
      float dst​(float x, float y)  
      static float dst​(float x1, float y1, float x2, float y2)  
      float dst​(Vector2 v)  
      float dst2​(float x, float y)  
      static float dst2​(float x1, float y1, float x2, float y2)  
      float dst2​(Vector2 v)
      This method is faster than Vector.dst(Vector) because it avoids calculating a square root.
      boolean epsilonEquals​(float x, float y)
      Compares this vector with the other vector using MathUtils.FLOAT_ROUNDING_ERROR for fuzzy equality testing
      boolean epsilonEquals​(float x, float y, float epsilon)
      Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
      boolean epsilonEquals​(Vector2 other)
      Compares this vector with the other vector using MathUtils.FLOAT_ROUNDING_ERROR for fuzzy equality testing
      boolean epsilonEquals​(Vector2 other, float epsilon)
      Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
      boolean equals​(java.lang.Object obj)  
      Vector2 fromString​(java.lang.String v)
      Sets this Vector2 to the value represented by the specified string according to the format of toString().
      int hashCode()  
      boolean hasOppositeDirection​(Vector2 vector)  
      boolean hasSameDirection​(Vector2 vector)  
      boolean idt​(Vector2 vector)
      Returns true if this vector and the vector parameter have identical components.
      Vector2 interpolate​(Vector2 target, float alpha, Interpolation interpolation)
      Interpolates between this vector and the given target vector by alpha (within range [0,1]) using the given Interpolation method.
      boolean isCollinear​(Vector2 other)  
      boolean isCollinear​(Vector2 other, float epsilon)  
      boolean isCollinearOpposite​(Vector2 other)  
      boolean isCollinearOpposite​(Vector2 other, float epsilon)  
      boolean isOnLine​(Vector2 other)  
      boolean isOnLine​(Vector2 other, float epsilon)  
      boolean isPerpendicular​(Vector2 vector)  
      boolean isPerpendicular​(Vector2 vector, float epsilon)  
      boolean isUnit()  
      boolean isUnit​(float margin)  
      boolean isZero()  
      boolean isZero​(float margin)  
      float len()  
      static float len​(float x, float y)  
      float len2()
      This method is faster than Vector.len() because it avoids calculating a square root.
      static float len2​(float x, float y)  
      Vector2 lerp​(Vector2 target, float alpha)
      Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1].
      Vector2 limit​(float limit)
      Limits the length of this vector, based on the desired maximum length.
      Vector2 limit2​(float limit2)
      Limits the length of this vector, based on the desired maximum length squared.
      Vector2 mul​(Matrix3 mat)
      Left-multiplies this vector by the given matrix
      Vector2 mulAdd​(Vector2 vec, float scalar)
      First scale a supplied vector, then add it to this vector.
      Vector2 mulAdd​(Vector2 vec, Vector2 mulVec)
      First scale a supplied vector, then add it to this vector.
      Vector2 nor()
      Normalizes this vector.
      Vector2 rotate​(float degrees)
      Deprecated.
      use rotateDeg(float) instead.
      Vector2 rotate90​(int dir)
      Rotates the Vector2 by 90 degrees in the specified direction, where >= 0 is counter-clockwise and < 0 is clockwise.
      Vector2 rotateAround​(Vector2 reference, float degrees)
      Deprecated.
      Vector2 rotateAroundDeg​(Vector2 reference, float degrees)
      Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
      Vector2 rotateAroundRad​(Vector2 reference, float radians)
      Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
      Vector2 rotateDeg​(float degrees)
      Rotates the Vector2 by the given angle, counter-clockwise assuming the y-axis points up.
      Vector2 rotateRad​(float radians)
      Rotates the Vector2 by the given angle, counter-clockwise assuming the y-axis points up.
      Vector2 scl​(float scalar)
      Scales this vector by a scalar
      Vector2 scl​(float x, float y)
      Multiplies this vector by a scalar
      Vector2 scl​(Vector2 v)
      Scales this vector by another vector
      Vector2 set​(float x, float y)
      Sets the components of this vector
      Vector2 set​(Vector2 v)
      Sets this vector from the given vector
      Vector2 setAngle​(float degrees)
      Deprecated.
      use setAngleDeg(float) instead.
      Vector2 setAngleDeg​(float degrees)
      Sets the angle of the vector in degrees relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
      Vector2 setAngleRad​(float radians)
      Sets the angle of the vector in radians relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
      Vector2 setLength​(float len)
      Sets the length of this vector.
      Vector2 setLength2​(float len2)
      Sets the length of this vector, based on the square of the desired length.
      Vector2 setToRandomDirection()
      Sets this vector to the unit vector with a random direction
      Vector2 setZero()
      Sets the components of this vector to 0
      Vector2 sub​(float x, float y)
      Subtracts the other vector from this vector.
      Vector2 sub​(Vector2 v)
      Subtracts the given vector from this vector.
      java.lang.String toString()
      Converts this Vector2 to a string in the format (x,y).
      • Methods inherited from class java.lang.Object

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

      • Zero

        public static final Vector2 Zero
      • x

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

        public float y
        the y-component of this vector
    • Constructor Detail

      • Vector2

        public Vector2()
        Constructs a new vector at (0,0)
      • Vector2

        public Vector2​(float x,
                       float y)
        Constructs a vector with the given components
        Parameters:
        x - The x-component
        y - The y-component
      • Vector2

        public Vector2​(Vector2 v)
        Constructs a vector from the given vector
        Parameters:
        v - The vector
    • Method Detail

      • len

        public static float len​(float x,
                                float y)
      • len

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

        public static float len2​(float x,
                                 float y)
      • 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<Vector2>
        Returns:
        The squared Euclidean length
      • set

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

        public Vector2 set​(float x,
                           float y)
        Sets the components of this vector
        Parameters:
        x - The x-component
        y - The y-component
        Returns:
        This vector for chaining
      • sub

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

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

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

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

        public Vector2 add​(float x,
                           float y)
        Adds the given components to this vector
        Parameters:
        x - The x-component
        y - The y-component
        Returns:
        This vector for chaining
      • dot

        public static float dot​(float x1,
                                float y1,
                                float x2,
                                float y2)
      • dot

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

        public float dot​(float ox,
                         float oy)
      • scl

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

        public Vector2 scl​(float x,
                           float y)
        Multiplies this vector by a scalar
        Returns:
        This vector for chaining
      • scl

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

        public Vector2 mulAdd​(Vector2 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<Vector2>
        Parameters:
        vec - addition vector
        scalar - for scaling the addition vector
      • mulAdd

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

        public boolean idt​(Vector2 vector)
        Returns true if this vector and the vector parameter have identical components.
        Parameters:
        vector - The other vector
        Returns:
        Whether this and the other vector are equal with exact precision
      • dst

        public static float dst​(float x1,
                                float y1,
                                float x2,
                                float y2)
      • dst

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

        public float dst​(float x,
                         float y)
        Parameters:
        x - The x-component of the other vector
        y - The y-component of the other vector
        Returns:
        the distance between this and the other vector
      • dst2

        public static float dst2​(float x1,
                                 float y1,
                                 float x2,
                                 float y2)
      • dst2

        public float dst2​(Vector2 v)
        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<Vector2>
        Parameters:
        v - The other vector
        Returns:
        the squared distance between this and the other vector
      • dst2

        public float dst2​(float x,
                          float y)
        Parameters:
        x - The x-component of the other vector
        y - The y-component of the other vector
        Returns:
        the squared distance between this and the other vector
      • limit

        public Vector2 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<Vector2>
        Parameters:
        limit - desired maximum length for this vector
        Returns:
        this vector for chaining
      • limit2

        public Vector2 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<Vector2>
        Parameters:
        limit2 - squared desired maximum length for this vector
        Returns:
        this vector for chaining
        See Also:
        Vector.len2()
      • clamp

        public Vector2 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<Vector2>
        Parameters:
        min - Min length
        max - Max length
        Returns:
        This vector for chaining
      • setLength

        public Vector2 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<Vector2>
        Parameters:
        len - desired length for this vector
        Returns:
        this vector for chaining
      • setLength2

        public Vector2 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<Vector2>
        Parameters:
        len2 - desired square of the length for this vector
        Returns:
        this vector for chaining
        See Also:
        Vector.len2()
      • toString

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

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

        public Vector2 mul​(Matrix3 mat)
        Left-multiplies this vector by the given matrix
        Parameters:
        mat - the matrix
        Returns:
        this vector
      • crs

        public float crs​(Vector2 v)
        Calculates the 2D cross product between this and the given vector.
        Parameters:
        v - the other vector
        Returns:
        the cross product
      • crs

        public float crs​(float x,
                         float y)
        Calculates the 2D cross product between this and the given vector.
        Parameters:
        x - the x-coordinate of the other vector
        y - the y-coordinate of the other vector
        Returns:
        the cross product
      • angle

        @Deprecated
        public float angle()
        Deprecated.
        use angleDeg() instead.
        Returns:
        the angle in degrees of this vector (point) relative to the x-axis. Angles are towards the positive y-axis (typically counter-clockwise) and between 0 and 360.
      • angle

        @Deprecated
        public float angle​(Vector2 reference)
        Deprecated.
        use angleDeg(Vector2) instead. Beware of the changes in returned angle to counter-clockwise and the range.
        Returns:
        the angle in degrees of this vector (point) relative to the given vector. Angles are towards the negative y-axis (typically clockwise) between -180 and +180
      • angleDeg

        public float angleDeg()
        Returns:
        the angle in degrees of this vector (point) relative to the x-axis. Angles are towards the positive y-axis (typically counter-clockwise) and in the [0, 360) range.
      • angleDeg

        public float angleDeg​(Vector2 reference)
        Returns:
        the angle in degrees of this vector (point) relative to the given vector. Angles are towards the positive y-axis (typically counter-clockwise.) in the [0, 360) range
      • angleRad

        public float angleRad()
        Returns:
        the angle in radians of this vector (point) relative to the x-axis. Angles are towards the positive y-axis. (typically counter-clockwise)
      • angleRad

        public float angleRad​(Vector2 reference)
        Returns:
        the angle in radians of this vector (point) relative to the given vector. Angles are towards the positive y-axis. (typically counter-clockwise.)
      • setAngle

        @Deprecated
        public Vector2 setAngle​(float degrees)
        Deprecated.
        use setAngleDeg(float) instead.
        Sets the angle of the vector in degrees relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
        Parameters:
        degrees - The angle in degrees to set.
      • setAngleDeg

        public Vector2 setAngleDeg​(float degrees)
        Sets the angle of the vector in degrees relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
        Parameters:
        degrees - The angle in degrees to set.
      • setAngleRad

        public Vector2 setAngleRad​(float radians)
        Sets the angle of the vector in radians relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
        Parameters:
        radians - The angle in radians to set.
      • rotate

        @Deprecated
        public Vector2 rotate​(float degrees)
        Deprecated.
        use rotateDeg(float) instead.
        Rotates the Vector2 by the given angle, counter-clockwise assuming the y-axis points up.
        Parameters:
        degrees - the angle in degrees
      • rotateAround

        @Deprecated
        public Vector2 rotateAround​(Vector2 reference,
                                    float degrees)
        Deprecated.
        Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
        Parameters:
        degrees - the angle in degrees
        reference - center Vector2
      • rotateDeg

        public Vector2 rotateDeg​(float degrees)
        Rotates the Vector2 by the given angle, counter-clockwise assuming the y-axis points up.
        Parameters:
        degrees - the angle in degrees
      • rotateRad

        public Vector2 rotateRad​(float radians)
        Rotates the Vector2 by the given angle, counter-clockwise assuming the y-axis points up.
        Parameters:
        radians - the angle in radians
      • rotateAroundDeg

        public Vector2 rotateAroundDeg​(Vector2 reference,
                                       float degrees)
        Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
        Parameters:
        degrees - the angle in degrees
        reference - center Vector2
      • rotateAroundRad

        public Vector2 rotateAroundRad​(Vector2 reference,
                                       float radians)
        Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
        Parameters:
        radians - the angle in radians
        reference - center Vector2
      • rotate90

        public Vector2 rotate90​(int dir)
        Rotates the Vector2 by 90 degrees in the specified direction, where >= 0 is counter-clockwise and < 0 is clockwise.
      • lerp

        public Vector2 lerp​(Vector2 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<Vector2>
        Parameters:
        target - The target vector
        alpha - The interpolation coefficient
        Returns:
        This vector for chaining.
      • interpolate

        public Vector2 interpolate​(Vector2 target,
                                   float alpha,
                                   Interpolation interpolation)
        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<Vector2>
        Parameters:
        target - The target vector
        alpha - The interpolation coefficient
        interpolation - An Interpolation object describing the used interpolation method
        Returns:
        This vector for chaining.
      • setToRandomDirection

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

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

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • epsilonEquals

        public boolean epsilonEquals​(Vector2 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<Vector2>
        Returns:
        whether the vectors have fuzzy equality.
      • epsilonEquals

        public boolean epsilonEquals​(float x,
                                     float y,
                                     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​(Vector2 other)
        Compares this vector with the other vector using MathUtils.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)
        Compares this vector with the other vector using MathUtils.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
        Returns:
        true if vector are equal, otherwise false
      • isUnit

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

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

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

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

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

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

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

        public boolean isPerpendicular​(Vector2 vector,
                                       float epsilon)
        Specified by:
        isPerpendicular in interface Vector<Vector2>
        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​(Vector2 vector)
        Specified by:
        hasSameDirection in interface Vector<Vector2>
        Returns:
        Whether this vector has similar direction compared to the other vector. True if the normalized dot product is > 0.
      • hasOppositeDirection

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

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