com.jme.math
Class Vector2f

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

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

Vector2f defines a Vector for a two float value vector.

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

Field Summary
 float x
          the x value of the vector.
 float y
          the y value of the vector.
 
Constructor Summary
Vector2f()
          Creates a Vector2f with x and y set to 0.
Vector2f(float x, float y)
          Creates a Vector2f with the given initial x and y values.
Vector2f(Vector2f vector2f)
          Creates a new Vector2f that contains the passed vector's information
 
Method Summary
 Vector2f add(Vector2f vec)
          add adds a provided vector to this vector creating a resultant vector which is returned.
 Vector2f add(Vector2f vec, Vector2f result)
          add adds this vector by vec and stores the result in result.
 Vector2f addLocal(float addX, float addY)
          addLocal adds the provided values to this vector internally, and returns a handle to this vector for easy chaining of calls.
 Vector2f addLocal(Vector2f vec)
          addLocal adds a provided vector to this vector internally, and returns a handle to this vector for easy chaining of calls.
 float angleBetween(Vector2f otherVector)
          angleBetween returns (in radians) the angle required to rotate a ray represented by this vector to lie colinear to a ray described by the given vector.
 Vector2f clone()
           
 Vector3f cross(Vector2f v)
          cross calculates the cross product of this vector with a parameter vector v.
 float determinant(Vector2f v)
           
 float distance(Vector2f v)
          distance calculates the distance between this vector and vector v.
 float distanceSquared(float otherX, float otherY)
          distanceSquared calculates the distance squared between this vector and vector v.
 float distanceSquared(Vector2f v)
          distanceSquared calculates the distance squared between this vector and vector v.
 Vector2f divide(float scalar)
          divide divides the values of this vector by a scalar and returns the result.
 Vector2f divideLocal(float scalar)
          divideLocal divides this vector by a scalar internally, and returns a handle to this vector for easy chaining of calls.
 float dot(Vector2f 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 and y values.
 float getAngle()
          getAngle returns (in radians) the angle represented by this Vector2f as expressed by a conversion from rectangular coordinates (xy) to polar coordinates (r, theta).
 java.lang.Class<? extends Vector2f> getClassTag()
           
 float getX()
           
 float getY()
           
 int hashCode()
          hashCode returns a unique code for this vector object based on it's values.
 void interpolate(Vector2f finalVec, float changeAmnt)
          Sets this vector to the interpolation by changeAmnt from this to the finalVec this=(1-changeAmnt)*this + changeAmnt * finalVec
 void interpolate(Vector2f beginVec, Vector2f finalVec, float changeAmnt)
          Sets this vector to the interpolation by changeAmnt from beginVec to finalVec this=(1-changeAmnt)*beginVec + changeAmnt * finalVec
static boolean isValidVector(Vector2f 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.
 Vector2f mult(float scalar)
          mult multiplies this vector by a scalar.
 Vector2f mult(float scalar, Vector2f product)
          Multiplies this Vector2f's x and y by the scalar and stores the result in product.
 Vector2f multLocal(float scalar)
          multLocal multiplies this vector by a scalar internally, and returns a handle to this vector for easy chaining of calls.
 Vector2f multLocal(Vector2f vec)
          multLocal multiplies a provided vector to this vector internally, and returns a handle to this vector for easy chaining of calls.
 Vector2f negate()
          negate returns the negative of this vector.
 Vector2f negateLocal()
          negateLocal negates the internal values of this vector.
 Vector2f normalize()
          normalize returns the unit vector of this vector.
 Vector2f 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 rotateAroundOrigin(float angle, boolean cw)
           
 Vector2f set(float x, float y)
          set the x and y values of the vector
 Vector2f set(Vector2f vec)
          set the x and y values of the vector from another vector
 void setX(float x)
           
 void setY(float y)
           
 float smallestAngleBetween(Vector2f otherVector)
          smallestAngleBetween returns (in radians) the minimum angle between two vectors.
 Vector2f subtract(float valX, float valY)
          subtract subtracts the given x,y values from those of this vector creating a new vector object.
 Vector2f subtract(Vector2f vec)
          subtract subtracts the values of a given vector from those of this vector creating a new vector object.
 Vector2f subtract(Vector2f vec, Vector2f store)
          subtract subtracts the values of a given vector from those of this vector storing the result in the given vector object.
 Vector2f subtractLocal(float valX, float valY)
          subtractLocal subtracts the provided values from this vector internally, and returns a handle to this vector for easy chaining of calls.
 Vector2f subtractLocal(Vector2f 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 Vector2f into the given float[] object.
 java.lang.String toString()
          toString returns the string representation of this vector object.
 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

x

public float x
the x value of the vector.


y

public float y
the y value of the vector.

Constructor Detail

Vector2f

public Vector2f(float x,
                float y)
Creates a Vector2f with the given initial x and y values.

Parameters:
x - The x value of this Vector2f.
y - The y value of this Vector2f.

Vector2f

public Vector2f()
Creates a Vector2f with x and y set to 0. Equivalent to Vector2f(0,0).


Vector2f

public Vector2f(Vector2f vector2f)
Creates a new Vector2f that contains the passed vector's information

Parameters:
vector2f - The vector to copy
Method Detail

set

public Vector2f set(float x,
                    float y)
set the x and y values of the vector

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

set

public Vector2f set(Vector2f vec)
set the x and y values of the vector from another vector

Parameters:
vec - the vector to copy from
Returns:
this vector

add

public Vector2f add(Vector2f 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.

addLocal

public Vector2f addLocal(Vector2f 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

addLocal

public Vector2f addLocal(float addX,
                         float addY)
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
Returns:
this

add

public Vector2f add(Vector2f vec,
                    Vector2f result)
add adds this vector by vec and stores the result in result.

Parameters:
vec - The vector to add.
result - The vector to store the result in.
Returns:
The result vector, after adding.

dot

public float dot(Vector2f 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(Vector2f v)
cross 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:
the cross product vector.

determinant

public float determinant(Vector2f v)

interpolate

public void interpolate(Vector2f 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 percentage change from this towards finalVec

interpolate

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

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

isValidVector

public static boolean isValidVector(Vector2f 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.

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(Vector2f 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.

distanceSquared

public float distanceSquared(float otherX,
                             float otherY)
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(Vector2f 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 Vector2f mult(float scalar)
mult multiplies this vector by a scalar. The resultant vector is returned.

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

multLocal

public Vector2f 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 Vector2f multLocal(Vector2f 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 Vector2f mult(float scalar,
                     Vector2f product)
Multiplies this Vector2f's x and y by the scalar and stores the result in product. The result is returned for chaining. Similar to product=this*scalar;

Parameters:
scalar - The scalar to multiply by.
product - The vector2f to store the result in.
Returns:
product, after multiplication.

divide

public Vector2f 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 Vector2f 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

negate

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

Returns:
the negated vector.

negateLocal

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

Returns:
this.

subtract

public Vector2f subtract(Vector2f 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, an exception is thrown.

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

subtract

public Vector2f subtract(Vector2f vec,
                         Vector2f store)
subtract subtracts the values of a given vector from those of this vector storing the result in the given vector object. If the provided vector is null, an exception is thrown.

Parameters:
vec - the vector to subtract from this vector.
store - the vector to store the result in. It is safe for this to be the same as vec. If null, a new vector is created.
Returns:
the result vector.

subtract

public Vector2f subtract(float valX,
                         float valY)
subtract subtracts the given x,y values from those of this vector creating a new vector object.

Parameters:
valX - value to subtract from x
valY - value to subtract from y
Returns:
this

subtractLocal

public Vector2f subtractLocal(Vector2f 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

subtractLocal

public Vector2f subtractLocal(float valX,
                              float valY)
subtractLocal subtracts the provided values from this vector internally, and returns a handle to this vector for easy chaining of calls.

Parameters:
valX - value to subtract from x
valY - value to subtract from y
Returns:
this

normalize

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

Returns:
unit vector of this vector.

normalizeLocal

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

Returns:
this.

smallestAngleBetween

public float smallestAngleBetween(Vector2f otherVector)
smallestAngleBetween returns (in radians) the minimum 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.

angleBetween

public float angleBetween(Vector2f otherVector)
angleBetween returns (in radians) the angle required to rotate a ray represented by this vector to lie colinear to a ray described by the given vector. It is assumed that both this vector and the given vector are unit vectors (iow, normalized).

Parameters:
otherVector - the "destination" unit vector
Returns:
the angle in radians.

getX

public float getX()

setX

public void setX(float x)

getY

public float getY()

setY

public void setY(float y)

getAngle

public float getAngle()
getAngle returns (in radians) the angle represented by this Vector2f as expressed by a conversion from rectangular coordinates (xy) to polar coordinates (r, theta).

Returns:
the angle in radians. [-pi, pi)

zero

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


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.

clone

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

toArray

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

Parameters:
floats - The float[] to take this Vector2f. If null, a new float[2] is created.
Returns:
The array, with X, Y 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 and y values.

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

toString

public java.lang.String toString()
toString returns the string representation of this vector object. The format of the string is such: com.jme.math.Vector2f [X=XX.XXXX, Y=YY.YYYY]

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 - ObjectInput
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 - ObjectOutput
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 Vector2f> getClassTag()
Specified by:
getClassTag in interface Savable

rotateAroundOrigin

public void rotateAroundOrigin(float angle,
                               boolean cw)