Package com.badlogic.gdx.math
Class Vector4
- java.lang.Object
-
- com.badlogic.gdx.math.Vector4
-
public class Vector4 extends java.lang.Object implements java.io.Serializable, Vector<Vector4>
Encapsulates a 4D vector. Allows chaining operations by returning a reference to itself in all modification methods.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description float
w
the w-component of this vectorstatic Vector4
W
float
x
the x-component of this vectorstatic Vector4
X
float
y
the y-component of this vectorstatic Vector4
Y
float
z
the z-component of this vectorstatic Vector4
Z
static Vector4
Zero
-
Constructor Summary
Constructors Constructor Description Vector4()
Constructs a vector at (0,0,0,0)Vector4(float[] values)
Creates a vector from the given array.Vector4(float x, float y, float z, float w)
Creates a vector with the given componentsVector4(Vector2 vector, float z, float w)
Creates a vector from the given Vector2 and z- and w-componentsVector4(Vector3 vector, float w)
Creates a vector from the given Vector3 and w-componentVector4(Vector4 vector)
Creates a vector from the given Vector4
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Vector4
add(float values)
Adds the given value to all four components of the vector.Vector4
add(float x, float y, float z, float w)
Adds the given components to this vectorVector4
add(Vector4 vector)
Adds the given vector to this vectorVector4
clamp(float min, float max)
Clamps this vector's length to given min and max valuesVector4
cpy()
float
dot(float x, float y, float z, float w)
Returns the dot product between this and the given vector (given as 4 components).static float
dot(float x1, float y1, float z1, float w1, float x2, float y2, float z2, float w2)
float
dot(Vector4 vector)
float
dst(float x, float y, float z, float w)
static float
dst(float x1, float y1, float z1, float w1, float x2, float y2, float z2, float w2)
float
dst(Vector4 vector)
float
dst2(float x, float y, float z, float w)
Returns the squared distance between this point and the given pointstatic float
dst2(float x1, float y1, float z1, float w1, float x2, float y2, float z2, float w2)
float
dst2(Vector4 point)
This method is faster thanVector.dst(Vector)
because it avoids calculating a square root.boolean
epsilonEquals(float x, float y, float z, float w)
Compares this vector with the other vector usingMathUtils.FLOAT_ROUNDING_ERROR
for its epsilon.boolean
epsilonEquals(float x, float y, float z, float w, float epsilon)
Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.boolean
epsilonEquals(Vector4 other)
Compares this vector with the other vector usingMathUtils.FLOAT_ROUNDING_ERROR
for its epsilon.boolean
epsilonEquals(Vector4 other, float epsilon)
Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.boolean
equals(java.lang.Object obj)
Vector4
fromString(java.lang.String v)
Sets thisVector4
to the value represented by the specified string according to the format oftoString()
.int
hashCode()
boolean
hasOppositeDirection(Vector4 vector)
boolean
hasSameDirection(Vector4 vector)
boolean
idt(Vector4 vector)
Returns true if this vector and the vector parameter have identical components.Vector4
interpolate(Vector4 target, float alpha, Interpolation interpolator)
Interpolates between this vector and the given target vector by alpha (within range [0,1]) using the given Interpolation method.boolean
isCollinear(Vector4 other)
boolean
isCollinear(Vector4 other, float epsilon)
boolean
isCollinearOpposite(Vector4 other)
boolean
isCollinearOpposite(Vector4 other, float epsilon)
boolean
isOnLine(Vector4 other)
boolean
isOnLine(Vector4 other, float epsilon)
boolean
isPerpendicular(Vector4 vector)
boolean
isPerpendicular(Vector4 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 z, float w)
float
len2()
This method is faster thanVector.len()
because it avoids calculating a square root.static float
len2(float x, float y, float z, float w)
Vector4
lerp(Vector4 target, float alpha)
Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1].Vector4
limit(float limit)
Limits the length of this vector, based on the desired maximum length.Vector4
limit2(float limit2)
Limits the length of this vector, based on the desired maximum length squared.Vector4
mulAdd(Vector4 vec, float scalar)
First scale a supplied vector, then add it to this vector.Vector4
mulAdd(Vector4 vec, Vector4 mulVec)
First scale a supplied vector, then add it to this vector.Vector4
nor()
Normalizes this vector.Vector4
scl(float scalar)
Multiplies each component of this vector by the given scalarVector4
scl(float vx, float vy, float vz, float vw)
Scales this vector by the given valuesVector4
scl(Vector4 other)
Multiplies each component of this vector by the corresponding component in otherVector4
set(float[] values)
Sets the components from the array.Vector4
set(float x, float y, float z, float w)
Sets the vector to the given componentsVector4
set(Vector2 vector, float z, float w)
Sets the components to the given Vector2, z-component and w-componentVector4
set(Vector3 vector, float w)
Sets the components of the given vector3 and w-componentVector4
set(Vector4 vector)
Sets this vector from the given vectorVector4
setLength(float len)
Sets the length of this vector.Vector4
setLength2(float len2)
Sets the length of this vector, based on the square of the desired length.Vector4
setToRandomDirection()
Sets this vector to the unit vector with a random directionVector4
setZero()
Sets the components of this vector to 0Vector4
sub(float value)
Subtracts the given value from all components of this vectorVector4
sub(float x, float y, float z, float w)
Subtracts the given components from this vector.Vector4
sub(Vector4 a_vec)
Subtracts the given vector from this vector.java.lang.String
toString()
Converts thisVector4
to a string in the format(x,y,z,w)
.
-
-
-
Field Detail
-
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
-
w
public float w
the w-component of this vector
-
X
public static final Vector4 X
-
Y
public static final Vector4 Y
-
Z
public static final Vector4 Z
-
W
public static final Vector4 W
-
Zero
public static final Vector4 Zero
-
-
Constructor Detail
-
Vector4
public Vector4()
Constructs a vector at (0,0,0,0)
-
Vector4
public Vector4(float x, float y, float z, float w)
Creates a vector with the given components- Parameters:
x
- The x-componenty
- The y-componentz
- The z-componentw
- The w-component *
-
Vector4
public Vector4(Vector4 vector)
Creates a vector from the given Vector4- Parameters:
vector
- The vector
-
Vector4
public Vector4(float[] values)
Creates a vector from the given array. The array must have at least 4 elements.- Parameters:
values
- The array
-
Vector4
public Vector4(Vector2 vector, float z, float w)
Creates a vector from the given Vector2 and z- and w-components- Parameters:
vector
- The vectorz
- The z-componentw
- The w-component
-
Vector4
public Vector4(Vector3 vector, float w)
Creates a vector from the given Vector3 and w-component- Parameters:
vector
- The vectorw
- The w-component
-
-
Method Detail
-
set
public Vector4 set(float x, float y, float z, float w)
Sets the vector to the given components- Parameters:
x
- The x-componenty
- The y-componentz
- The z-componentw
- The w-component- Returns:
- this vector for chaining
-
set
public Vector4 set(Vector4 vector)
Description copied from interface:Vector
Sets this vector from the given vector
-
set
public Vector4 set(float[] values)
Sets the components from the array. The array must have at least 4 elements- Parameters:
values
- The array- Returns:
- this vector for chaining
-
set
public Vector4 set(Vector2 vector, float z, float w)
Sets the components to the given Vector2, z-component and w-component- Parameters:
vector
- The vector2 holding the x- and y-componentsz
- The z-componentw
- The w-component- Returns:
- This vector for chaining
-
set
public Vector4 set(Vector3 vector, float w)
Sets the components of the given vector3 and w-component- Parameters:
vector
- The vectorw
- The w-component- Returns:
- This vector for chaining
-
setToRandomDirection
public Vector4 setToRandomDirection()
Description copied from interface:Vector
Sets this vector to the unit vector with a random direction- Specified by:
setToRandomDirection
in interfaceVector<Vector4>
- Returns:
- This vector for chaining
-
cpy
public Vector4 cpy()
-
add
public Vector4 add(Vector4 vector)
Description copied from interface:Vector
Adds the given vector to this vector
-
add
public Vector4 add(float x, float y, float z, float w)
Adds the given components to this vector- Parameters:
x
- Added to the x-componenty
- Added to the y-componentz
- Added to the z-componentw
- Added to the w-component- Returns:
- This vector for chaining.
-
add
public Vector4 add(float values)
Adds the given value to all four components of the vector.- Parameters:
values
- The value- Returns:
- This vector for chaining
-
sub
public Vector4 sub(Vector4 a_vec)
Description copied from interface:Vector
Subtracts the given vector from this vector.
-
sub
public Vector4 sub(float x, float y, float z, float w)
Subtracts the given components from this vector.- Parameters:
x
- Subtracted from the x-componenty
- Subtracted from the y-componentz
- Subtracted from the z-componentw
- Subtracted from the w-component- Returns:
- This vector for chaining
-
sub
public Vector4 sub(float value)
Subtracts the given value from all components of this vector- Parameters:
value
- The value- Returns:
- This vector for chaining
-
scl
public Vector4 scl(float scalar)
Multiplies each component of this vector by the given scalar
-
scl
public Vector4 scl(Vector4 other)
Multiplies each component of this vector by the corresponding component in other
-
scl
public Vector4 scl(float vx, float vy, float vz, float vw)
Scales this vector by the given values- Parameters:
vx
- Multiplied with the X valuevy
- Multiplied with the Y valuevz
- Multiplied with the Z valuevw
- Multiplied with the W value- Returns:
- This vector for chaining
-
mulAdd
public Vector4 mulAdd(Vector4 vec, float scalar)
Description copied from interface:Vector
First scale a supplied vector, then add it to this vector.
-
mulAdd
public Vector4 mulAdd(Vector4 vec, Vector4 mulVec)
Description copied from interface:Vector
First scale a supplied vector, then add it to this vector.
-
len
public static float len(float x, float y, float z, float w)
- Returns:
- The Euclidean length
-
len2
public static float len2(float x, float y, float z, float w)
- Returns:
- The squared Euclidean length
-
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.
-
idt
public boolean idt(Vector4 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 z1, float w1, float x2, float y2, float z2, float w2)
- Returns:
- The Euclidean distance between the two specified vectors
-
dst
public float dst(Vector4 vector)
-
dst
public float dst(float x, float y, float z, float w)
- Returns:
- the distance between this point and the given point
-
dst2
public static float dst2(float x1, float y1, float z1, float w1, float x2, float y2, float z2, float w2)
- Returns:
- the squared distance between the given points
-
dst2
public float dst2(Vector4 point)
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, float z, float w)
Returns the squared distance between this point and the given point- Parameters:
x
- The x-component of the other pointy
- The y-component of the other pointz
- The z-component of the other pointw
- The w-component of the other point- Returns:
- The squared distance
-
nor
public Vector4 nor()
Description copied from interface:Vector
Normalizes this vector. Does nothing if it is zero.
-
dot
public static float dot(float x1, float y1, float z1, float w1, float x2, float y2, float z2, float w2)
- Returns:
- The dot product between the two vectors
-
dot
public float dot(Vector4 vector)
-
dot
public float dot(float x, float y, float z, float w)
Returns the dot product between this and the given vector (given as 4 components).- Parameters:
x
- The x-component of the other vectory
- The y-component of the other vectorz
- The z-component of the other vectorw
- The w-component of the other vector- Returns:
- The dot product
-
isUnit
public boolean isUnit()
-
isUnit
public boolean isUnit(float margin)
-
isZero
public boolean isZero()
-
isZero
public boolean isZero(float margin)
-
isOnLine
public boolean isOnLine(Vector4 other, float epsilon)
-
isOnLine
public boolean isOnLine(Vector4 other)
-
isCollinear
public boolean isCollinear(Vector4 other, float epsilon)
- Specified by:
isCollinear
in interfaceVector<Vector4>
- Returns:
- true if this vector is collinear with the other vector (
isOnLine(Vector4, float)
&&hasSameDirection(Vector4)
).
-
isCollinear
public boolean isCollinear(Vector4 other)
- Specified by:
isCollinear
in interfaceVector<Vector4>
- Returns:
- true if this vector is collinear with the other vector (
isOnLine(Vector4)
&&hasSameDirection(Vector4)
).
-
isCollinearOpposite
public boolean isCollinearOpposite(Vector4 other, float epsilon)
- Specified by:
isCollinearOpposite
in interfaceVector<Vector4>
- Returns:
- true if this vector is collinear with the other vector (
isOnLine(Vector4, float)
&&hasSameDirection(Vector4)
).
-
isCollinearOpposite
public boolean isCollinearOpposite(Vector4 other)
- Specified by:
isCollinearOpposite
in interfaceVector<Vector4>
- Returns:
- true if this vector is collinear with the other vector (
isOnLine(Vector4)
&&hasSameDirection(Vector4)
).
-
isPerpendicular
public boolean isPerpendicular(Vector4 vector)
- Specified by:
isPerpendicular
in interfaceVector<Vector4>
- Returns:
- Whether this vector is perpendicular with the other vector. True if the dot product is 0.
-
isPerpendicular
public boolean isPerpendicular(Vector4 vector, float epsilon)
- Specified by:
isPerpendicular
in interfaceVector<Vector4>
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(Vector4 vector)
- Specified by:
hasSameDirection
in interfaceVector<Vector4>
- Returns:
- Whether this vector has similar direction compared to the other vector. True if the normalized dot product is > 0.
-
hasOppositeDirection
public boolean hasOppositeDirection(Vector4 vector)
- Specified by:
hasOppositeDirection
in interfaceVector<Vector4>
- Returns:
- Whether this vector has opposite direction compared to the other vector. True if the normalized dot product is < 0.
-
lerp
public Vector4 lerp(Vector4 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 Vector4 interpolate(Vector4 target, float alpha, Interpolation 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 interfaceVector<Vector4>
- Parameters:
target
- The target vectoralpha
- The interpolation coefficientinterpolator
- An Interpolation object describing the used interpolation method- Returns:
- This vector for chaining.
-
toString
public java.lang.String toString()
Converts thisVector4
to a string in the format(x,y,z,w)
. Strings with this exact format can be parsed withfromString(String)
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation of this object.
-
fromString
public Vector4 fromString(java.lang.String v)
Sets thisVector4
to the value represented by the specified string according to the format oftoString()
.- Parameters:
v
- the string.- Returns:
- this vector, set with the value from v, for chaining
-
limit
public Vector4 limit(float limit)
Description copied from interface:Vector
Limits the length of this vector, based on the desired maximum length.
-
limit2
public Vector4 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<Vector4>
- Parameters:
limit2
- squared desired maximum length for this vector- Returns:
- this vector for chaining
- See Also:
Vector.len2()
-
setLength
public Vector4 setLength(float len)
Description copied from interface:Vector
Sets the length of this vector. Does nothing if this vector is zero.
-
setLength2
public Vector4 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<Vector4>
- Parameters:
len2
- desired square of the length for this vector- Returns:
- this vector for chaining
- See Also:
Vector.len2()
-
clamp
public Vector4 clamp(float min, float max)
Description copied from interface:Vector
Clamps this vector's length to given min and max values
-
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(Vector4 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<Vector4>
- Returns:
- whether the vectors have fuzzy equality.
-
epsilonEquals
public boolean epsilonEquals(float x, float y, float z, float w, float epsilon)
Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.- Parameters:
x
- x component of the other vector to comparey
- y component of the other vector to comparez
- z component of the other vector to comparew
- w component of the other vector to compareepsilon
- how much error to tolerate and still consider two floats equal- Returns:
- whether the vectors are the same.
-
epsilonEquals
public boolean epsilonEquals(Vector4 other)
Compares this vector with the other vector usingMathUtils.FLOAT_ROUNDING_ERROR
for its epsilon.- Parameters:
other
- other vector to compare- Returns:
- true if the vectors are equal, otherwise false
-
epsilonEquals
public boolean epsilonEquals(float x, float y, float z, float w)
Compares this vector with the other vector usingMathUtils.FLOAT_ROUNDING_ERROR
for its epsilon.- Parameters:
x
- x component of the other vector to comparey
- y component of the other vector to comparez
- z component of the other vector to comparew
- w component of the other vector to compare- Returns:
- true if the vectors are equal, otherwise false
-
-