Package com.ardor3d.math
Class Vector3
java.lang.Object
com.ardor3d.math.Vector3
- All Implemented Interfaces:
Poolable
,ReadOnlyVector3
,Savable
,Externalizable
,Serializable
,Cloneable
public class Vector3
extends Object
implements Cloneable, Savable, Externalizable, ReadOnlyVector3, Poolable
Vector3 represents a point or vector in a three dimensional system. This implementation stores its data in
double-precision.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprotected double
protected double
protected double
static final ReadOnlyVector3
-1, -1, -1static final ReadOnlyVector3
-1, 0, 0static final ReadOnlyVector3
0, -1, 0static final ReadOnlyVector3
0, 0, -1static final ReadOnlyVector3
1, 1, 1static final ReadOnlyVector3
1, 0, 0static final ReadOnlyVector3
0, 1, 0static final ReadOnlyVector3
0, 0, 1static final ReadOnlyVector3
0, 0, 0 -
Constructor Summary
ConstructorDescriptionVector3()
Constructs a new vector set to (0, 0, 0).Vector3
(double x, double y, double z) Constructs a new vector set to (x, y, z).Vector3
(ReadOnlyVector3 src) Constructs a new vector set to the (x, y, z) values of the given source vector. -
Method Summary
Modifier and TypeMethodDescriptionAdds the given values to those of this vector and returns them in store * @param store the vector to store the result in for return.add
(ReadOnlyVector3 source, Vector3 store) Adds the values of the given source vector to those of this vector and returns them in store.addLocal
(double x, double y, double z) Increments the values of this vector with the given x, y and z values.addLocal
(ReadOnlyVector3 source) Increments the values of this vector with the x, y and z values of the given vector.clone()
cross
(ReadOnlyVector3 vec, Vector3 store) crossLocal
(double x, double y, double z) double
distance
(double x, double y, double z) double
distance
(ReadOnlyVector3 destination) double
distanceSquared
(double x, double y, double z) double
distanceSquared
(ReadOnlyVector3 destination) Divides the values of this vector by the given scale values and returns the result in store.Divides the values of this vector by the given scalar value and returns the result in store.divide
(ReadOnlyVector3 scale, Vector3 store) Divides the values of this vector by the given scale values and returns the result in store.divideLocal
(double scalar) Internally modifies the values of this vector by dividing them each by the given scalar value.divideLocal
(double x, double y, double z) Internally modifies the values of this vector by dividing them each by the given scale values.divideLocal
(ReadOnlyVector3 scale) Internally modifies the values of this vector by dividing them each by the given scale values.double
dot
(double x, double y, double z) double
dot
(ReadOnlyVector3 vec) boolean
Compares this object against the specified objectstatic final Vector3
double
getValue
(int index) double
getX()
float
getXf()
double
getY()
float
getYf()
double
getZ()
float
getZf()
int
hashCode()
static boolean
isInfinite
(ReadOnlyVector3 vector) Check if a vector is non-null and has infinite values.static boolean
isValid
(ReadOnlyVector3 vector) Check a vector... if it is null or its doubles are NaN or infinite, return false.double
length()
double
lerp
(ReadOnlyVector3 endVec, double scalar, Vector3 store) Performs a linear interpolation between this vector and the given end vector, using the given scalar as a percent. iow, if changeAmnt is closer to 0, the result will be closer to the current value of this vector and if it is closer to 1, the result will be closer to the end value.static Vector3
lerp
(ReadOnlyVector3 beginVec, ReadOnlyVector3 endVec, double scalar, Vector3 store) Performs a linear interpolation between the given begin and end vectors, using the given scalar as a percent.lerpLocal
(ReadOnlyVector3 endVec, double scalar) Performs a linear interpolation between this vector and the given end vector, using the given scalar as a percent. iow, if changeAmnt is closer to 0, the result will be closer to the current value of this vector and if it is closer to 1, the result will be closer to the end value.lerpLocal
(ReadOnlyVector3 beginVec, ReadOnlyVector3 endVec, double scalar) Performs a linear interpolation between the given begin and end vectors, using the given scalar as a percent.Multiplies the values of this vector by the given scale values and returns the result in store.Multiplies the values of this vector by the given scalar value and returns the result in store.multiply
(ReadOnlyVector3 scale, Vector3 store) Multiplies the values of this vector by the given scale values and returns the result in store.multiplyLocal
(double scalar) Internally modifies the values of this vector by multiplying them each by the given scalar value.multiplyLocal
(double x, double y, double z) Internally modifies the values of this vector by multiplying them each by the given scale values.multiplyLocal
(ReadOnlyVector3 scale) Internally modifies the values of this vector by multiplying them each by the given scale values.Creates a new unit length vector from this one by dividing by length.Converts this vector into a unit vector by dividing it internally by its length.void
read
(InputCapsule capsule) void
Used with serialization.static final void
Releases a Vector3 back to be used by a future call to fetchTempInstance.scaleAdd
(double scale, ReadOnlyVector3 add, Vector3 store) Scales this vector by multiplying its values with a given scale value, then adding a given "add" value.scaleAddLocal
(double scale, ReadOnlyVector3 add) Internally modifies this vector by multiplying its values with a given scale value, then adding a given "add" value.set
(double x, double y, double z) Sets the value of this vector to (x, y, z)set
(ReadOnlyVector3 source) Sets the value of this vector to the (x, y, z) values of the provided source vector.void
setValue
(int index, double value) void
setX
(double x) Sets the first component of this vector to the given double value.void
setY
(double y) Sets the second component of this vector to the given double value.void
setZ
(double z) Sets the third component of this vector to the given double value.double
smallestAngleBetween
(ReadOnlyVector3 otherVector) Subtracts the given values from those of this vector and returns them in store.subtract
(ReadOnlyVector3 source, Vector3 store) Subtracts the values of the given source vector from those of this vector and returns them in store.subtractLocal
(double x, double y, double z) Decrements the values of this vector by the given x, y and z values.subtractLocal
(ReadOnlyVector3 source) Decrements the values of this vector by the x, y and z values from the given source vector.double[]
toArray
(double[] store) Stores the double values of this vector in the given double array.float[]
toFloatArray
(float[] store) Stores the double values of this vector in the given float array.toString()
void
write
(OutputCapsule capsule) void
Used with serialization.zero()
Sets the value of this vector to (0, 0, 0)
-
Field Details
-
ZERO
0, 0, 0 -
ONE
1, 1, 1 -
NEG_ONE
-1, -1, -1 -
UNIT_X
1, 0, 0 -
NEG_UNIT_X
-1, 0, 0 -
UNIT_Y
0, 1, 0 -
NEG_UNIT_Y
0, -1, 0 -
UNIT_Z
0, 0, 1 -
NEG_UNIT_Z
0, 0, -1 -
_x
protected double _x -
_y
protected double _y -
_z
protected double _z
-
-
Constructor Details
-
Vector3
public Vector3()Constructs a new vector set to (0, 0, 0). -
Vector3
Constructs a new vector set to the (x, y, z) values of the given source vector.- Parameters:
src
- the source vector
-
Vector3
public Vector3(double x, double y, double z) Constructs a new vector set to (x, y, z).- Parameters:
x
- the x valuey
- the y valuez
- the z value
-
-
Method Details
-
getX
public double getX()- Specified by:
getX
in interfaceReadOnlyVector3
-
getY
public double getY()- Specified by:
getY
in interfaceReadOnlyVector3
-
getZ
public double getZ()- Specified by:
getZ
in interfaceReadOnlyVector3
-
getXf
public float getXf()- Specified by:
getXf
in interfaceReadOnlyVector3
- Returns:
- x as a float, to decrease need for explicit casts.
-
getYf
public float getYf()- Specified by:
getYf
in interfaceReadOnlyVector3
- Returns:
- y as a float, to decrease need for explicit casts.
-
getZf
public float getZf()- Specified by:
getZf
in interfaceReadOnlyVector3
- Returns:
- z as a float, to decrease need for explicit casts.
-
getValue
public double getValue(int index) - Specified by:
getValue
in interfaceReadOnlyVector3
- Parameters:
index
- the field index- Returns:
- x value if index == 0, y value if index == 1 or z value if index == 2
- Throws:
IllegalArgumentException
- if index is not one of 0, 1, 2.
-
setValue
public void setValue(int index, double value) - Parameters:
index
- which field index in this vector to set.value
- to set to one of x, y or z.- Throws:
IllegalArgumentException
- if index is not one of 0, 1, 2.
-
toArray
public double[] toArray(double[] store) Stores the double values of this vector in the given double array.- Specified by:
toArray
in interfaceReadOnlyVector3
- Parameters:
store
- if null, a new double[3] array is created.- Returns:
- the double array
- Throws:
ArrayIndexOutOfBoundsException
- if store is not at least length 3.
-
toFloatArray
public float[] toFloatArray(float[] store) Stores the double values of this vector in the given float array.- Parameters:
store
- if null, a new float[3] array is created.- Returns:
- the float array
- Throws:
NullPointerException
- if store is null.ArrayIndexOutOfBoundsException
- if store is not at least length 3.
-
setX
public void setX(double x) Sets the first component of this vector to the given double value.- Parameters:
x
- the x value
-
setY
public void setY(double y) Sets the second component of this vector to the given double value.- Parameters:
y
- the y value
-
setZ
public void setZ(double z) Sets the third component of this vector to the given double value.- Parameters:
z
- the z value
-
set
Sets the value of this vector to (x, y, z)- Parameters:
x
- the x valuey
- the y valuez
- the z value- Returns:
- this vector for chaining
-
set
Sets the value of this vector to the (x, y, z) values of the provided source vector.- Parameters:
source
- the source vector- Returns:
- this vector for chaining
- Throws:
NullPointerException
- if source is null.
-
zero
Sets the value of this vector to (0, 0, 0)- Returns:
- this vector for chaining
-
add
Adds the given values to those of this vector and returns them in store * @param store the vector to store the result in for return. If null, a new vector object is created and returned. .- Specified by:
add
in interfaceReadOnlyVector3
- Parameters:
x
- the x valuey
- the y valuez
- the z valuestore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- (this.x + x, this.y + y, this.z + z)
-
addLocal
Increments the values of this vector with the given x, y and z values.- Parameters:
x
- the x valuey
- the y valuez
- the z value- Returns:
- this vector for chaining
-
add
Adds the values of the given source vector to those of this vector and returns them in store.- Specified by:
add
in interfaceReadOnlyVector3
- Parameters:
source
- the source vectorstore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- (this.x + source.x, this.y + source.y, this.z + source.z)
- Throws:
NullPointerException
- if source is null.
-
addLocal
Increments the values of this vector with the x, y and z values of the given vector.- Parameters:
source
- the source vector- Returns:
- this vector for chaining
- Throws:
NullPointerException
- if source is null.
-
subtract
Subtracts the given values from those of this vector and returns them in store.- Specified by:
subtract
in interfaceReadOnlyVector3
- Parameters:
x
- the x valuey
- the y valuez
- the z valuestore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- (this.x - x, this.y - y, this.z - z)
-
subtractLocal
Decrements the values of this vector by the given x, y and z values.- Parameters:
x
- the x valuey
- the y valuez
- the z value- Returns:
- this vector for chaining
-
subtract
Subtracts the values of the given source vector from those of this vector and returns them in store.- Specified by:
subtract
in interfaceReadOnlyVector3
- Parameters:
source
- the source vectorstore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- (this.x - source.x, this.y - source.y, this.z - source.z)
- Throws:
NullPointerException
- if source is null.
-
subtractLocal
Decrements the values of this vector by the x, y and z values from the given source vector.- Parameters:
source
- the source vector- Returns:
- this vector for chaining
- Throws:
NullPointerException
- if source is null.
-
multiply
Multiplies the values of this vector by the given scalar value and returns the result in store.- Specified by:
multiply
in interfaceReadOnlyVector3
- Parameters:
scalar
- the scalar valuestore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- a new vector (this.x * scalar, this.y * scalar, this.z * scalar)
-
multiplyLocal
Internally modifies the values of this vector by multiplying them each by the given scalar value.- Parameters:
scalar
- the scalar value- Returns:
- this vector for chaining
-
multiply
Multiplies the values of this vector by the given scale values and returns the result in store.- Specified by:
multiply
in interfaceReadOnlyVector3
- Parameters:
scale
- the scale valuestore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- a new vector (this.x * scale.x, this.y * scale.y, this.z * scale.z)
-
multiplyLocal
Internally modifies the values of this vector by multiplying them each by the given scale values.- Parameters:
scale
- the scale value- Returns:
- this vector for chaining
-
multiply
Multiplies the values of this vector by the given scale values and returns the result in store.- Specified by:
multiply
in interfaceReadOnlyVector3
- Parameters:
x
- the x valuey
- the y valuez
- the z valuestore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- a new vector (this.x * scale.x, this.y * scale.y, this.z * scale.z)
-
multiplyLocal
Internally modifies the values of this vector by multiplying them each by the given scale values.- Parameters:
x
- the x valuey
- the y valuez
- the z value- Returns:
- this vector for chaining
-
divide
Divides the values of this vector by the given scalar value and returns the result in store.- Specified by:
divide
in interfaceReadOnlyVector3
- Parameters:
scalar
- the scalar valuestore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- a new vector (this.x / scalar, this.y / scalar, this.z / scalar)
-
divideLocal
Internally modifies the values of this vector by dividing them each by the given scalar value.- Parameters:
scalar
- the scalar value- Returns:
- this vector for chaining
- Throws:
ArithmeticException
- if scalar is 0
-
divide
Divides the values of this vector by the given scale values and returns the result in store.- Specified by:
divide
in interfaceReadOnlyVector3
- Parameters:
scale
- the scale valuestore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- a new vector (this.x / scale.x, this.y / scale.y, this.z / scale.z)
-
divideLocal
Internally modifies the values of this vector by dividing them each by the given scale values.- Parameters:
scale
- the scale value- Returns:
- this vector for chaining
-
divide
Divides the values of this vector by the given scale values and returns the result in store.- Specified by:
divide
in interfaceReadOnlyVector3
- Parameters:
x
- the x valuey
- the y valuez
- the z valuestore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- a new vector (this.x / scale.x, this.y / scale.y, this.z / scale.z)
-
divideLocal
Internally modifies the values of this vector by dividing them each by the given scale values.- Parameters:
x
- the x valuey
- the y valuez
- the z value- Returns:
- this vector for chaining
-
scaleAddLocal
Internally modifies this vector by multiplying its values with a given scale value, then adding a given "add" value.- Parameters:
scale
- the value to multiply this vector by.add
- the value to add to the result- Returns:
- this vector for chaining
-
scaleAdd
Scales this vector by multiplying its values with a given scale value, then adding a given "add" value. The result is store in the given store parameter.- Specified by:
scaleAdd
in interfaceReadOnlyVector3
- Parameters:
scale
- the value to multiply by.add
- the value to addstore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- the store variable
-
negate
- Specified by:
negate
in interfaceReadOnlyVector3
- Parameters:
store
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- same as multiply(-1, store)
-
negateLocal
- Returns:
- same as multiplyLocal(-1)
-
normalize
Creates a new unit length vector from this one by dividing by length. If the length is 0, (ie, if the vector is 0, 0, 0) then a new vector (0, 0, 0) is returned.- Specified by:
normalize
in interfaceReadOnlyVector3
- Parameters:
store
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- a new unit vector (or 0, 0, 0 if this unit is 0 length)
-
normalizeLocal
Converts this vector into a unit vector by dividing it internally by its length. If the length is 0, (ie, if the vector is 0, 0, 0) then no action is taken.- Returns:
- this vector for chaining
-
lerp
Performs a linear interpolation between this vector and the given end vector, using the given scalar as a percent. iow, if changeAmnt is closer to 0, the result will be closer to the current value of this vector and if it is closer to 1, the result will be closer to the end value. The result is returned as a new vector object.- Specified by:
lerp
in interfaceReadOnlyVector3
- Parameters:
endVec
- the end vectorscalar
- the scalar valuestore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- a new vector as described above.
- Throws:
NullPointerException
- if endVec is null.
-
lerpLocal
Performs a linear interpolation between this vector and the given end vector, using the given scalar as a percent. iow, if changeAmnt is closer to 0, the result will be closer to the current value of this vector and if it is closer to 1, the result will be closer to the end value. The result is stored back in this vector.- Parameters:
endVec
- the end vectorscalar
- the scalar value- Returns:
- this vector for chaining
- Throws:
NullPointerException
- if endVec is null.
-
lerp
public static Vector3 lerp(ReadOnlyVector3 beginVec, ReadOnlyVector3 endVec, double scalar, Vector3 store) Performs a linear interpolation between the given begin and end vectors, using the given scalar as a percent. iow, if changeAmnt is closer to 0, the result will be closer to the begin value and if it is closer to 1, the result will be closer to the end value. The result is returned as a new vector object.- Parameters:
beginVec
- the begin vectorendVec
- the end vectorscalar
- the scalar as a percent.store
- the vector to store the result in for return. If null, a new vector object is created and returned. It IS safe for store to be the same as the begin or end vector.- Returns:
- a new vector as described above.
- Throws:
NullPointerException
- if beginVec or endVec are null.
-
lerpLocal
Performs a linear interpolation between the given begin and end vectors, using the given scalar as a percent. iow, if scalar is closer to 0, the result will be closer to the begin value and if it is closer to 1, the result will be closer to the end value. The result is stored back in this vector.- Parameters:
beginVec
- the begin vectorendVec
- the end vectorscalar
- the scalar as a percent.- Returns:
- this vector for chaining
- Throws:
NullPointerException
- if beginVec or endVec are null.
-
length
public double length()- Specified by:
length
in interfaceReadOnlyVector3
- Returns:
- the magnitude or distance between the origin (0, 0, 0) and the point described by this vector (x, y, z).
Effectively the square root of the value returned by
lengthSquared()
.
-
lengthSquared
public double lengthSquared()- Specified by:
lengthSquared
in interfaceReadOnlyVector3
- Returns:
- the squared magnitude or squared distance between the origin (0, 0, 0) and the point described by this vector (x, y, z)
-
distanceSquared
public double distanceSquared(double x, double y, double z) - Specified by:
distanceSquared
in interfaceReadOnlyVector3
- Parameters:
x
- the x valuey
- the y valuez
- the z value- Returns:
- the squared distance between the point described by this vector and the given x, y, z point. When comparing the relative distance between two points it is usually sufficient to compare the squared distances, thus avoiding an expensive square root operation.
-
distanceSquared
- Specified by:
distanceSquared
in interfaceReadOnlyVector3
- Parameters:
destination
- the destination- Returns:
- the squared distance between the point described by this vector and the given destination point. When comparing the relative distance between two points it is usually sufficient to compare the squared distances, thus avoiding an expensive square root operation.
- Throws:
NullPointerException
- if destination is null.
-
distance
public double distance(double x, double y, double z) - Specified by:
distance
in interfaceReadOnlyVector3
- Parameters:
x
- the x valuey
- the y valuez
- the z value- Returns:
- the distance between the point described by this vector and the given x, y, z point.
-
distance
- Specified by:
distance
in interfaceReadOnlyVector3
- Parameters:
destination
- the destination- Returns:
- the distance between the point described by this vector and the given destination point.
- Throws:
NullPointerException
- if destination is null.
-
dot
public double dot(double x, double y, double z) - Specified by:
dot
in interfaceReadOnlyVector3
- Parameters:
x
- the x valuey
- the y valuez
- the z value- Returns:
- the dot product of this vector with the given x, y, z values.
-
dot
- Specified by:
dot
in interfaceReadOnlyVector3
- Parameters:
vec
- the vector- Returns:
- the dot product of this vector with the x, y, z values of the given vector.
- Throws:
NullPointerException
- if vec is null.
-
cross
- Specified by:
cross
in interfaceReadOnlyVector3
- Parameters:
x
- the x valuey
- the y valuez
- the z valuestore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- the cross product of this vector with the given x, y, z values.
-
cross
- Specified by:
cross
in interfaceReadOnlyVector3
- Parameters:
vec
- the vectorstore
- the vector to store the result in for return. If null, a new vector object is created and returned.- Returns:
- the cross product of this vector with the given vector's x, y, z values
- Throws:
NullPointerException
- if vec is null.
-
crossLocal
- Parameters:
x
- the x valuey
- the y valuez
- the z value- Returns:
- this vector, set to the cross product of this vector with the given x, y, z values.
-
crossLocal
- Parameters:
vec
- the vector- Returns:
- this vector, set to the cross product of this vector with the given vector's x, y, z values
- Throws:
NullPointerException
- if vec is null.
-
smallestAngleBetween
- Specified by:
smallestAngleBetween
in interfaceReadOnlyVector3
- Parameters:
otherVector
- a unit vector to find the angle against- Returns:
- the minimum angle (in radians) between two vectors. It is assumed that both this vector and the given vector are unit vectors (normalized).
- Throws:
NullPointerException
- if otherVector is null.
-
isValid
Check a vector... if it is null or its doubles are NaN or infinite, return false. Else return true.- Parameters:
vector
- the vector to check- Returns:
- true or false as stated above.
-
isInfinite
Check if a vector is non-null and has infinite values.- Parameters:
vector
- the vector to check- Returns:
- true or false as stated above.
-
toString
-
hashCode
public int hashCode() -
equals
Compares this object against the specified object -
clone
- Specified by:
clone
in interfaceReadOnlyVector3
- Overrides:
clone
in classObject
-
getClassTag
- Specified by:
getClassTag
in interfaceSavable
-
write
- Specified by:
write
in interfaceSavable
- Throws:
IOException
-
read
- Specified by:
read
in interfaceSavable
- Throws:
IOException
-
readExternal
Used with serialization. Not to be called manually.- Specified by:
readExternal
in interfaceExternalizable
- Parameters:
in
- ObjectInput- Throws:
IOException
- if something wrong occurs while readingClassNotFoundException
- if a class is not found
-
writeExternal
Used with serialization. Not to be called manually.- Specified by:
writeExternal
in interfaceExternalizable
- Parameters:
out
- ObjectOutput- Throws:
IOException
- if something wrong occurs while writing
-
fetchTempInstance
- Returns:
- An instance of Vector3 that is intended for temporary use in calculations and so forth. Multiple calls to the method should return instances of this class that are not currently in use.
-
releaseTempInstance
Releases a Vector3 back to be used by a future call to fetchTempInstance. TAKE CARE: this Vector3 object should no longer have other classes referencing it or "Bad Things" will happen.- Parameters:
vec
- the Vector3 to release.
-