com.jme.math
Class Vector3f

java.lang.Object
  extended by com.jme.math.Vector3f
All Implemented Interfaces:
Savable, java.io.Externalizable, java.io.Serializable, java.lang.Cloneable

public class Vector3f
extends java.lang.Object
implements java.io.Externalizable, Savable, java.lang.Cloneable

Vector3f defines a Vector for a three float value tuple. Vector3f can represent any three dimensional value, such as a vertex, a normal, etc. Utility methods are also included to aid in mathematical calculations.

Author:
Mark Powell, Joshua Slack
See Also:
Serialized Form

Field Summary
static Vector3f UNIT_X
           
static Vector3f UNIT_XYZ
           
static Vector3f UNIT_Y
           
static Vector3f UNIT_Z
           
 float x
          the x value of the vector.
 float y
          the y value of the vector.
 float z
          the z value of the vector.
static Vector3f ZERO
           
 
Constructor Summary
Vector3f()
          Constructor instantiates a new Vector3f with default values of (0,0,0).
Vector3f(float x, float y, float z)
          Constructor instantiates a new Vector3f with provides values.
Vector3f(Vector3f copy)
          Constructor instantiates a new Vector3f that is a copy of the provided vector
 
Method Summary
 Vector3f add(float addX, float addY, float addZ)
          add adds the provided values to this vector, creating a new vector that is then returned.
 Vector3f add(Vector3f vec)
          add adds a provided vector to this vector creating a resultant vector which is returned.
 Vector3f add(Vector3f vec, Vector3f result)
          add adds the values of a provided vector storing the values in the supplied vector.
 Vector3f addLocal(float addX, float addY, float addZ)
          addLocal adds the provided values to this vector internally, and returns a handle to this vector for easy chaining of calls.
 Vector3f addLocal(Vector3f vec)
          addLocal adds a provided vector to this vector internally, and returns a handle to this vector for easy chaining of calls.
 float angleBetween(Vector3f otherVector)
          angleBetween returns (in radians) the angle between two vectors.
 Vector3f clone()
           
 Vector3f cross(float otherX, float otherY, float otherZ, Vector3f result)
          cross calculates the cross product of this vector with a Vector comprised of the specified other* elements.
 Vector3f cross(Vector3f v)
          Returns a new vector which is the cross product of this vector with the specified vector.
 Vector3f cross(Vector3f v, Vector3f result)
          cross calculates the cross product of this vector with a parameter vector v.
 Vector3f crossLocal(float otherX, float otherY, float otherZ)
          crossLocal calculates the cross product of this vector with a parameter vector v.
 Vector3f crossLocal(Vector3f v)
          crossLocal calculates the cross product of this vector with a parameter vector v.
 float distance(Vector3f v)
          distance calculates the distance between this vector and vector v.
 float distanceSquared(Vector3f v)
          distanceSquared calculates the distance squared between this vector and vector v.
 Vector3f divide(float scalar)
          divide divides the values of this vector by a scalar and returns the result.
 Vector3f divide(Vector3f scalar)
          divide divides the values of this vector by a scalar and returns the result.
 Vector3f divideLocal(float scalar)
          divideLocal divides this vector by a scalar internally, and returns a handle to this vector for easy chaining of calls.
 Vector3f divideLocal(Vector3f scalar)
          divideLocal divides this vector by a scalar internally, and returns a handle to this vector for easy chaining of calls.
 float dot(Vector3f vec)
          dot calculates the dot product of this vector with a provided vector.
 boolean equals(java.lang.Object o)
          are these two vectors the same? they are is they both have the same x,y, and z values.
static void generateComplementBasis(Vector3f u, Vector3f v, Vector3f w)
           
static void generateOrthonormalBasis(Vector3f u, Vector3f v, Vector3f w)
           
 float get(int index)
           
 java.lang.Class<? extends Vector3f> getClassTag()
           
 float getX()
           
 float getY()
           
 float getZ()
           
 int hashCode()
          hashCode returns a unique code for this vector object based on it's values.
 void interpolate(Vector3f finalVec, float changeAmnt)
          Sets this vector to the interpolation by changeAmnt from this to the finalVec this=(1-changeAmnt)*this + changeAmnt * finalVec
 void interpolate(Vector3f beginVec, Vector3f finalVec, float changeAmnt)
          Sets this vector to the interpolation by changeAmnt from beginVec to finalVec this=(1-changeAmnt)*beginVec + changeAmnt * finalVec
