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 Vector2
add(float x, float y)
Adds the given components to this vectorVector2
add(Vector2 v)
Adds the given vector to this vectorfloat
angle()
Deprecated.useangleDeg()
instead.float
angle(Vector2 reference)
Deprecated.useangleDeg(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 valuesVector2
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 thanVector.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 testingboolean
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 testingboolean
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 thisVector2
to the value represented by the specified string according to the format oftoString()
.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 thanVector.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 matrixVector2
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.userotateDeg(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.userotateAroundDeg(Vector2, float)
instead.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 scalarVector2
scl(float x, float y)
Multiplies this vector by a scalarVector2
scl(Vector2 v)
Scales this vector by another vectorVector2
set(float x, float y)
Sets the components of this vectorVector2
set(Vector2 v)
Sets this vector from the given vectorVector2
setAngle(float degrees)
Deprecated.usesetAngleDeg(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 directionVector2
setZero()
Sets the components of this vector to 0Vector2
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 thisVector2
to 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:Vector
This 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:Vector
Sets 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:Vector
Subtracts 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:Vector
Normalizes this vector. Does nothing if it is zero.
-
add
public Vector2 add(Vector2 v)
Description copied from interface:Vector
Adds 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:Vector
Scales 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:Vector
Scales this vector by another vector
-
mulAdd
public Vector2 mulAdd(Vector2 vec, float scalar)
Description copied from interface:Vector
First scale a supplied vector, then add it to this 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.
-
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:Vector
This 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:Vector
Limits the length of this vector, based on the desired maximum length.
-
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 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:Vector
Clamps this vector's length to given min and max values
-
setLength
public Vector2 setLength(float len)
Description copied from interface:Vector
Sets the length of this vector. Does nothing if this vector is zero.
-
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 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 thisVector2
to a string in the format(x,y)
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation of this object.
-
fromString
public Vector2 fromString(java.lang.String v)
Sets thisVector2
to 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: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.
-
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 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:Vector
Sets this vector to the unit vector with a random direction- Specified by:
setToRandomDirection
in interfaceVector<Vector2>
- Returns:
- This vector for chaining
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.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 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:
isCollinear
in 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:
isCollinear
in 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:
isCollinearOpposite
in 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:
isCollinearOpposite
in 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:
isPerpendicular
in 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:
isPerpendicular
in 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:
hasSameDirection
in 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:
hasOppositeDirection
in interfaceVector<Vector2>
- Returns:
- Whether this vector has opposite direction compared to the other vector. True if the normalized dot product is < 0.
-
-