Package com.jme3.math
Class Vector2f
java.lang.Object
com.jme3.math.Vector2f
- All Implemented Interfaces:
Savable
,Serializable
,Cloneable
A vector composed of 2 single-precision components, used to represent
locations, offsets, directions, and rotations in 2-dimensional space.
Methods with names ending in "Local" modify the current instance. They are used to avoid creating garbage.
- See Also:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionAdds 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) Adds specified amounts to the vector's components and returns the (modified) current instance.Adds the argument and returns the (modified) current instance.float
angleBetween
(Vector2f otherVector) Returns the signed angle between the current instance and the argument.clone()
Creates a copy.Calculates a cross product with the argument and returns the product as a new instance.float
Returns the Z component of the cross product with the argument.float
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
Returns the square of the distance between the current instance and the argument.divide
(float scalar) Divides by the scalar argument and returns the quotient as a new instance.divide
(float x, float y) Divides component-wise by the specified components and returns the quotient as a new instance.divideLocal
(float scalar) Divides the vector by the scalar argument and returns the (modified) current instance.divideLocal
(float x, float y) Divides component-wise by the specified components 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
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.interpolateLocal
(Vector2f finalVec, float changeAmount) Interpolates linearly between the current instance and the specified vector, returning the (modified) current instance.interpolateLocal
(Vector2f beginVec, Vector2f finalVec, float changeAmount) Interpolates linearly between the specified beginning and final vectors, returning the (modified) current instance.boolean
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
Returns the square of the length.mult
(float scalar) Multiplies with the scalar argument and returns the product as a new instance.mult
(float x, float y) Multiplies component-wise by the specified components and returns the product as a new instance.Multiplies with the specified scalar and stores the product in the specified vector.multLocal
(float scalar) Multiplies by the scalar argument and returns the (modified) current instance.multLocal
(float x, float y) 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 of the vector.Negates both 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.void
read
(JmeImporter importer) De-serializes the vector from the specified importer, for example when loading from a J3O file.void
Sets the vector from anObjectInput
object.void
rotateAroundOrigin
(float angle, boolean cw) Rotates the vector around (0,0) by the specified angle.set
(float x, float y) Sets both components to specified values.Copies both components from the argument.setX
(float x) Sets the X component.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.subtract
(float valX, float valY) 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 valX, float valY) Subtracts the specified amounts from the vector's components and returns the (modified) current instance.subtractLocal
(Vector2f vec) Subtracts the argument and returns the (modified) current instance.float[]
toArray
(float[] floats) Copies the vector to the array argument.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
Writes the vector to anObjectOutput
object.zero()
Sets both components to zero.
-
Field Details
-
ZERO
Shared instance of the all-zero vector (0,0). Do not modify! -
UNIT_XY
Shared instance of the all-ones vector (1,1). Do not modify! -
x
public float xThe first (X) component. -
y
public float yThe 2nd (Y) component.
-
-
Constructor Details
-
Vector2f
public Vector2f(float x, float y) Instantiates a vector with specified components.- Parameters:
x
- the desired X componenty
- the desired Y component
-
Vector2f
public Vector2f()Instantiates an all-zero vector (0,0). -
Vector2f
Instantiates a copy of the argument.- Parameters:
vector2f
- the vector to copy (not null, unaffected)
-
-
Method Details
-
set
Sets both components to specified values.- Parameters:
x
- the desired X componenty
- the desired Y component- Returns:
- the (modified) current instance (for chaining)
-
set
Copies both components from the argument.- Parameters:
vec
- the Vector2f 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 Vector2f or null
-
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
-
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 component- Returns:
- the (modified) current instance (for chaining)
-
add
Adds a specified vector and returns the sum in a 3rd vector. If the argument is null, null is returned. Either way, the current instance is unaffected unless it'sresult
.- Parameters:
vec
- the vector to add (unaffected unless it'sresult
) or null for noneresult
- storage for the sum, or null for a new Vector2f- Returns:
- the sum (either
result
or a new Vector2f)
-
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)
-
determinant
Returns the Z component of the cross product with the argument. The current instance is unaffected.- Parameters:
v
- the right factor (not null, unaffected)- Returns:
- the Z component of
this
crossv
-
interpolateLocal
Interpolates linearly between the current instance 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'sthis
)changeAmount
- the fractional change amount- Returns:
- the (modified) current instance (for chaining)
-
interpolateLocal
Interpolates 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'sthis
)finalVec
- the desired value when changeAmount=1 (not null, unaffected unless it'sthis
)changeAmount
- the fractional change amount- 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
-
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)
-
distanceSquared
Returns the square of the distance between the current instance 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)
-
distanceSquared
public float distanceSquared(float otherX, float otherY) Returns the square of the distance between the current instance and a vector with the specified components. The current instance is unaffected.- Parameters:
otherX
- the X component of the vector to compareotherY
- the Y component of the vector to compare- Returns:
- the square of the Euclidean distance (not negative)
-
distance
Returns the distance between the current instance 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 scalar argument and returns the product as a new instance. The current instance is unaffected.- Parameters:
scalar
- the scaling factor- Returns:
- a new
Vector2f
-
multLocal
Multiplies by the scalar 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
-
mult
Multiplies with the specified scalar and stores the product in the specified vector. The current instance is unaffected.- Parameters:
scalar
- the scaling factorproduct
- storage for the product, or null for a new Vector2f- Returns:
- either
product
or a new Vector2f
-
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 component- Returns:
- a new Vector2f
-
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 component- Returns:
- the (modified) current instance (for chaining)
-
divide
Divides by the scalar argument and returns the quotient as a new instance. The current instance is unaffected.- Parameters:
scalar
- the divisor- Returns:
- a new
Vector2f
-
divideLocal
Divides the vector by the scalar argument and returns the (modified) current instance.- Parameters:
scalar
- the divisor- Returns:
- the (modified) current instance (for chaining)
-
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 component- Returns:
- a new Vector2f
-
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 component- Returns:
- the (modified) current instance (for chaining)
-
negate
Returns the negative of the vector. The current instance is unaffected.- Returns:
- a new Vector2f
-
negateLocal
Negates both 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 Vector2f
-
subtract
Subtracts the specified vector and returns the difference in a 3rd vector. The current instance is unaffected unless it'sstore
.It is safe for
vec
andstore
to be the same object.- Parameters:
vec
- the vector to subtract (not null, unaffected unless it'sstore
)store
- storage for the difference, or null for a new Vector2f- Returns:
- either
store
or a new Vector2f
-
subtract
Subtracts the specified amounts from the vector's components and returns the difference as a new instance. The current instance is unaffected.- Parameters:
valX
- the amount to subtract from the X componentvalY
- the amount to subtract from the Y component- Returns:
- a new Vector2f
-
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
-
subtractLocal
Subtracts the specified amounts from the vector's components and returns the (modified) current instance.- Parameters:
valX
- the amount to subtract from the X componentvalY
- the amount to subtract from the Y 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 Vector2f
-
normalizeLocal
Normalizes 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)
-
smallestAngleBetween
Returns the unsigned angle between the current instance and the argument, provided both vectors have length=1. IfotherVector
is null, Pi/2 is returned. The current instance is unaffected.- Parameters:
otherVector
- the unit vector to compare (unaffected) or null for none- Returns:
- the angle in radians (not negative)
-
angleBetween
Returns the signed angle between the current instance and the argument. The current instance is unaffected.- Parameters:
otherVector
- the vector to compare (not null, unaffected)- Returns:
- the angle in radians, measured counter-clockwise from
this
tootherVector
(≥-2*Pi, ≤2*Pi)
-
getX
public float getX()Returns the X component. The vector is unaffected.- Returns:
- the value of the
x
component
-
setX
Sets the X component.- Parameters:
x
- the desired value- Returns:
- the (modified) current instance (for chaining)
-
getY
public float getY()Returns the Y component. The vector is unaffected.- Returns:
- the value of the
y
component
-
setY
Sets the Y component.- Parameters:
y
- the desired value- Returns:
- the (modified) current instance (for chaining)
-
getAngle
public float getAngle()Returns the angle of the vector in polar coordinates. The current instance is unaffected.- Returns:
- the polar angle in radians, measured counter-clockwise from the +X axis (≥-Pi, ≤Pi)
-
zero
Sets both components to zero.- Returns:
- the (modified) current instance (for chaining)
-
hashCode
public int hashCode()Returns a hash code. If two vectors are logically equivalent, they will return the same hash code. The current instance is unaffected. -
clone
Creates a copy. The current instance is unaffected. -
toArray
public float[] toArray(float[] floats) Copies the vector to the array argument. The current instance is unaffected.- Parameters:
floats
- storage for the components (must have length≥2) or null for a new float[2]- Returns:
- an array containing the X and Y components in that order (either
floats
or a new float[2])
-
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. -
isSimilar
Tests for approximate equality with the specified vector, using the specified tolerance. Ifother
is null, false is returned. Either way, the current instance is unaffected.- Parameters:
other
- the vector to compare (unaffected) or null for noneepsilon
- the tolerance for each component- Returns:
- true if both components are within tolerance, otherwise false
-
toString
Returns a string representation of the vector. The current instance is unaffected. The format is:(XX.XXXX, YY.YYYY)
-
readExternal
Sets the vector from anObjectInput
object.Used with serialization. Shouldn't be invoked directly by application code.
- Parameters:
in
- the object to read from (not null)- Throws:
IOException
- if the ObjectInput cannot read a floatClassNotFoundException
- never- See Also:
-
writeExternal
Writes the vector to anObjectOutput
object. The current instance is unaffected.Used with serialization. Shouldn't be invoked directly by application code.
- Parameters:
out
- the object to write to (not null)- Throws:
IOException
- if the ObjectOuput cannot write a float- See Also:
-
write
Serializes the vector to the specified exporter, for example when saving to a J3O file. The current instance is unaffected.- Specified by:
write
in interfaceSavable
- Parameters:
e
- the exporter to use (not null)- Throws:
IOException
- from the exporter
-
read
De-serializes the vector from the specified importer, for example when loading from a J3O file.- Specified by:
read
in interfaceSavable
- Parameters:
importer
- the importer to use (not null)- Throws:
IOException
- from the importer
-
rotateAroundOrigin
public void rotateAroundOrigin(float angle, boolean cw) Rotates the vector around (0,0) by the specified angle.- Parameters:
angle
- the rotation angle (in radians)cw
- true to rotate clockwise, false to rotate counter-clockwise
-