Package com.jme3.math
Class Vector3f
java.lang.Object
com.jme3.math.Vector3f
- All Implemented Interfaces:
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 +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.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.float
Returns the distance between this vector and the argument.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.float
get
(int index) Returns the indexed component.int
hashCode()
Returns a hash code.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.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.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.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.toString()
Returns a string representation of the vector, which is unaffected.zero()
Sets all 3 components to zero.
-
Field Details
-
ZERO
Shared instance of the all-zero vector (0,0,0). 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! -
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)
-
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 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)
-
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)
-
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
-
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
-
zero
Sets all 3 components to zero.- 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
-
clone
Creates a copy. The current instance is unaffected. -
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. -
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)
-
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
-