public final class Vector2f extends java.lang.Object implements Savable, java.lang.Cloneable, java.io.Serializable
Methods with names ending in "Local" modify the current instance. They are used to avoid creating garbage.
Modifier and Type | Field and Description |
---|---|
static Vector2f |
UNIT_XY
Shared instance of the all-ones vector (1,1).
|
float |
x
The first (X) component.
|
float |
y
The 2nd (Y) component.
|
static Vector2f |
ZERO
Shared instance of the all-zero vector (0,0).
|
Constructor and Description |
---|
Vector2f()
Instantiates an all-zero vector (0,0).
|
Vector2f(float x,
float y)
Instantiates a vector with specified components.
|
Vector2f(Vector2f vector2f)
Instantiates a copy of the argument.
|
Modifier and Type | Method and Description |
---|---|
Vector2f |
add(Vector2f vec)
Adds the argument and returns the sum as a new instance.
|
Vector2f |
add(Vector2f vec,
Vector2f result)
Adds a specified vector and returns the sum in a 3rd vector.
|
Vector2f |
addLocal(float addX,
float addY)
Adds specified amounts to the vector's components and returns the
(modified) current instance.
|
Vector2f |
addLocal(Vector2f vec)
Adds the argument and returns the (modified) current instance.
|
float |
angleBetween(Vector2f otherVector)
Returns the signed angle between the current instance and the argument.
|
Vector2f |
clone()
Creates a copy.
|
Vector3f |
cross(Vector2f v)
Calculates a cross product with the argument and returns the product as a
new instance.
|
float |
determinant(Vector2f v)
Returns the Z component of the cross product with the argument.
|
float |
distance(Vector2f v)
Returns the distance between the current instance and the argument.
|
float |
distanceSquared(float otherX,
float otherY)
Returns the square of the distance between the current instance and a
vector with the specified components.
|
float |
distanceSquared(Vector2f v)
Returns the square of the distance between the current instance and the
argument.
|
Vector2f |
divide(float scalar)
Divides by the scalar argument and returns the quotient as a new
instance.
|
Vector2f |
divide(float x,
float y)
Divides component-wise by the specified components and returns the quotient
as a new instance.
|
Vector2f |
divideLocal(float scalar)
Divides the vector by the scalar argument and returns the (modified)
current instance.
|
Vector2f |
divideLocal(float x,
float y)
Divides component-wise by the specified components returns the (modified)
current instance.
|
float |
dot(Vector2f vec)
Returns the dot (or inner) product with the argument.
|
boolean |
equals(java.lang.Object o)
Tests for exact equality with the argument, distinguishing -0 from 0.
|
float |
getAngle()
Returns the angle of the vector in polar coordinates.
|
float |
getX()
Returns the X component.
|
float |
getY()
Returns the Y component.
|
int |
hashCode()
Returns a hash code.
|
Vector2f |
interpolateLocal(Vector2f finalVec,
float changeAmount)
Interpolates linearly between the current instance and the specified
vector, returning the (modified) current instance.
|
Vector2f |
interpolateLocal(Vector2f beginVec,
Vector2f finalVec,
float changeAmount)
Interpolates linearly between the specified beginning and final vectors,
returning the (modified) current instance.
|
boolean |
isSimilar(Vector2f other,
float epsilon)
Tests for approximate equality with the specified vector, using the
specified tolerance.
|
static boolean |
isValidVector(Vector2f 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 |
lengthSquared()
Returns the square of the length.
|
Vector2f |
mult(float scalar)
Multiplies with the scalar argument and returns the product as a new
instance.
|
Vector2f |
mult(float x,
float y)
Multiplies component-wise by the specified components and returns the
product as a new instance.
|
Vector2f |
mult(float scalar,
Vector2f product)
Multiplies with the specified scalar and stores the product in the
specified vector.
|
Vector2f |
multLocal(float scalar)
Multiplies by the scalar argument and returns the (modified) current
instance.
|
Vector2f |
multLocal(float x,
float y)
Multiplies component-wise by the specified components and returns the
(modified) current instance.
|
Vector2f |
multLocal(Vector2f vec)
Multiplies component-wise by the argument and returns the (modified)
current instance.
|
Vector2f |
negate()
Returns the negative of the vector.
|
Vector2f |
negateLocal()
Negates both components and returns the (modified) current instance.
|
Vector2f |
normalize()
Normalizes the vector to length=1 and returns the result as a new
instance.
|
Vector2f |
normalizeLocal()
Normalizes the vector to length=1 and returns the (modified) current
instance.
|
void |
read(JmeImporter importer)
De-serializes the vector from the specified importer, for example
when loading from a J3O file.
|
void |
readExternal(java.io.ObjectInput in)
Sets the vector from an
ObjectInput object. |
void |
rotateAroundOrigin(float angle,
boolean cw)
Rotates the vector around (0,0) by the specified angle.
|
Vector2f |
set(float x,
float y)
Sets both components to specified values.
|
Vector2f |
set(Vector2f vec)
Copies both components from the argument.
|
Vector2f |
setX(float x)
Sets the X component.
|
Vector2f |
setY(float y)
Sets the Y component.
|
float |
smallestAngleBetween(Vector2f otherVector)
Returns the unsigned angle between the current instance and the argument,
provided both vectors have length=1.
|
Vector2f |
subtract(float valX,
float valY)
Subtracts the specified amounts from the vector's components and returns
the difference as a new instance.
|
Vector2f |
subtract(Vector2f vec)
Subtracts the argument and returns the difference as a new instance.
|
Vector2f |
subtract(Vector2f vec,
Vector2f store)
Subtracts the specified vector and returns the difference in a 3rd
vector.
|
Vector2f |
subtractLocal(float valX,
float valY)
Subtracts the specified amounts from the vector's components and returns
the (modified) current instance.
|
Vector2f |
subtractLocal(Vector2f vec)
Subtracts the argument and returns the (modified) current instance.
|
float[] |
toArray(float[] floats)
Copies the vector to the array argument.
|
java.lang.String |
toString()
Returns a string representation of the vector.
|
void |
write(JmeExporter e)
Serializes the vector to the specified exporter, for example when saving
to a J3O file.
|
void |
writeExternal(java.io.ObjectOutput out)
Writes the vector to an
ObjectOutput object. |
Vector2f |
zero()
Sets both components to zero.
|
public static final Vector2f ZERO
public static final Vector2f UNIT_XY
public float x
public float y
public Vector2f(float x, float y)
x
- the desired X componenty
- the desired Y componentpublic Vector2f()
public Vector2f(Vector2f vector2f)
vector2f
- the vector to copy (not null, unaffected)public Vector2f set(float x, float y)
x
- the desired X componenty
- the desired Y componentpublic Vector2f set(Vector2f vec)
vec
- the Vector2f to copy (not null, unaffected)public Vector2f add(Vector2f vec)
vec
- the vector to add (unaffected) or null for nonepublic Vector2f addLocal(Vector2f vec)
vec
- the vector to add (unaffected unless it's this
) or
null for nonepublic Vector2f addLocal(float addX, float addY)
addX
- the amount to add to the X componentaddY
- the amount to add to the Y componentpublic Vector2f add(Vector2f vec, Vector2f result)
result
.vec
- the vector to add (unaffected unless it's result
) or
null for noneresult
- storage for the sum, or null for a new Vector2fresult
or a new Vector2f)public float dot(Vector2f vec)
vec
- the vector to multiply (unaffected) or null for nonepublic Vector3f cross(Vector2f v)
v
- the right factor (not null, unaffected)this
cross v
(a new Vector3f)public float determinant(Vector2f v)
v
- the right factor (not null, unaffected)this
cross v
public Vector2f interpolateLocal(Vector2f finalVec, float changeAmount)
this = (1 - changeAmount) * this + changeAmount * finalVec
finalVec
- the desired value when changeAmount=1 (not null,
unaffected unless it's this
)changeAmount
- the fractional change amountpublic Vector2f interpolateLocal(Vector2f beginVec, Vector2f finalVec, float changeAmount)
this = (1 - changeAmount) * beginVec + changeAmount * finalVec
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 amountpublic static boolean isValidVector(Vector2f vector)
vector
- the vector to test (unaffected)public float length()
public float lengthSquared()
public float distanceSquared(Vector2f v)
v
- the vector to compare (not null, unaffected)public float distanceSquared(float otherX, float otherY)
otherX
- the X component of the vector to compareotherY
- the Y component of the vector to comparepublic float distance(Vector2f v)
v
- the vector to compare (not null, unaffected)public Vector2f mult(float scalar)
scalar
- the scaling factorVector2f
public Vector2f multLocal(float scalar)
scalar
- the scaling factorpublic Vector2f multLocal(Vector2f vec)
vec
- the scale vector (unaffected unless it's this
) or
null for nonepublic Vector2f mult(float scalar, Vector2f product)
scalar
- the scaling factorproduct
- storage for the product, or null for a new Vector2fproduct
or a new Vector2fpublic Vector2f mult(float x, float y)
x
- the scale factor for the X componenty
- the scale factor for the Y componentpublic Vector2f multLocal(float x, float y)
x
- the scale factor for the X componenty
- the scale factor for the Y componentpublic Vector2f divide(float scalar)
scalar
- the divisorVector2f
public Vector2f divideLocal(float scalar)
scalar
- the divisorpublic Vector2f divide(float x, float y)
x
- the divisor for the X componenty
- the divisor for the Y componentpublic Vector2f divideLocal(float x, float y)
x
- the divisor for the X componenty
- the divisor for the Y componentpublic Vector2f negate()
public Vector2f negateLocal()
public Vector2f subtract(Vector2f vec)
vec
- the vector to subtract (not null, unaffected)public Vector2f subtract(Vector2f vec, Vector2f store)
store
.
It is safe for vec
and store
to be the same object.
vec
- the vector to subtract (not null, unaffected unless it's
store
)store
- storage for the difference, or null for a new Vector2fstore
or a new Vector2fpublic Vector2f subtract(float valX, float valY)
valX
- the amount to subtract from the X componentvalY
- the amount to subtract from the Y componentpublic Vector2f subtractLocal(Vector2f vec)
vec
- the vector to subtract (unaffected unless it's this
)
or null for nonepublic Vector2f subtractLocal(float valX, float valY)
valX
- the amount to subtract from the X componentvalY
- the amount to subtract from the Y componentpublic Vector2f normalize()
public Vector2f normalizeLocal()
public float smallestAngleBetween(Vector2f otherVector)
otherVector
is null, Pi/2
is returned. The current instance is unaffected.otherVector
- the unit vector to compare (unaffected) or null for
nonepublic float angleBetween(Vector2f otherVector)
otherVector
- the vector to compare (not null, unaffected)this
to otherVector
(≥-2*Pi, ≤2*Pi)public float getX()
x
componentpublic Vector2f setX(float x)
x
- the desired valuepublic float getY()
y
componentpublic Vector2f setY(float y)
y
- the desired valuepublic float getAngle()
public Vector2f zero()
public int hashCode()
hashCode
in class java.lang.Object
public Vector2f clone()
clone
in class java.lang.Object
public float[] toArray(float[] floats)
floats
- storage for the components (must have length≥2) or null
for a new float[2]floats
or a new float[2])public boolean equals(java.lang.Object o)
o
is null, false is returned. Either way, the current instance is
unaffected.equals
in class java.lang.Object
o
- the object to compare (unaffected) or null for nonepublic boolean isSimilar(Vector2f other, float epsilon)
other
is null, false is returned. Either
way, the current instance is unaffected.other
- the vector to compare (unaffected) or null for noneepsilon
- the tolerance for each componentpublic java.lang.String toString()
(XX.XXXX, YY.YYYY)
toString
in class java.lang.Object
public void readExternal(java.io.ObjectInput in) throws java.io.IOException, java.lang.ClassNotFoundException
ObjectInput
object.
Used with serialization. Shouldn't be invoked directly by application code.
in
- the object to read from (not null)java.io.IOException
- if the ObjectInput cannot read a floatjava.lang.ClassNotFoundException
- neverExternalizable
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException
ObjectOutput
object. The current instance
is unaffected.
Used with serialization. Shouldn't be invoked directly by application code.
out
- the object to write to (not null)java.io.IOException
- if the ObjectOuput cannot write a floatExternalizable
public void write(JmeExporter e) throws java.io.IOException
public void read(JmeImporter importer) throws java.io.IOException
public void rotateAroundOrigin(float angle, boolean cw)
angle
- the rotation angle (in radians)cw
- true to rotate clockwise, false to rotate counter-clockwise