static boolean isValidVector(Vector3f vector)
          Check a vector...
 float length()
          length calculates the magnitude of this vector.
 float lengthSquared()
          lengthSquared calculates the squared value of the magnitude of the vector.
 Vector3f mult(float scalar)
          mult multiplies this vector by a scalar.
 Vector3f mult(float scalar, Vector3f product)
          mult multiplies this vector by a scalar.
 Vector3f mult(Vector3f vec)
          Returns a new Vector instance comprised of elements which are the product of the corresponding vector elements.
 Vector3f mult(Vector3f vec, Vector3f store)
          Multiplies a provided 'vec' vector with this vector.
 Vector3f multLocal(float scalar)
          multLocal multiplies this vector by a scalar internally, and returns a handle to this vector for easy chaining of calls.
 Vector3f multLocal(Vector3f vec)
          multLocal multiplies a provided vector to this vector internally, and returns a handle to this vector for easy chaining of calls.
 Vector3f negate()
          negate returns the negative of this vector.
 Vector3f negateLocal()
          negateLocal negates the internal values of this vector.
 Vector3f normalize()
          normalize returns the unit vector of this vector.
 Vector3f normalizeLocal()
          normalizeLocal makes this vector into a unit vector of itself.
 void read(JMEImporter e)
           
 void readExternal(java.io.ObjectInput in)
          Used with serialization.
 void scaleAdd(float scalar, Vector3f add)
          scaleAdd multiplies this vector by a scalar then adds the given Vector3f.
 void scaleAdd(float scalar, Vector3f mult, Vector3f add)
          scaleAdd multiplies the given vector by a scalar then adds the given vector.
 Vector3f set(float x, float y, float z)
          set sets the x,y,z values of the vector based on passed parameters.
 void set(int index, float value)
           
 Vector3f set(Vector3f vect)
          set sets the x,y,z values of the vector by copying the supplied vector.
 void setX(float x)
           
 void setY(float y)
           
 void setZ(float z)
           
 Vector3f subtract(float subtractX, float subtractY, float subtractZ)
          subtract subtracts the provided values from this vector, creating a new vector that is then returned.
 Vector3f subtract(Vector3f vec)
          subtract subtracts the values of a given vector from those of this vector creating a new vector object.
 Vector3f subtract(Vector3f vec, Vector3f result)
          subtract
 Vector3f subtractLocal(float subtractX, float subtractY, float subtractZ)
          subtractLocal subtracts the provided values from this vector internally, and returns a handle to this vector for easy chaining of calls.
 Vector3f subtractLocal(Vector3f vec)
          subtractLocal subtracts a provided vector to this vector internally, and returns a handle to this vector for easy chaining of calls.
 float[] toArray(float[] floats)
          Saves this Vector3f into the given float[] object.
 java.lang.String toString()
          toString returns the string representation of this vector.
 void write(JMEExporter e)
           
 void writeExternal(java.io.ObjectOutput out)
          Used with serialization.
 void zero()
          zero resets this vector's data to zero internally.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final Vector3f ZERO

UNIT_X

public static final Vector3f UNIT_X

UNIT_Y

public static final Vector3f UNIT_Y

UNIT_Z

public static final Vector3f UNIT_Z

UNIT_XYZ

public static final Vector3f UNIT_XYZ

x

public float x
the x value of the vector.


y

public float y
the y value of the vector.


z

public float z
the z value of the vector.

Constructor Detail

Vector3f

public Vector3f()
Constructor instantiates a new Vector3f with default values of (0,0,0).


Vector3f

public Vector3f(float x,
                float y,
                float z)
Constructor instantiates a new Vector3f with provides values.

Parameters:
x - the x value of the vector.
y - the y value of the vector.
z - the z value of the vector.

Vector3f

public Vector3f(Vector3f copy)
Constructor instantiates a new Vector3f that is a copy of the provided vector

Parameters:
copy - The Vector3f to copy
Method Detail

set

public Vector3f set(float x,
                    float y,
                    float z)
set sets the x,y,z values of the vector based on passed parameters.

Parameters:
x - the x value of the vector.
y - the y value of the vector.
z - the z value of the vector.
Returns:
this vector

set

public Vector3f set(Vector3f vect)
set sets the x,y,z values of the vector by copying the supplied vector.

Parameters:
vect - the vector to copy.
Returns:
this vector

add

