Package com.badlogic.gdx.math
Class Vector3
- java.lang.Object
-
- com.badlogic.gdx.math.Vector3
-
public class Vector3 extends java.lang.Object implements java.io.Serializable, Vector<Vector3>
Encapsulates a 3D vector. Allows chaining operations by returning a reference to itself in all modification methods.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Vector3()
Constructs a vector at (0,0,0)Vector3(float[] values)
Creates a vector from the given array.Vector3(float x, float y, float z)
Creates a vector with the given componentsVector3(Vector2 vector, float z)
Creates a vector from the given vector and z-componentVector3(Vector3 vector)
Creates a vector from the given vector
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Vector3
add(float values)
Adds the given value to all three components of the vector.Vector3
add(float x, float y, float z)
Adds the given vector to this componentVector3
add(Vector3 vector)
Adds the given vector to this vectorVector3
clamp(float min, float max)
Clamps this vector's length to given min and max valuesVector3
cpy()
Vector3
crs(float x, float y, float z)
Sets this vector to the cross product between it and the other vector.Vector3
crs(Vector3 vector)
Sets this vector to the cross product between it and the other vector.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(Vector3 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(Vector3 vector)
float
dst2(float x, float y, float z)
Returns the squared distance between this point and the given pointstatic float
dst2(float x1, float y1, float z1, float x2, float y2, float z2)
float
dst2(Vector3 point)
This method is faster thanVector.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 MathUtils.FLOAT_ROUNDING_ERROR for fuzzy equality testingboolean
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
epsilonEquals(Vector3 other)
Compares this vector with the other vector using MathUtils.FLOAT_ROUNDING_ERROR for fuzzy equality testingboolean
epsilonEquals(Vector3 other, float epsilon)
Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.boolean
equals(java.lang.Object obj)
Vector3
fromString(java.lang.String v)
Sets thisVector3
to the value represented by the specified string according to the format oftoString()
.int
hashCode()
boolean
hasOppositeDirection(Vector3 vector)
boolean
hasSameDirection(Vector3 vector)
boolean
idt(Vector3 vector)
Returns true if this vector and the vector parameter have identical components.Vector3
interpolate(Vector3 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(Vector3 other)
boolean
isCollinear(Vector3 other, float epsilon)
boolean
isCollinearOpposite(Vector3 other)
boolean
isCollinearOpposite(Vector3 other, float epsilon)
boolean
isOnLine(Vector3 other)
boolean
isOnLine(Vector3 other, float epsilon)
boolean
isPerpendicular(Vector3 vector)
boolean
isPerpendicular(Vector3 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
len2()
This method is faster thanVector.len()
because it avoids calculating a square root.static float
len2(float x, float y, float z)
Vector3
lerp(Vector3 target, float alpha)
Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1].Vector3
limit(float limit)
Limits the length of this vector, based on the desired maximum length.Vector3
limit2(float limit2)
Limits the length of this vector, based on the desired maximum length squared.Vector3
mul(Matrix3 matrix)
Left-multiplies the vector by the given matrix.Vector3
mul(Matrix4 matrix)
Left-multiplies the vector by the given matrix, assuming the fourth (w) component of the vector is 1.Vector3
mul(Quaternion quat)
Multiplies the vector by the givenQuaternion
.Vector3
mul4x3(float[] matrix)
Left-multiplies the vector by the given 4x3 column major matrix.Vector3
mulAdd(Vector3 vec, float scalar)
First scale a supplied vector, then add it to this vector.Vector3
mulAdd(Vector3 vec, Vector3 mulVec)
First scale a supplied vector, then add it to this vector.Vector3
nor()
Normalizes this vector.Vector3
prj(Matrix4 matrix)
Multiplies this vector by the given matrix dividing by w, assuming the fourth (w) component of the vector is 1.Vector3
rot(Matrix4 matrix)
Multiplies this vector by the first three columns of the matrix, essentially only applying rotation and scaling.Vector3
rotate(float degrees, float axisX, float axisY, float axisZ)
Rotates this vector by the given angle in degrees around the given axis.Vector3
rotate(Vector3 axis, float degrees)
Rotates this vector by the given angle in degrees around the given axis.Vector3
rotateRad(float radians, float axisX, float axisY, float axisZ)
Rotates this vector by the given angle in radians around the given axis.Vector3
rotateRad(Vector3 axis, float radians)
Rotates this vector by the given angle in radians around the given axis.Vector3
scl(float scalar)
Scales this vector by a scalarVector3
scl(float vx, float vy, float vz)
Scales this vector by the given valuesVector3
scl(Vector3 other)
Scales this vector by another vectorVector3
set(float[] values)
Sets the components from the array.Vector3
set(float x, float y, float z)
Sets the vector to the given componentsVector3
set(Vector2 vector, float z)
Sets the components of the given vector and z-componentVector3
set(Vector3 vector)
Sets this vector from the given vectorVector3
setFromSpherical(float azimuthalAngle, float polarAngle)
Sets the components from the given spherical coordinateVector3
setLength(float len)
Sets the length of this vector.Vector3
setLength2(float len2)
Sets the length of this vector, based on the square of the desired length.Vector3
setToRandomDirection()
Sets this vector to the unit vector with a random directionVector3
setZero()
Sets the components of this vector to 0Vector3
slerp(Vector3 target, float alpha)
Spherically interpolates between this vector and the target vector by alpha which is in the range [0,1].Vector3
sub(float value)
Subtracts the given value from all components of this vectorVector3
sub(float x, float y, float z)
Subtracts the other vector from this vector.Vector3
sub(Vector3 a_vec)
Subtracts the given vector from this vector.java.lang.String
toString()
Converts thisVector3
to a string in the format(x,y,z)
.Vector3
traMul(Matrix3 matrix)
Multiplies the vector by the transpose of the given matrix.Vector3
traMul(Matrix4 matrix)
Multiplies the vector by the transpose of the given matrix, assuming the fourth (w) component of the vector is 1.Vector3
unrotate(Matrix4 matrix)
Multiplies this vector by the transpose of the first three columns of the matrix.Vector3
untransform(Matrix4 matrix)
Translates this vector in the direction opposite to the translation of the matrix and the multiplies this vector by the transpose of the first three columns of the matrix.
-
-
-
Constructor Detail
-
Vector3
public Vector3()
Constructs a vector at (0,0,0)
-
Vector3
public Vector3(float x, float y, float z)
Creates a vector with the given components- Parameters:
x
- The x-componenty
- The y-componentz
- The z-component
-
Vector3
public Vector3(Vector3 vector)
Creates a vector from the given vector- Parameters:
vector
- The vector
-
Vector3
public Vector3(float[] values)
Creates a vector from the given array. The array must have at least 3 elements.- Parameters:
values
- The array
-
Vector3
public Vector3(Vector2 vector, float z)
Creates a vector from the given vector and z-component- Parameters:
vector
- The vectorz
- The z-component
-
-
Method Detail
-
set
public Vector3 set(float x, float y, float z)
Sets the vector to the given components- Parameters:
x
- The x-componenty
- The y-componentz
- The z-component- Returns:
- this vector for chaining
-
set
public Vector3 set(Vector3 vector)
Description copied from interface:Vector
Sets this vector from the given vector
-
set
public Vector3 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 Vector3 set(Vector2 vector, float z)
Sets the components of the given vector and z-component- Parameters:
vector
- The vectorz
- The z-component- Returns:
- This vector for chaining
-
setFromSpherical
public Vector3 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 Vector3 setToRandomDirection()
Description copied from interface:Vector
Sets this vector to the unit vector with a random direction- Specified by:
setToRandomDirection
in interfaceVector<Vector3>
- Returns:
- This vector for chaining
-
cpy
public Vector3 cpy()
-
add
public Vector3 add(Vector3 vector)
Description copied from interface:Vector
Adds the given vector to this vector
-
add
public Vector3 add(float x, float y, float z)
Adds the given vector to this component- Parameters:
x
- The x-component of the other vectory
- The y-component of the other vectorz
- The z-component of the other vector- Returns:
- This vector for chaining.
-
add
public Vector3 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 Vector3 sub(Vector3 a_vec)
Description copied from interface:Vector
Subtracts the given vector from this vector.
-
sub
public Vector3 sub(float x, float y, float z)
Subtracts the other vector from this vector.- Parameters:
x
- The x-component of the other vectory
- The y-component of the other vectorz
- The z-component of the other vector- Returns:
- This vector for chaining
-
sub
public Vector3 sub(float value)
Subtracts the given value from all components of this vector- Parameters:
value
- The value- Returns:
- This vector for chaining
-
scl
public Vector3 scl(float scalar)
Description copied from interface:Vector
Scales this vector by a scalar
-
scl
public Vector3 scl(Vector3 other)
Description copied from interface:Vector
Scales this vector by another vector
-
scl
public Vector3 scl(float vx, float vy, float vz)
Scales this vector by the given values- Parameters:
vx
- X valuevy
- Y valuevz
- Z value- Returns:
- This vector for chaining
-
mulAdd
public Vector3 mulAdd(Vector3 vec, float scalar)
Description copied from interface:Vector
First scale a supplied vector, then add it to this vector.
-
mulAdd
public Vector3 mulAdd(Vector3 vec, Vector3 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)
- Returns:
- The Euclidean length
-
len2
public static float len2(float x, float y, float z)
- 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(Vector3 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 x2, float y2, float z2)
- Returns:
- The Euclidean distance between the two specified vectors
-
dst
public float dst(Vector3 vector)
-
dst
public float dst(float x, float y, float z)
- Returns:
- the distance between this point and the given point
-
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
-
dst2
public float dst2(Vector3 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)
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 point- Returns:
- The squared distance
-
nor
public Vector3 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 x2, float y2, float z2)
- Returns:
- The dot product between the two vectors
-
dot
public float dot(Vector3 vector)
-
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 vectory
- The y-component of the other vectorz
- The z-component of the other vector- Returns:
- The dot product
-
crs
public Vector3 crs(Vector3 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 Vector3 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 vectory
- The y-component of the other vectorz
- The z-component of the other vector- Returns:
- This vector for chaining
-
mul4x3
public Vector3 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 Vector3 mul(Matrix4 matrix)
Left-multiplies the vector by the given matrix, assuming the fourth (w) component of the vector is 1.- Parameters:
matrix
- The matrix- Returns:
- This vector for chaining
-
traMul
public Vector3 traMul(Matrix4 matrix)
Multiplies the vector by the transpose of the given matrix, assuming the fourth (w) component of the vector is 1.- Parameters:
matrix
- The matrix- Returns:
- This vector for chaining
-
mul
public Vector3 mul(Matrix3 matrix)
Left-multiplies the vector by the given matrix.- Parameters:
matrix
- The matrix- Returns:
- This vector for chaining
-
traMul
public Vector3 traMul(Matrix3 matrix)
Multiplies the vector by the transpose of the given matrix.- Parameters:
matrix
- The matrix- Returns:
- This vector for chaining
-
mul
public Vector3 mul(Quaternion quat)
Multiplies the vector by the givenQuaternion
.- Returns:
- This vector for chaining
-
prj
public Vector3 prj(Matrix4 matrix)
Multiplies this vector by the given matrix dividing by w, assuming the fourth (w) component of the vector is 1. This is mostly used to project/unproject vectors via a perspective projection matrix.- Parameters:
matrix
- The matrix.- Returns:
- This vector for chaining
-
rot
public Vector3 rot(Matrix4 matrix)
Multiplies this vector by the first three columns of the matrix, essentially only applying rotation and scaling.- Parameters:
matrix
- The matrix- Returns:
- This vector for chaining
-
unrotate
public Vector3 unrotate(Matrix4 matrix)
Multiplies this vector by the transpose of the first three columns of the matrix. Note: only works for translation and rotation, does not work for scaling. For those, userot(Matrix4)
withMatrix4.inv()
.- Parameters:
matrix
- The transformation matrix- Returns:
- The vector for chaining
-
untransform
public Vector3 untransform(Matrix4 matrix)
Translates this vector in the direction opposite to the translation of the matrix and the multiplies this vector by the transpose of the first three columns of the matrix. Note: only works for translation and rotation, does not work for scaling. For those, usemul(Matrix4)
withMatrix4.inv()
.- Parameters:
matrix
- The transformation matrix- Returns:
- The vector for chaining
-
rotate
public Vector3 rotate(float degrees, float axisX, float axisY, float axisZ)
Rotates this vector by the given angle in degrees around the given axis.- Parameters:
degrees
- the angle in degreesaxisX
- the x-component of the axisaxisY
- the y-component of the axisaxisZ
- the z-component of the axis- Returns:
- This vector for chaining
-
rotateRad
public Vector3 rotateRad(float radians, float axisX, float axisY, float axisZ)
Rotates this vector by the given angle in radians around the given axis.- Parameters:
radians
- the angle in radiansaxisX
- the x-component of the axisaxisY
- the y-component of the axisaxisZ
- the z-component of the axis- Returns:
- This vector for chaining
-
rotate
public Vector3 rotate(Vector3 axis, float degrees)
Rotates this vector by the given angle in degrees around the given axis.- Parameters:
axis
- the axisdegrees
- the angle in degrees- Returns:
- This vector for chaining
-
rotateRad
public Vector3 rotateRad(Vector3 axis, float radians)
Rotates this vector by the given angle in radians around the given axis.- Parameters:
axis
- the axisradians
- the angle in radians- Returns:
- This vector for chaining
-
isUnit
public boolean isUnit()
-
isUnit
public boolean isUnit(float margin)
-
isZero
public boolean isZero()
-
isZero
public boolean isZero(float margin)
-
isOnLine
public boolean isOnLine(Vector3 other, float epsilon)
-
isOnLine
public boolean isOnLine(Vector3 other)
-
isCollinear
public boolean isCollinear(Vector3 other, float epsilon)
- Specified by:
isCollinear
in interfaceVector<Vector3>
- Returns:
- true if this vector is collinear with the other vector (
Vector.isOnLine(Vector, float)
&&Vector.hasSameDirection(Vector)
).
-
isCollinear
public boolean isCollinear(Vector3 other)
- Specified by:
isCollinear
in interfaceVector<Vector3>
- Returns:
- true if this vector is collinear with the other vector (
Vector.isOnLine(Vector)
&&Vector.hasSameDirection(Vector)
).
-
isCollinearOpposite
public boolean isCollinearOpposite(Vector3 other, float epsilon)
- Specified by:
isCollinearOpposite
in interfaceVector<Vector3>
- Returns:
- true if this vector is opposite collinear with the other vector (
Vector.isOnLine(Vector, float)
&&Vector.hasOppositeDirection(Vector)
).
-
isCollinearOpposite
public boolean isCollinearOpposite(Vector3 other)
- Specified by:
isCollinearOpposite
in interfaceVector<Vector3>
- Returns:
- true if this vector is opposite collinear with the other vector (
Vector.isOnLine(Vector)
&&Vector.hasOppositeDirection(Vector)
).
-
isPerpendicular
public boolean isPerpendicular(Vector3 vector)
- Specified by:
isPerpendicular
in interfaceVector<Vector3>
- Returns:
- Whether this vector is perpendicular with the other vector. True if the dot product is 0.
-
isPerpendicular
public boolean isPerpendicular(Vector3 vector, float epsilon)
- Specified by:
isPerpendicular
in interfaceVector<Vector3>
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(Vector3 vector)
- Specified by:
hasSameDirection
in interfaceVector<Vector3>
- Returns:
- Whether this vector has similar direction compared to the other vector. True if the normalized dot product is > 0.
-
hasOppositeDirection
public boolean hasOppositeDirection(Vector3 vector)
- Specified by:
hasOppositeDirection
in interfaceVector<Vector3>
- Returns:
- Whether this vector has opposite direction compared to the other vector. True if the normalized dot product is < 0.
-
lerp
public Vector3 lerp(Vector3 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 Vector3 interpolate(Vector3 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<Vector3>
- Parameters:
target
- The target vectoralpha
- The interpolation coefficientinterpolator
- An Interpolation object describing the used interpolation method- Returns:
- This vector for chaining.
-
slerp
public Vector3 slerp(Vector3 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 vectoralpha
- The interpolation coefficient- Returns:
- This vector for chaining.
-
toString
public java.lang.String toString()
Converts thisVector3
to a string in the format(x,y,z)
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation of this object.
-
fromString
public Vector3 fromString(java.lang.String v)
Sets thisVector3
to the value represented by the specified string according to the format oftoString()
.- Parameters:
v
- the string.- Returns:
- this vector for chaining
-
limit
public Vector3 limit(float limit)
Description copied from interface:Vector
Limits the length of this vector, based on the desired maximum length.
-
limit2
public Vector3 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<Vector3>
- Parameters:
limit2
- squared desired maximum length for this vector- Returns:
- this vector for chaining
- See Also:
Vector.len2()
-
setLength
public Vector3 setLength(float len)
Description copied from interface:Vector
Sets the length of this vector. Does nothing if this vector is zero.
-
setLength2
public Vector3 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<Vector3>
- Parameters:
len2
- desired square of the length for this vector- Returns:
- this vector for chaining
- See Also:
Vector.len2()
-
clamp
public Vector3 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(Vector3 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<Vector3>
- 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(Vector3 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, float z)
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 comparez
- z component of the other vector to compare- Returns:
- true if vector are equal, otherwise false
-
-