Package com.badlogic.gdx.math
Class Vector2
- java.lang.Object
-
- com.badlogic.gdx.math.Vector2
-
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
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Vector2add(float x, float y)Adds the given components to this vectorVector2add(Vector2 v)Adds the given vector to this vectorfloatangle()Deprecated.useangleDeg()instead.floatangle(Vector2 reference)Deprecated.useangleDeg(Vector2)instead.floatangleDeg()floatangleDeg(Vector2 reference)floatangleRad()floatangleRad(Vector2 reference)Vector2clamp(float min, float max)Clamps this vector's length to given min and max valuesVector2cpy()floatcrs(float x, float y)Calculates the 2D cross product between this and the given vector.floatcrs(Vector2 v)Calculates the 2D cross product between this and the given vector.floatdot(float ox, float oy)static floatdot(float x1, float y1, float x2, float y2)floatdot(Vector2 v)floatdst(float x, float y)static floatdst(float x1, float y1, float x2, float y2)floatdst(Vector2 v)floatdst2(float x, float y)static floatdst2(float x1, float y1, float x2, float y2)floatdst2(Vector2 v)This method is faster thanVector.dst(Vector)because it avoids calculating a square root.booleanepsilonEquals(float x, float y)Compares this vector with the other vector using MathUtils.FLOAT_ROUNDING_ERROR for fuzzy equality testingbooleanepsilonEquals(float x, float y, float epsilon)Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.booleanepsilonEquals(Vector2 other)Compares this vector with the other vector using MathUtils.FLOAT_ROUNDING_ERROR for fuzzy equality testingbooleanepsilonEquals(Vector2 other, float epsilon)Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.booleanequals(java.lang.Object obj)Vector2fromString(java.lang.String v)Sets thisVector2to the value represented by the specified string according to the format oftoString().inthashCode()booleanhasOppositeDirection(Vector2 vector)booleanhasSameDirection(Vector2 vector)booleanidt(Vector2 vector)Returns true if this vector and the vector parameter have identical components.Vector2interpolate(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.booleanisCollinear(Vector2 other)booleanisCollinear(Vector2 other, float epsilon)booleanisCollinearOpposite(Vector2 other)booleanisCollinearOpposite(Vector2 other, float epsilon)booleanisOnLine(Vector2 other)booleanisOnLine(Vector2 other, float epsilon)booleanisPerpendicular(Vector2 vector)booleanisPerpendicular(Vector2 vector, float epsilon)booleanisUnit()booleanisUnit(float margin)booleanisZero()booleanisZero(float margin)floatlen()static floatlen(float x, float y)floatlen2()This method is faster thanVector.len()because it avoids calculating a square root.static floatlen2(float x, float y)Vector2lerp(Vector2 target, float alpha)Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1].Vector2limit(float limit)Limits the length of this vector, based on the desired maximum length.Vector2limit2(float limit2)Limits the length of this vector, based on the desired maximum length squared.Vector2mul(Matrix3 mat)Left-multiplies this vector by the given matrixVector2mulAdd(Vector2 vec, float scalar)First scale a supplied vector, then add it to this vector.Vector2mulAdd(Vector2 vec, Vector2 mulVec)First scale a supplied vector, then add it to this vector.Vector2nor()Normalizes this vector.Vector2rotate(float degrees)Deprecated.userotateDeg(float)instead.Vector2rotate90(int dir)Rotates the Vector2 by 90 degrees in the specified direction, where >= 0 is counter-clockwise and < 0 is clockwise.Vector2rotateAround(Vector2 reference, float degrees)Deprecated.userotateAroundDeg(Vector2, float)instead.Vector2rotateAroundDeg(Vector2 reference, float degrees)Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.Vector2rotateAroundRad(Vector2 reference, float radians)Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.Vector2rotateDeg(float degrees)Rotates the Vector2 by the given angle, counter-clockwise assuming the y-axis points up.Vector2rotateRad(float radians)Rotates the Vector2 by the given angle, counter-clockwise assuming the y-axis points up.Vector2scl(float scalar)Scales this vector by a scalarVector2scl(float x, float y)Multiplies this vector by a scalarVector2scl(Vector2 v)Scales this vector by another vectorVector2set(float x, float y)Sets the components of this vectorVector2set(Vector2 v)Sets this vector from the given vectorVector2setAngle(float degrees)Deprecated.usesetAngleDeg(float)instead.Vector2setAngleDeg(float degrees)Sets the angle of the vector in degrees relative to the x-axis, towards the positive y-axis (typically counter-clockwise).Vector2setAngleRad(float radians)Sets the angle of the vector in radians relative to the x-axis, towards the positive y-axis (typically counter-clockwise).Vector2setLength(float len)Sets the length of this vector.Vector2setLength2(float len2)Sets the length of this vector, based on the square of the desired length.Vector2setToRandomDirection()Sets this vector to the unit vector with a random directionVector2setZero()Sets the components of this vector to 0Vector2sub(float x, float y)Subtracts the other vector from this vector.Vector2sub(Vector2 v)Subtracts the given vector from this vector.java.lang.StringtoString()Converts thisVector2to a string in the format(x,y).
-
-
-
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-componenty- The y-component
-
Vector2
public Vector2(Vector2 v)
Constructs a vector from the given vector- Parameters:
v- The vector
-
-
Method Detail
-
cpy
public Vector2 cpy()
-
len
public static float len(float x, float y)
-
len2
public static float len2(float x, float y)
-
len2
public float len2()
Description copied from interface:VectorThis method is faster thanVector.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.
-
set
public Vector2 set(Vector2 v)
Description copied from interface:VectorSets this vector from the given vector
-
set
public Vector2 set(float x, float y)
Sets the components of this vector- Parameters:
x- The x-componenty- The y-component- Returns:
- This vector for chaining
-
sub
public Vector2 sub(Vector2 v)
Description copied from interface:VectorSubtracts the given vector from this vector.
-
sub
public Vector2 sub(float x, float y)
Subtracts the other vector from this vector.- Parameters:
x- The x-component of the other vectory- The y-component of the other vector- Returns:
- This vector for chaining
-
nor
public Vector2 nor()
Description copied from interface:VectorNormalizes this vector. Does nothing if it is zero.
-
add
public Vector2 add(Vector2 v)
Description copied from interface:VectorAdds the given vector to this vector
-
add
public Vector2 add(float x, float y)
Adds the given components to this vector- Parameters:
x- The x-componenty- 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)
-
dot
public float dot(float ox, float oy)
-
scl
public Vector2 scl(float scalar)
Description copied from interface:VectorScales this vector by a scalar
-
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:VectorScales this vector by another vector
-
mulAdd
public Vector2 mulAdd(Vector2 vec, float scalar)
Description copied from interface:VectorFirst scale a supplied vector, then add it to this vector.
-
mulAdd
public Vector2 mulAdd(Vector2 vec, Vector2 mulVec)
Description copied from interface:VectorFirst scale a supplied vector, then add it to this vector.
-
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)
-
dst
public float dst(float x, float y)- Parameters:
x- The x-component of the other vectory- 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:VectorThis method is faster thanVector.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.
-
dst2
public float dst2(float x, float y)- Parameters:
x- The x-component of the other vectory- 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:VectorLimits the length of this vector, based on the desired maximum length.
-
limit2
public Vector2 limit2(float limit2)
Description copied from interface:VectorLimits the length of this vector, based on the desired maximum length squared. This method is slightly faster than limit().- Specified by:
limit2in interfaceVector<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:VectorClamps this vector's length to given min and max values
-
setLength
public Vector2 setLength(float len)
Description copied from interface:VectorSets the length of this vector. Does nothing if this vector is zero.
-
setLength2
public Vector2 setLength2(float len2)
Description copied from interface:VectorSets 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:
setLength2in interfaceVector<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 thisVector2to a string in the format(x,y).- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation of this object.
-
fromString
public Vector2 fromString(java.lang.String v)
Sets thisVector2to the value represented by the specified string according to the format oftoString().- 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 vectory- the y-coordinate of the other vector- Returns:
- the cross product
-
angle
@Deprecated public float angle()
Deprecated.useangleDeg()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.useangleDeg(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.usesetAngleDeg(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.userotateDeg(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.userotateAroundDeg(Vector2, float)instead.Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.- Parameters:
degrees- the angle in degreesreference- 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 degreesreference- 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 radiansreference- 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:VectorLinearly interpolates between this vector and the target vector by alpha which is in the range [0,1]. The result is stored in this vector.
-
interpolate
public Vector2 interpolate(Vector2 target, float alpha, Interpolation interpolation)
Description copied from interface:VectorInterpolates 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:
interpolatein interfaceVector<Vector2>- Parameters:
target- The target vectoralpha- The interpolation coefficientinterpolation- An Interpolation object describing the used interpolation method- Returns:
- This vector for chaining.
-
setToRandomDirection
public Vector2 setToRandomDirection()
Description copied from interface:VectorSets this vector to the unit vector with a random direction- Specified by:
setToRandomDirectionin interfaceVector<Vector2>- Returns:
- This vector for chaining
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
epsilonEquals
public boolean epsilonEquals(Vector2 other, float epsilon)
Description copied from interface:VectorCompares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.- Specified by:
epsilonEqualsin interfaceVector<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 comparey- y component of the other vector to compare- Returns:
- true if vector are equal, otherwise false
-
isUnit
public boolean isUnit()
-
isUnit
public boolean isUnit(float margin)
-
isZero
public boolean isZero()
-
isZero
public boolean isZero(float margin)
-
isOnLine
public boolean isOnLine(Vector2 other)
-
isOnLine
public boolean isOnLine(Vector2 other, float epsilon)
-
isCollinear
public boolean isCollinear(Vector2 other, float epsilon)
- Specified by:
isCollinearin interfaceVector<Vector2>- Returns:
- true if this vector is collinear with the other vector (
Vector.isOnLine(Vector, float)&&Vector.hasSameDirection(Vector)).
-
isCollinear
public boolean isCollinear(Vector2 other)
- Specified by:
isCollinearin interfaceVector<Vector2>- Returns:
- true if this vector is collinear with the other vector (
Vector.isOnLine(Vector)&&Vector.hasSameDirection(Vector)).
-
isCollinearOpposite
public boolean isCollinearOpposite(Vector2 other, float epsilon)
- Specified by:
isCollinearOppositein interfaceVector<Vector2>- Returns:
- true if this vector is opposite collinear with the other vector (
Vector.isOnLine(Vector, float)&&Vector.hasOppositeDirection(Vector)).
-
isCollinearOpposite
public boolean isCollinearOpposite(Vector2 other)
- Specified by:
isCollinearOppositein interfaceVector<Vector2>- Returns:
- true if this vector is opposite collinear with the other vector (
Vector.isOnLine(Vector)&&Vector.hasOppositeDirection(Vector)).
-
isPerpendicular
public boolean isPerpendicular(Vector2 vector)
- Specified by:
isPerpendicularin interfaceVector<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:
isPerpendicularin interfaceVector<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:
hasSameDirectionin interfaceVector<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:
hasOppositeDirectionin interfaceVector<Vector2>- Returns:
- Whether this vector has opposite direction compared to the other vector. True if the normalized dot product is < 0.
-
-