public Vector3f add(Vector3f vec)
add adds a provided vector to this vector creating a resultant vector which is returned. If the provided vector is null, null is returned.

Parameters:
vec - the vector to add to this.
Returns:
the resultant vector.

add

public Vector3f add(Vector3f vec,
                    Vector3f result)
add adds the values of a provided vector storing the values in the supplied vector.

Parameters:
vec - the vector to add to this
result - the vector to store the result in
Returns:
result returns the supplied result vector.

addLocal

public Vector3f addLocal(Vector3f vec)
addLocal adds a provided vector to this vector internally, and returns a handle to this vector for easy chaining of calls. If the provided vector is null, null is returned.

Parameters:
vec - the vector to add to this vector.
Returns:
this

add

public Vector3f add(float addX,
                    float addY,
                    float addZ)
add adds the provided values to this vector, creating a new vector that is then returned.

Parameters:
addX - the x value to add.
addY - the y value to add.
addZ - the z value to add.
Returns:
the result vector.

addLocal

public Vector3f addLocal(float addX,
                         float addY,
                         float addZ)
addLocal adds the provided values to this vector internally, and returns a handle to this vector for easy chaining of calls.

Parameters:
addX - value to add to x
addY - value to add to y
addZ - value to add to z
Returns:
this

scaleAdd

public void scaleAdd(float scalar,
                     Vector3f add)
scaleAdd multiplies this vector by a scalar then adds the given Vector3f.

Parameters:
scalar - the value to multiply this vector by.
add - the value to add

scaleAdd

public void scaleAdd(float scalar,
                     Vector3f mult,
                     Vector3f add)
scaleAdd multiplies the given vector by a scalar then adds the given vector.

Parameters:
scalar - the value to multiply this vector by.
mult - the value to multiply the scalar by
add - the value to add

dot

public float dot(Vector3f vec)
dot calculates the dot product of this vector with a provided vector. If the provided vector is null, 0 is returned.

Parameters:
vec - the vector to dot with this vector.
Returns:
the resultant dot product of this vector and a given vector.

cross

public Vector3f cross(Vector3f v)
Returns a new vector which is the cross product of this vector with the specified vector.

Neither 'this' nor v are modified. The starting value of 'result'

Parameters:
v - the vector to take the cross product of with this.
Returns:
the cross product vector.

cross

public Vector3f cross(Vector3f v,
                      Vector3f result)
cross calculates the cross product of this vector with a parameter vector v. The result is stored in result

Neither 'this' nor v are modified. The starting value of 'result' (if any) is ignored.

Parameters:
v - the vector to take the cross product of with this.
result - the vector to store the cross product result.
Returns:
result, after recieving the cross product vector.

cross

public Vector3f cross(float otherX,
                      float otherY,
                      float otherZ,
                      Vector3f result)
cross calculates the cross product of this vector with a Vector comprised of the specified other* elements. The result is stored in result, without modifying either 'this' or the 'other*' values.

Parameters:
otherX - x component of the vector to take the cross product of with this.
otherY - y component of the vector to take the cross product of with this.
otherZ - z component of the vector to take the cross product of with this.
result - the vector to store the cross product result.
Returns:
result, after recieving the cross product vector.

crossLocal

public Vector3f crossLocal(Vector3f v)
crossLocal calculates the cross product of this vector with a parameter vector v.

Parameters:
v - the vector to take the cross product of with this.
Returns:
this.

crossLocal

public Vector3f crossLocal(float otherX,
                           float otherY,
                           float otherZ)
crossLocal calculates the cross product of this vector with a parameter vector v.

Parameters:
otherX - x component of the vector to take the cross product of with this.
otherY - y component of the vector to take the cross product of with this.
otherZ - z component of the vector to take the cross product of with this.
Returns:
this.

length

public float length()
length calculates the magnitude of this vector.

Returns:
the length or magnitude of the vector.

lengthSquared

public float lengthSquared()
lengthSquared calculates the squared value of the magnitude of the vector.

Returns:
the magnitude squared of the vector.

distanceSquared

public float distanceSquared(Vector3f v)
distanceSquared calculates the distance squared between this vector and vector v.

Parameters:
v - the second vector to determine the distance squared.
Returns:
the distance squared between the two vectors.

distance

public float distance(Vector3f v)
distance calculates the distance between this vector and vector v.

Parameters:
v - the second vector to determine the distance.
Returns:
the distance between the two vectors.

mult

