Package com.jme3.math
Class Vector3f
java.lang.Object
com.jme3.math.Vector3f
- All Implemented Interfaces:
Savable
,Serializable
,Cloneable
A vector composed of 3 single-precision components, used to represent
locations, offsets, velocities, and directions in 3-dimensional space.
Methods with names ending in "Local" modify the current instance. They are used to cut down on the creation of new instances.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Vector3f
Shared instance of the all-NaN vector (NaN,NaN,NaN).static final Vector3f
Shared instance of the all-negative-infinity vector (-Inf,-Inf,-Inf).static final Vector3f
Shared instance of the all-plus-infinity vector (+Inf,+Inf,+Inf).static final Vector3f
Shared instance of the +X direction (1,0,0).static final Vector3f
Shared instance of the all-ones vector (1,1,1).static final Vector3f
Shared instance of the +Y direction (0,1,0).static final Vector3f
Shared instance of the +Z direction (0,0,1).float
The first (X) component.float
The 2nd (Y) component.float
The 3rd (Z) component.static final Vector3f
Shared instance of the all-zero vector (0,0,0). -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionadd
(float addX, float addY, float addZ) Adds specified amounts to the vector's components and returns the sum as a new instance.Adds the argument and returns the sum as a new instance.Adds a specified vector and returns the sum in a 3rd vector.addLocal
(float addX, float addY, float addZ) Adds specified amounts to the vector's components and returns the (modified) current instance.Adds the argument and returns the (modified) current instance.float
angleBetween
(Vector3f otherVector) Returns the angle (in radians) between this vector and the argument, provided both vectors have length=1.clone()
Creates a copy.Calculates a cross product with specified components and returns the product in the specified vector.Calculates a cross product with the argument and returns the product as a new instance.Calculates a cross product with a specified vector and returns the product in a 3rd vector.crossLocal
(float otherX, float otherY, float otherZ) Right multiplies by the specified components (cross product) and returns the (modified) current instance.Right multiplies by the argument (cross product) and returns the (modified) current instance.float
Returns the distance between this vector and the argument.float
Returns the square of the distance between this vector and the argument.divide
(float scalar) Divides by the argument and returns the quotient as a new instance.divide
(float x, float y, float z) Divides component-wise by the specified components and returns the quotient as a new instance.Divides component-wise by the argument and returns the quotient as a new instance.divideLocal
(float scalar) Divides by the argument and returns the (modified) current instance.divideLocal
(float x, float y, float z) Divides component-wise by the specified components returns the (modified) current instance.divideLocal
(Vector3f divisor) Divides component-wise by the argument and returns the (modified) current instance.float
Returns the dot (or inner) product with the argument.boolean
Tests for exact equality with the argument, distinguishing -0 from 0.static void
generateComplementBasis
(Vector3f u, Vector3f v, Vector3f w) static void
generateOrthonormalBasis
(Vector3f u, Vector3f v, Vector3f w) float
get
(int index) Returns the indexed component.float
getX()
Returns the X component.float
getY()
Returns the Y component.float
getZ()
Returns the Z component.int
hashCode()
Returns a hash code.interpolateLocal
(Vector3f finalVec, float changeAmount) Interpolates linearly between this vector and the specified vector, returning the (modified) current instance.interpolateLocal
(Vector3f beginVec, Vector3f finalVec, float changeAmount) Interpolates linearly between the specified beginning and final vectors, returning the (modified) current instance.boolean
Tests for approximate equality with the specified vector, using the specified tolerance.boolean
Tests for a unit vector, with 1% tolerance.static boolean
isValidVector
(Vector3f vector) Tests whether the argument is a valid vector, returning false if it's null or if any component is NaN or infinite.float
length()
Returns the length (or magnitude).float
Returns the square of the length.Compares this vector component-wise with the argument (keeping the most positive value for each component) and returns the (modified) current instance.Compares this vector component-wise with the argument (keeping the most negative value for each component) and returns the (modified) current instance.mult
(float scalar) Multiplies with the argument and returns the product as a new instance.mult
(float x, float y, float z) Multiplies component-wise by the specified components and returns the product as a new instance.Multiplies with the specified scalar and returns the product in the specified vector.Multiplies component-wise with the argument and returns the product as a new instance.Multiplies component-wise with the specified vector and returns the product in a 3rd vector.multLocal
(float scalar) Multiplies by the argument and returns the (modified) current instance.multLocal
(float x, float y, float z) Multiplies component-wise by the specified components and returns the (modified) current instance.Multiplies component-wise by the argument and returns the (modified) current instance.negate()
Returns the negative.Negates all 3 components and returns the (modified) current instance.Normalizes the vector to length=1 and returns the result as a new instance.Normalizes the vector to length=1 and returns the (modified) current instance.Projects onto the argument and returns the result as a new vector.projectLocal
(Vector3f other) Projects onto the argument and returns the (modified) current instance.void
read
(JmeImporter importer) De-serializes from the argument, for example when loading from a J3O file.Multiplies by the specified scalar, adds the specified vector, and returns the (modified) current instance.Multiplies a specified vector by a specified scalar, then adds another specified vector to it, before storing the result in the current instance.set
(float x, float y, float z) Sets all 3 components to specified values.void
set
(int index, float value) Sets the indexed component.Copies all 3 components from the argument.setX
(float x) Sets the X component.setY
(float y) Sets the Y component.setZ
(float z) Sets the Z component.subtract
(float subtractX, float subtractY, float subtractZ) Subtracts the specified amounts from the vector's components and returns the difference as a new instance.Subtracts the argument and returns the difference as a new instance.Subtracts the specified vector and returns the difference in a 3rd vector.subtractLocal
(float subtractX, float subtractY, float subtractZ) Subtracts the specified amounts from the vector's components and returns the (modified) current instance.subtractLocal
(Vector3f vec) Subtracts the argument and returns the (modified) current instance.float[]
toArray
(float[] floats) Copies the vector into the argument.toString()
Returns a string representation of the vector, which is unaffected.void
write
(JmeExporter e) Serializes to the argument, for example when saving to a J3O file.zero()
Sets all 3 components to zero.
-
Field Details
-
ZERO
Shared instance of the all-zero vector (0,0,0). Do not modify! -
NAN
Shared instance of the all-NaN vector (NaN,NaN,NaN). Do not modify! -
UNIT_X
Shared instance of the +X direction (1,0,0). Do not modify! -
UNIT_Y
Shared instance of the +Y direction (0,1,0). Do not modify! -
UNIT_Z
Shared instance of the +Z direction (0,0,1). Do not modify! -
UNIT_XYZ
Shared instance of the all-ones vector (1,1,1). Do not modify! -
POSITIVE_INFINITY
Shared instance of the all-plus-infinity vector (+Inf,+Inf,+Inf). Do not modify! -
NEGATIVE_INFINITY
Shared instance of the all-negative-infinity vector (-Inf,-Inf,-Inf). Do not modify! -
x
public float xThe first (X) component. -
y
public float yThe 2nd (Y) component. -
z
public float zThe 3rd (Z) component.
-
-
Constructor Details
-
Vector3f
public Vector3f()Instantiates an all-zero vector (0,0,0). -
Vector3f
public Vector3f(float x, float y, float z) Instantiates a vector with specified components.- Parameters:
x
- the desired X componenty
- the desired Y componentz
- the desired Z component
-
Vector3f
Instantiates a copy of the argument.- Parameters:
copy
- the vector to copy (not null, unaffected)
-
-
Method Details
-
set
Sets all 3 components to specified values.- Parameters:
x
- the desired X componenty
- the desired Y componentz
- the desired Z component- Returns:
- the (modified) current instance (for chaining)
-
set
Copies all 3 components from the argument.- Parameters:
vect
- the Vector3f to copy (not null, unaffected)- Returns:
- the (modified) current instance (for chaining)
-
add
Adds the argument and returns the sum as a new instance. If the argument is null, null is returned. Either way, the current instance is unaffected.- Parameters:
vec
- the vector to add (unaffected) or null for none- Returns:
- a new Vector3f or null
-
add
Adds a specified vector and returns the sum in a 3rd vector. The current instance is unaffected unless it'sresult
.- Parameters:
vec
- the vector to add (not null, unaffected unless it'sresult
)result
- storage for the sum (not null)- Returns:
result
(for chaining)
-
addLocal
Adds the argument and returns the (modified) current instance. If the argument is null, null is returned.- Parameters:
vec
- the vector to add (unaffected unless it'sthis
) or null for none- Returns:
- the (modified) current instance or null
-
add
Adds specified amounts to the vector's components and returns the sum as a new instance. The current instance is unaffected.- Parameters:
addX
- the amount to add to the X componentaddY
- the amount to add to the Y componentaddZ
- the amount to add to the Z component- Returns:
- a new Vector3f
-
addLocal
Adds specified amounts to the vector's components and returns the (modified) current instance.- Parameters:
addX
- the amount to add to the X componentaddY
- the amount to add to the Y componentaddZ
- the amount to add to the Z component- Returns:
- the (modified) current instance (for chaining)
-
scaleAdd
Multiplies by the specified scalar, adds the specified vector, and returns the (modified) current instance.this = scalar * this + add
- Parameters:
scalar
- the scaling factoradd
- the vector to add (not null, unaffected unless it'sthis
)- Returns:
- the (modified) current instance (for chaining)
-
scaleAdd
Multiplies a specified vector by a specified scalar, then adds another specified vector to it, before storing the result in the current instance.this = scalar * mult + add
- Parameters:
scalar
- the scaling factormult
- the vector to scale (not null, unaffected unless it'sthis
)add
- the vector to add (not null, unaffected unless it'sthis
)- Returns:
- the (modified) current instance (for chaining)
-
dot
Returns the dot (or inner) product with the argument. If the argument is null, 0 is returned. Either way, the current instance is unaffected.- Parameters:
vec
- the vector to multiply (unaffected) or null for none- Returns:
- the product or 0
-
cross
Calculates a cross product with the argument and returns the product as a new instance. The current instance is unaffected.- Parameters:
v
- the right factor (not null, unaffected)- Returns:
this
crossv
(a new Vector3f)
-
cross
Calculates a cross product with a specified vector and returns the product in a 3rd vector. The current instance is unaffected unless it'sresult
.- Parameters:
v
- the right factor (not null, unaffected unless it'sresult
)result
- storage for the product, or null for a new Vector3f- Returns:
this
crossv
(eitherresult
or a new Vector3f)
-
cross
Calculates a cross product with specified components and returns the product in the specified vector. The current instance is unaffected unless it'sresult
.- Parameters:
otherX
- the X component of the right factorotherY
- the Y component of the right factorotherZ
- the Z component of the right factorresult
- storage for the product, or null for a new Vector3f- Returns:
this
cross (X,Y,Z) (eitherresult
or a new Vector3f)
-
crossLocal
Right multiplies by the argument (cross product) and returns the (modified) current instance.- Parameters:
v
- the right factor (not null, unaffected unless it'sthis
)- Returns:
- the (modified) current instance (for chaining)
-
crossLocal
Right multiplies by the specified components (cross product) and returns the (modified) current instance.- Parameters:
otherX
- the X component of the right factorotherY
- the Y component of the right factorotherZ
- the Z component of the right factor- Returns:
- the (modified) current instance (for chaining)
-
project
Projects onto the argument and returns the result as a new vector. The current instance is unaffected.- Parameters:
other
- the vector to project onto (not null, unaffected)- Returns:
- a new Vector3f
-
projectLocal
Projects onto the argument and returns the (modified) current instance.- Parameters:
other
- the vector to project onto (not null, unaffected unless it'sthis
)- Returns:
- the (modified) current instance (for chaining)
-
isUnitVector
public boolean isUnitVector()Tests for a unit vector, with 1% tolerance. The current instance is unaffected.- Returns:
- true if the current vector's length is between 0.99 and 1.01 inclusive, otherwise false
-
length
public float length()Returns the length (or magnitude). The current instance is unaffected.- Returns:
- the root-sum of the squared components (not negative)
-
lengthSquared
public float lengthSquared()Returns the square of the length. The current instance is unaffected.- Returns:
- the sum of the squared components (not negative)
-
distanceSquared
Returns the square of the distance between this vector and the argument. The current instance is unaffected.- Parameters:
v
- the vector to compare (not null, unaffected)- Returns:
- the square of the Euclidean distance (not negative)
-
distance
Returns the distance between this vector and the argument. The current instance is unaffected.- Parameters:
v
- the vector to compare (not null, unaffected)- Returns:
- the Euclidean distance (not negative)
-
mult
Multiplies with the argument and returns the product as a new instance. The current instance is unaffected.- Parameters:
scalar
- the scaling factor- Returns:
- a new Vector3f
-
mult
Multiplies with the specified scalar and returns the product in the specified vector. The current instance is unaffected, unless it'sproduct
.- Parameters:
scalar
- the scaling factorproduct
- storage for the product, or null for a new Vector3f- Returns:
- either
product
or a new Vector3f
-
multLocal
Multiplies by the argument and returns the (modified) current instance.- Parameters:
scalar
- the scaling factor- Returns:
- the (modified) current instance (for chaining)
-
multLocal
Multiplies component-wise by the argument and returns the (modified) current instance. If the argument is null, null is returned.- Parameters:
vec
- the scale vector (unaffected unless it'sthis
) or null for none- Returns:
- the (modified) current instance (for chaining) or null
-
multLocal
Multiplies component-wise by the specified components and returns the (modified) current instance.- Parameters:
x
- the scale factor for the X componenty
- the scale factor for the Y componentz
- the scale factor for the Z component- Returns:
- the (modified) current instance (for chaining)
-
mult
Multiplies component-wise with the argument and returns the product as a new instance. If the argument is null, null is returned. Either way, the current instance is unaffected.- Parameters:
vec
- the scale vector (unaffected) or null for none- Returns:
- a new Vector3f or null
-
mult
Multiplies component-wise by the specified components and returns the product as a new instance. The current instance is unaffected.- Parameters:
x
- the scale factor for the X componenty
- the scale factor for the Y componentz
- the scale factor for the Z component- Returns:
- a new Vector3f
-
mult
Multiplies component-wise with the specified vector and returns the product in a 3rd vector. If the argument is null, null is returned. Either way, the current instance is unaffected, unless it'sstore
.- Parameters:
vec
- the scale vector (unaffected unless it'sstore
) or null for nonestore
- storage for the product, or null for a new Vector3f- Returns:
- either
store
or a new Vector3f or null
-
divide
Divides by the argument and returns the quotient as a new instance. The current instance is unaffected.- Parameters:
scalar
- the divisor- Returns:
- a new Vector3f
-
divideLocal
Divides by the argument and returns the (modified) current instance.- Parameters:
scalar
- the divisor- Returns:
- the (modified) current instance (for chaining)
-
divideLocal
Divides component-wise by the specified components returns the (modified) current instance.- Parameters:
x
- the divisor for the X componenty
- the divisor for the Y componentz
- the divisor for the Z component- Returns:
- the (modified) current instance (for chaining)
-
divide
Divides component-wise by the argument and returns the quotient as a new instance. The current instance is unaffected.- Parameters:
divisor
- the divisor (not null, unaffected)- Returns:
- a new Vector3f
-
divide
Divides component-wise by the specified components and returns the quotient as a new instance. The current instance is unaffected.- Parameters:
x
- the divisor for the X componenty
- the divisor for the Y componentz
- the divisor for the Z component- Returns:
- a new Vector3f
-
divideLocal
Divides component-wise by the argument and returns the (modified) current instance.- Parameters:
divisor
- the divisor (not null, unaffected unless it'sthis
)- Returns:
- the (modified) current instance (for chaining)
-
negate
Returns the negative. The current instance is unaffected.- Returns:
- a new Vector3f
-
negateLocal
Negates all 3 components and returns the (modified) current instance.- Returns:
- the (modified) current instance (for chaining)
-
subtract
Subtracts the argument and returns the difference as a new instance. The current instance is unaffected.- Parameters:
vec
- the vector to subtract (not null, unaffected)- Returns:
- a new Vector3f
-
subtractLocal
Subtracts the argument and returns the (modified) current instance. If the argument is null, null is returned.- Parameters:
vec
- the vector to subtract (unaffected unless it'sthis
) or null for none- Returns:
- the (modified) current instance or null
-
subtract
Subtracts the specified vector and returns the difference in a 3rd vector. The current instance is unaffected unless it'sresult
.- Parameters:
vec
- the vector to subtract (not null, unaffected unless it'sresult
)result
- storage for the difference, or null for a new Vector3f- Returns:
- either
result
or a new Vector3f
-
subtract
Subtracts the specified amounts from the vector's components and returns the difference as a new instance. The current instance is unaffected.- Parameters:
subtractX
- the amount to subtract from the X componentsubtractY
- the amount to subtract from the Y componentsubtractZ
- the amount to subtract from the Z component- Returns:
- a new Vector3f
-
subtractLocal
Subtracts the specified amounts from the vector's components and returns the (modified) current instance.- Parameters:
subtractX
- the amount to subtract from the X componentsubtractY
- the amount to subtract from the Y componentsubtractZ
- the amount to subtract from the Z component- Returns:
- the (modified) current instance (for chaining)
-
normalize
Normalizes the vector to length=1 and returns the result as a new instance. If the vector has length=0, a clone is returned. Either way, the current instance is unaffected.- Returns:
- a new Vector3f
-
normalizeLocal
Normalizes the vector to length=1 and returns the (modified) current instance. If the vector has length=0, it's unchanged.- Returns:
- the (modified) current instance (for chaining)
-
maxLocal
Compares this vector component-wise with the argument (keeping the most positive value for each component) and returns the (modified) current instance.- Parameters:
other
- the vector to compare (not null, unaffected)- Returns:
- the (modified) current instance (for chaining)
-
minLocal
Compares this vector component-wise with the argument (keeping the most negative value for each component) and returns the (modified) current instance.- Parameters:
other
- the vector to compare (not null, unaffected)- Returns:
- the (modified) current instance (for chaining)
-
zero
Sets all 3 components to zero.- Returns:
- the (modified) current instance (for chaining)
-
angleBetween
Returns the angle (in radians) between this vector and the argument, provided both vectors have length=1. The current instance is unaffected.- Parameters:
otherVector
- a unit vector to compare (not null, unaffected)- Returns:
- the angle (in radians, not negative)
-
interpolateLocal
Interpolates linearly between this vector and the specified vector, returning the (modified) current instance.this = (1 - changeAmount) * this + changeAmount * finalVec
- Parameters:
finalVec
- the desired value when changeAmount=1 (not null, unaffected unless it'sthis
)changeAmount
- the fractional change amount- Returns:
- the (modified) current instance (for chaining)
-
interpolateLocal
Interpolates linearly between the specified beginning and final vectors, returning the (modified) current instance.this = (1 - changeAmount) * beginVec + changeAmount * finalVec
- Parameters:
beginVec
- the desired value when changeAmount=0 (not null, unaffected unless it'sthis
)finalVec
- the desired value when changeAmount=1 (not null, unaffected unless it'sthis
)changeAmount
- the fractional change amount- Returns:
- the (modified) current instance (for chaining)
-
isValidVector
Tests whether the argument is a valid vector, returning false if it's null or if any component is NaN or infinite.- Parameters:
vector
- the vector to test (unaffected)- Returns:
- true if non-null and finite, otherwise false
-
generateOrthonormalBasis
-
generateComplementBasis
-
clone
Creates a copy. The current instance is unaffected. -
toArray
public float[] toArray(float[] floats) Copies the vector into the argument. The current instance is unaffected.- Parameters:
floats
- storage for the components (must have length≥3) or null for a new float[3]- Returns:
- an array containing the X, Y, and Z components in that order
(either
floats
or a new float[3])
-
equals
Tests for exact equality with the argument, distinguishing -0 from 0. Ifo
is null, false is returned. Either way, the current instance is unaffected. -
isSimilar
Tests for approximate equality with the specified vector, using the specified tolerance. Ifother
is null, false is returned. Either way, the current instance is unaffected.- Parameters:
other
- the vector to compare (unaffected) or null for noneepsilon
- the tolerance for each component- Returns:
- true if all 3 components are within tolerance, otherwise false
-
hashCode
public int hashCode()Returns a hash code. If two vectors have identical values, they will have the same hash code. The current instance is unaffected. -
toString
Returns a string representation of the vector, which is unaffected. For example, the +X direction vector is represented by:(1.0, 0.0, 0.0)
-
write
Serializes to the argument, for example when saving to a J3O file. The current instance is unaffected.- Specified by:
write
in interfaceSavable
- Parameters:
e
- the exporter to use (not null)- Throws:
IOException
- from the exporter
-
read
De-serializes from the argument, for example when loading from a J3O file.- Specified by:
read
in interfaceSavable
- Parameters:
importer
- the importer to use (not null)- Throws:
IOException
- from the importer
-
getX
public float getX()Returns the X component. The vector is unaffected.- Returns:
- the value of the
x
component
-
setX
Sets the X component.- Parameters:
x
- the desired value- Returns:
- the (modified) current instance (for chaining)
-
getY
public float getY()Returns the Y component. The vector is unaffected.- Returns:
- the value of the
y
component
-
setY
Sets the Y component.- Parameters:
y
- the desired value- Returns:
- the (modified) current instance (for chaining)
-
getZ
public float getZ()Returns the Z component. The vector is unaffected.- Returns:
- z the value of the
z
component
-
setZ
Sets the Z component.- Parameters:
z
- the desired value- Returns:
- the (modified) current instance (for chaining)
-
get
public float get(int index) Returns the indexed component. The vector is unaffected.- Parameters:
index
- 0, 1, or 2- Returns:
- the X component if index=0, the Y component if index=1, or the Z component if index=2
- Throws:
IllegalArgumentException
- if index is not 0, 1, or 2
-
set
public void set(int index, float value) Sets the indexed component.- Parameters:
index
- which component to set: 0 → the X component, 1 → the Y component, 2 → the Z componentvalue
- the desired component value- Throws:
IllegalArgumentException
- if index is not 0, 1, or 2
-