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 SummaryFieldsModifier and TypeFieldDescriptionstatic final Vector3fShared instance of the all-NaN vector (NaN,NaN,NaN).static final Vector3fShared instance of the all-negative-infinity vector (-Inf,-Inf,-Inf).static final Vector3fShared instance of the all-plus-infinity vector (+Inf,+Inf,+Inf).static final Vector3fShared instance of the +X direction (1,0,0).static final Vector3fShared instance of the all-ones vector (1,1,1).static final Vector3fShared instance of the +Y direction (0,1,0).static final Vector3fShared instance of the +Z direction (0,0,1).floatThe first (X) component.floatThe 2nd (Y) component.floatThe 3rd (Z) component.static final Vector3fShared instance of the all-zero vector (0,0,0).
- 
Constructor SummaryConstructors
- 
Method SummaryModifier 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.floatangleBetween(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.floatReturns the distance between this vector and the argument.floatReturns 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.floatReturns the dot (or inner) product with the argument.booleanTests for exact equality with the argument, distinguishing -0 from 0.static voidgenerateComplementBasis(Vector3f u, Vector3f v, Vector3f w) static voidgenerateOrthonormalBasis(Vector3f u, Vector3f v, Vector3f w) floatget(int index) Returns the indexed component.floatgetX()Returns the X component.floatgetY()Returns the Y component.floatgetZ()Returns the Z component.inthashCode()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.booleanTests for approximate equality with the specified vector, using the specified tolerance.booleanTests for a unit vector, with 1% tolerance.static booleanisValidVector(Vector3f vector) Tests whether the argument is a valid vector, returning false if it's null or if any component is NaN or infinite.floatlength()Returns the length (or magnitude).floatReturns 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.voidread(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.voidset(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.voidwrite(JmeExporter e) Serializes to the argument, for example when saving to a J3O file.zero()Sets all 3 components to zero.
- 
Field Details- 
ZEROShared instance of the all-zero vector (0,0,0). Do not modify!
- 
NANShared instance of the all-NaN vector (NaN,NaN,NaN). Do not modify!
- 
UNIT_XShared instance of the +X direction (1,0,0). Do not modify!
- 
UNIT_YShared instance of the +Y direction (0,1,0). Do not modify!
- 
UNIT_ZShared instance of the +Z direction (0,0,1). Do not modify!
- 
UNIT_XYZShared instance of the all-ones vector (1,1,1). Do not modify!
- 
POSITIVE_INFINITYShared instance of the all-plus-infinity vector (+Inf,+Inf,+Inf). Do not modify!
- 
NEGATIVE_INFINITYShared instance of the all-negative-infinity vector (-Inf,-Inf,-Inf). Do not modify!
- 
xpublic float xThe first (X) component.
- 
ypublic float yThe 2nd (Y) component.
- 
zpublic float zThe 3rd (Z) component.
 
- 
- 
Constructor Details- 
Vector3fpublic Vector3f()Instantiates an all-zero vector (0,0,0).
- 
Vector3fpublic Vector3f(float x, float y, float z) Instantiates a vector with specified components.- Parameters:
- x- the desired X component
- y- the desired Y component
- z- the desired Z component
 
- 
Vector3fInstantiates a copy of the argument.- Parameters:
- copy- the vector to copy (not null, unaffected)
 
 
- 
- 
Method Details- 
setSets all 3 components to specified values.- Parameters:
- x- the desired X component
- y- the desired Y component
- z- the desired Z component
- Returns:
- the (modified) current instance (for chaining)
 
- 
setCopies all 3 components from the argument.- Parameters:
- vect- the Vector3f to copy (not null, unaffected)
- Returns:
- the (modified) current instance (for chaining)
 
- 
addAdds 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
 
- 
addAdds 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's- result)
- result- storage for the sum (not null)
- Returns:
- result(for chaining)
 
- 
addLocalAdds 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's- this) or null for none
- Returns:
- the (modified) current instance or null
 
- 
addAdds 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 component
- addY- the amount to add to the Y component
- addZ- the amount to add to the Z component
- Returns:
- a new Vector3f
 
- 
addLocalAdds specified amounts to the vector's components and returns the (modified) current instance.- Parameters:
- addX- the amount to add to the X component
- addY- the amount to add to the Y component
- addZ- the amount to add to the Z component
- Returns:
- the (modified) current instance (for chaining)
 
- 
scaleAddMultiplies by the specified scalar, adds the specified vector, and returns the (modified) current instance.this = scalar * this + add - Parameters:
- scalar- the scaling factor
- add- the vector to add (not null, unaffected unless it's- this)
- Returns:
- the (modified) current instance (for chaining)
 
- 
scaleAddMultiplies 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 factor
- mult- the vector to scale (not null, unaffected unless it's- this)
- add- the vector to add (not null, unaffected unless it's- this)
- Returns:
- the (modified) current instance (for chaining)
 
- 
dotReturns 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
 
- 
crossCalculates 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:
- thiscross- v(a new Vector3f)
 
- 
crossCalculates 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's- result)
- result- storage for the product, or null for a new Vector3f
- Returns:
- thiscross- v(either- resultor a new Vector3f)
 
- 
crossCalculates 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 factor
- otherY- the Y component of the right factor
- otherZ- the Z component of the right factor
- result- storage for the product, or null for a new Vector3f
- Returns:
- thiscross (X,Y,Z) (either- resultor a new Vector3f)
 
- 
crossLocalRight multiplies by the argument (cross product) and returns the (modified) current instance.- Parameters:
- v- the right factor (not null, unaffected unless it's- this)
- Returns:
- the (modified) current instance (for chaining)
 
- 
crossLocalRight multiplies by the specified components (cross product) and returns the (modified) current instance.- Parameters:
- otherX- the X component of the right factor
- otherY- the Y component of the right factor
- otherZ- the Z component of the right factor
- Returns:
- the (modified) current instance (for chaining)
 
- 
projectProjects 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
 
- 
projectLocalProjects onto the argument and returns the (modified) current instance.- Parameters:
- other- the vector to project onto (not null, unaffected unless it's- this)
- Returns:
- the (modified) current instance (for chaining)
 
- 
isUnitVectorpublic 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
 
- 
lengthpublic float length()Returns the length (or magnitude). The current instance is unaffected.- Returns:
- the root-sum of the squared components (not negative)
 
- 
lengthSquaredpublic float lengthSquared()Returns the square of the length. The current instance is unaffected.- Returns:
- the sum of the squared components (not negative)
 
- 
distanceSquaredReturns 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)
 
- 
distanceReturns 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)
 
- 
multMultiplies 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
 
- 
multMultiplies with the specified scalar and returns the product in the specified vector. The current instance is unaffected, unless it'sproduct.- Parameters:
- scalar- the scaling factor
- product- storage for the product, or null for a new Vector3f
- Returns:
- either productor a new Vector3f
 
- 
multLocalMultiplies by the argument and returns the (modified) current instance.- Parameters:
- scalar- the scaling factor
- Returns:
- the (modified) current instance (for chaining)
 
- 
multLocalMultiplies 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's- this) or null for none
- Returns:
- the (modified) current instance (for chaining) or null
 
- 
multLocalMultiplies component-wise by the specified components and returns the (modified) current instance.- Parameters:
- x- the scale factor for the X component
- y- the scale factor for the Y component
- z- the scale factor for the Z component
- Returns:
- the (modified) current instance (for chaining)
 
- 
multMultiplies 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
 
- 
multMultiplies 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 component
- y- the scale factor for the Y component
- z- the scale factor for the Z component
- Returns:
- a new Vector3f
 
- 
multMultiplies 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's- store) or null for none
- store- storage for the product, or null for a new Vector3f
- Returns:
- either storeor a new Vector3f or null
 
- 
divideDivides by the argument and returns the quotient as a new instance. The current instance is unaffected.- Parameters:
- scalar- the divisor
- Returns:
- a new Vector3f
 
- 
divideLocalDivides by the argument and returns the (modified) current instance.- Parameters:
- scalar- the divisor
- Returns:
- the (modified) current instance (for chaining)
 
- 
divideLocalDivides component-wise by the specified components returns the (modified) current instance.- Parameters:
- x- the divisor for the X component
- y- the divisor for the Y component
- z- the divisor for the Z component
- Returns:
- the (modified) current instance (for chaining)
 
- 
divideDivides 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
 
- 
divideDivides 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 component
- y- the divisor for the Y component
- z- the divisor for the Z component
- Returns:
- a new Vector3f
 
- 
divideLocalDivides component-wise by the argument and returns the (modified) current instance.- Parameters:
- divisor- the divisor (not null, unaffected unless it's- this)
- Returns:
- the (modified) current instance (for chaining)
 
- 
negateReturns the negative. The current instance is unaffected.- Returns:
- a new Vector3f
 
- 
negateLocalNegates all 3 components and returns the (modified) current instance.- Returns:
- the (modified) current instance (for chaining)
 
- 
subtractSubtracts 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
 
- 
subtractLocalSubtracts 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's- this) or null for none
- Returns:
- the (modified) current instance or null
 
- 
subtractSubtracts 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's- result)
- result- storage for the difference, or null for a new Vector3f
- Returns:
- either resultor a new Vector3f
 
- 
subtractSubtracts 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 component
- subtractY- the amount to subtract from the Y component
- subtractZ- the amount to subtract from the Z component
- Returns:
- a new Vector3f
 
- 
subtractLocalSubtracts the specified amounts from the vector's components and returns the (modified) current instance.- Parameters:
- subtractX- the amount to subtract from the X component
- subtractY- the amount to subtract from the Y component
- subtractZ- the amount to subtract from the Z component
- Returns:
- the (modified) current instance (for chaining)
 
- 
normalizeNormalizes 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
 
- 
normalizeLocalNormalizes 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)
 
- 
maxLocalCompares 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)
 
- 
minLocalCompares 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)
 
- 
zeroSets all 3 components to zero.- Returns:
- the (modified) current instance (for chaining)
 
- 
angleBetweenReturns 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)
 
- 
interpolateLocalInterpolates 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's- this)
- changeAmount- the fractional change amount
- Returns:
- the (modified) current instance (for chaining)
 
- 
interpolateLocalInterpolates 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's- this)
- finalVec- the desired value when changeAmount=1 (not null, unaffected unless it's- this)
- changeAmount- the fractional change amount
- Returns:
- the (modified) current instance (for chaining)
 
- 
isValidVectorTests 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
- 
cloneCreates a copy. The current instance is unaffected.
- 
toArraypublic 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 floatsor a new float[3])
 
- 
equalsTests for exact equality with the argument, distinguishing -0 from 0. Ifois null, false is returned. Either way, the current instance is unaffected.
- 
isSimilarTests for approximate equality with the specified vector, using the specified tolerance. Ifotheris null, false is returned. Either way, the current instance is unaffected.- Parameters:
- other- the vector to compare (unaffected) or null for none
- epsilon- the tolerance for each component
- Returns:
- true if all 3 components are within tolerance, otherwise false
 
- 
hashCodepublic int hashCode()Returns a hash code. If two vectors have identical values, they will have the same hash code. The current instance is unaffected.
- 
toStringReturns a string representation of the vector, which is unaffected. For example, the +X direction vector is represented by:(1.0, 0.0, 0.0) 
- 
writeSerializes to the argument, for example when saving to a J3O file. The current instance is unaffected.- Specified by:
- writein interface- Savable
- Parameters:
- e- the exporter to use (not null)
- Throws:
- IOException- from the exporter
 
- 
readDe-serializes from the argument, for example when loading from a J3O file.- Specified by:
- readin interface- Savable
- Parameters:
- importer- the importer to use (not null)
- Throws:
- IOException- from the importer
 
- 
getXpublic float getX()Returns the X component. The vector is unaffected.- Returns:
- the value of the xcomponent
 
- 
setXSets the X component.- Parameters:
- x- the desired value
- Returns:
- the (modified) current instance (for chaining)
 
- 
getYpublic float getY()Returns the Y component. The vector is unaffected.- Returns:
- the value of the ycomponent
 
- 
setYSets the Y component.- Parameters:
- y- the desired value
- Returns:
- the (modified) current instance (for chaining)
 
- 
getZpublic float getZ()Returns the Z component. The vector is unaffected.- Returns:
- z the value of the zcomponent
 
- 
setZSets the Z component.- Parameters:
- z- the desired value
- Returns:
- the (modified) current instance (for chaining)
 
- 
getpublic 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
 
- 
setpublic 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 component
- value- the desired component value
- Throws:
- IllegalArgumentException- if index is not 0, 1, or 2
 
 
-