public Vector3f mult(float scalar)
mult multiplies this vector by a scalar. The resultant vector is returned. "this" is not modified.

Parameters:
scalar - the value to multiply this vector by.
Returns:
the new vector.

mult

public Vector3f mult(float scalar,
                     Vector3f product)
mult multiplies this vector by a scalar. The resultant vector is supplied as the second parameter and returned. "this" is not modified.

Parameters:
scalar - the scalar to multiply this vector by.
product - the product to store the result in.
Returns:
product

multLocal

public Vector3f multLocal(float scalar)
multLocal multiplies this vector by a scalar internally, and returns a handle to this vector for easy chaining of calls.

Parameters:
scalar - the value to multiply this vector by.
Returns:
this

multLocal

public Vector3f multLocal(Vector3f vec)
multLocal multiplies a provided vector to this vector internally, and returns a handle to this vector for easy chaining of calls. If the provided vector is null, null is returned.

Parameters:
vec - the vector to mult to this vector.
Returns:
this

mult

public Vector3f mult(Vector3f vec)
Returns a new Vector instance comprised of elements which are the product of the corresponding vector elements. (N.b. this is not a cross product).

Neither 'this' nor 'vec' are modified.

Parameters:
vec - the vector to mult to this vector.

mult

public Vector3f mult(Vector3f vec,
                     Vector3f store)
Multiplies a provided 'vec' vector with this vector. If the specified 'store' is null, then a new Vector instance is returned. Otherwise, 'store' with replaced values will be returned, to facilitate chaining.

'This' is not modified; and the starting value of 'store' (if any) is ignored (and over-written).

The resultant Vector is comprised of elements which are the product of the corresponding vector elements. (N.b. this is not a cross product).

Parameters:
vec - the vector to mult to this vector.
store - result vector (null to create a new vector)
Returns:
'store', or a new Vector3f

divide

public Vector3f divide(float scalar)
divide divides the values of this vector by a scalar and returns the result. The values of this vector remain untouched.

Parameters:
scalar - the value to divide this vectors attributes by.
Returns:
the result Vector.

divideLocal

public Vector3f divideLocal(float scalar)
divideLocal divides this vector by a scalar internally, and returns a handle to this vector for easy chaining of calls. Dividing by zero will result in an exception.

Parameters:
scalar - the value to divides this vector by.
Returns:
this

divide

public Vector3f divide(Vector3f scalar)
divide divides the values of this vector by a scalar and returns the result. The values of this vector remain untouched.

Parameters:
scalar - the value to divide this vectors attributes by.
Returns:
the result Vector.

divideLocal

public Vector3f divideLocal(Vector3f scalar)
divideLocal divides this vector by a scalar internally, and returns a handle to this vector for easy chaining of calls. Dividing by zero will result in an exception.

Parameters:
scalar - the value to divides this vector by.
Returns:
this

negate

public Vector3f negate()
negate returns the negative of this vector. All values are negated and set to a new vector.

Returns:
the negated vector.

negateLocal

public Vector3f negateLocal()
negateLocal negates the internal values of this vector.

Returns:
this.

subtract

public Vector3f subtract(Vector3f vec)
subtract subtracts the values of a given vector from those of this vector creating a new vector object. If the provided vector is null, null is returned.

Parameters:
vec - the vector to subtract from this vector.
Returns:
the result vector.

subtractLocal

public Vector3f subtractLocal(Vector3f vec)
subtractLocal subtracts a provided vector to this vector internally, and returns a handle to this vector for easy chaining of calls. If the provided vector is null, null is returned.

Parameters:
vec - the vector to subtract
Returns:
this

subtract

public Vector3f subtract(Vector3f vec,
                         Vector3f result)
subtract

Parameters:
vec - the vector to subtract from this
result - the vector to store the result in
Returns:
result

subtract

public Vector3f subtract(float subtractX,
                         float subtractY,
                         float subtractZ)
subtract subtracts the provided values from this vector, creating a new vector that is then returned.

Parameters:
subtractX - the x value to subtract.
subtractY - the y value to subtract.
subtractZ - the z value to subtract.
Returns:
the result vector.

subtractLocal

public Vector3f subtractLocal(float subtractX,
                              float subtractY,
                              float subtractZ)
subtractLocal subtracts the provided values from this vector internally, and returns a handle to this vector for easy chaining of calls.

Parameters:
subtractX - the x value to subtract.
subtractY - the y value to subtract.
subtractZ - the z value to subtract.
Returns:
this

normalize

public Vector3f normalize()
normalize returns the unit vector of this vector.

Returns:
unit vector of this vector.

normalizeLocal

public Vector3f normalizeLocal()
normalizeLocal makes this vector into a unit vector of itself.

Returns:
this.

zero

public void zero()
zero resets this vector's data to zero internally.


angleBetween

public float angleBetween(Vector3f otherVector)
angleBetween returns (in radians) the angle between two vectors. It is assumed that both this vector and the given vector are unit vectors (iow, normalized).

Parameters:
otherVector - a unit vector to find the angle against
Returns:
the angle in radians.

interpolate

public void interpolate(Vector3f finalVec,
                        float changeAmnt)
Sets this vector to the interpolation by changeAmnt from this to the finalVec this=(1-changeAmnt)*this + changeAmnt * finalVec

Parameters:
finalVec - The final vector to interpolate towards
changeAmnt - An amount between 0.0 - 1.0 representing a precentage change from this towards finalVec

interpolate

public void interpolate(Vector3f beginVec,
                        Vector3f finalVec,
                        float changeAmnt)
Sets this vector to the interpolation by changeAmnt from beginVec to finalVec this=(1-changeAmnt)*beginVec + changeAmnt * finalVec

Parameters:
beginVec - the beging vector (changeAmnt=0)
finalVec - The final vector to interpolate towards
changeAmnt - An amount between 0.0 - 1.0 representing a precentage change from beginVec towards finalVec

isValidVector

public static boolean isValidVector(Vector3f vector)
Check a vector... if it is null or its floats are NaN or infinite, return false. Else return true.

Parameters:
vector - the vector to check
Returns:
true or false as stated above.

generateOrthonormalBasis

public static void generateOrthonormalBasis(Vector3f u,
                                            Vector3f v,
                                            Vector3f w)

generateComplementBasis

public static void generateComplementBasis(Vector3f u,
                                           Vector3f v,
                                           Vector3f w)

clone

public Vector3f clone()
Overrides:
clone in class java.lang.Object

toArray

public float[] toArray(float[] floats)
Saves this Vector3f into the given float[] object.

Parameters:
floats - The float[] to take this Vector3f. If null, a new float[3] is created.
Returns:
The array, with X, Y, Z float values in that order

equals

public boolean equals(java.lang.Object o)
are these two vectors the same? they are is they both have the same x,y, and z values.

Overrides:
equals in class java.lang.Object
Parameters:
o - the object to compare for equality
Returns:
true if they are equal

hashCode

public int hashCode()
hashCode returns a unique code for this vector object based on it's values. If two vectors are logically equivalent, they will return the same hash code value.

Overrides:
hashCode in class java.lang.Object
Returns:
the hash code value of this vector.

toString

public java.lang.String toString()
toString returns the string representation of this vector. The format is: org.jme.math.Vector3f [X=XX.XXXX, Y=YY.YYYY, Z=ZZ.ZZZZ]

Overrides:
toString in class java.lang.Object
Returns:
the string representation of this vector.

readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Used with serialization. Not to be called manually.

Specified by:
readExternal in interface java.io.Externalizable
Parameters:
in - input
Throws:
java.io.IOException
java.lang.ClassNotFoundException
See Also:
Externalizable

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Used with serialization. Not to be called manually.

Specified by:
writeExternal in interface java.io.Externalizable
Parameters:
out - output
Throws:
java.io.IOException
See Also:
Externalizable

write

public void write(JMEExporter e)
           throws java.io.IOException
Specified by:
write in interface Savable
Throws:
java.io.IOException

read

public void read(JMEImporter e)
          throws java.io.IOException
Specified by:
read in interface Savable
Throws:
java.io.IOException

getClassTag

public java.lang.Class<? extends Vector3f> getClassTag()
Specified by:
getClassTag in interface Savable

getX

public float getX()

setX

public void setX(float x)

getY

public float getY()

setY

public void setY(float y)

getZ

public float getZ()

setZ

public void setZ(float z)

get

public float get(int index)
Parameters:
index -
Returns:
x value if index == 0, y value if index == 1 or z value if index == 2
Throws:
java.lang.IllegalArgumentException - if index is not one of 0, 1, 2.

set

public void set(int index,
                float value)
Parameters:
index - which field index in this vector to set.
value - to set to one of x, y or z.
Throws:
java.lang.IllegalArgumentException - if index is not one of 0, 1, 2.