public final class Quaternion extends java.lang.Object implements Savable, java.lang.Cloneable, java.io.Serializable
Mathematically, quaternions are an extension of complex numbers. In mathematics texts, W often appears first, but in JME it always comes last.
Modifier and Type | Field and Description |
---|---|
static Quaternion |
DIRECTION_Z
Another shared instance of the identity quaternion (0, 0, 0, 1).
|
static Quaternion |
IDENTITY
Shared instance of the identity quaternion (0, 0, 0, 1).
|
protected float |
w
The real (W) component.
|
protected float |
x
The first imaginary (X) component.
|
protected float |
y
The 2nd imaginary (Y) component.
|
protected float |
z
The 3rd imaginary (Z) component.
|
static Quaternion |
ZERO
Shared instance of the zero quaternion (0, 0, 0, 0).
|
Constructor and Description |
---|
Quaternion()
Instantiates an identity quaternion: all components zeroed except
w , which is set to 1. |
Quaternion(float[] angles)
Instantiates a quaternion from Tait-Bryan angles, applying the rotations
in x-z-y extrinsic order or y-z'-x" intrinsic order.
|
Quaternion(float x,
float y,
float z,
float w)
Instantiates a quaternion with specified components.
|
Quaternion(Quaternion q)
Instantiates a copy of the argument.
|
Quaternion(Quaternion q1,
Quaternion q2,
float interp)
Instantiates a quaternion by interpolating between the specified
quaternions.
|
Modifier and Type | Method and Description |
---|---|
Quaternion |
add(Quaternion q)
Adds the argument and returns the sum as a new instance.
|
Quaternion |
addLocal(Quaternion q)
Adds the argument and returns the (modified) current instance.
|
void |
apply(Matrix3f matrix)
Applies the rotation represented by the argument to the current instance.
|
Quaternion |
clone()
Creates a copy.
|
float |
dot(Quaternion q)
Returns the dot product with the argument.
|
boolean |
equals(java.lang.Object o)
Tests for exact equality with the argument, distinguishing -0 from 0.
|
Quaternion |
fromAngleAxis(float angle,
Vector3f axis)
Sets the quaternion from the specified rotation angle and axis of
rotation.
|
Quaternion |
fromAngleNormalAxis(float angle,
Vector3f axis)
Sets the quaternion from the specified rotation angle and normalized axis
of rotation.
|
Quaternion |
fromAngles(float[] angles)
Sets the quaternion from the specified Tait-Bryan angles, applying the
rotations in x-z-y extrinsic order or y-z'-x" intrinsic order.
|
Quaternion |
fromAngles(float xAngle,
float yAngle,
float zAngle)
Sets the quaternion from the specified Tait-Bryan angles, applying the
rotations in x-z-y extrinsic order or y-z'-x" intrinsic order.
|
Quaternion |
fromAxes(Vector3f[] axis)
Sets the quaternion from the specified orthonormal basis.
|
Quaternion |
fromAxes(Vector3f xAxis,
Vector3f yAxis,
Vector3f zAxis)
Sets the quaternion from the specified orthonormal basis.
|
Quaternion |
fromRotationMatrix(float m00,
float m01,
float m02,
float m10,
float m11,
float m12,
float m20,
float m21,
float m22)
Sets the quaternion from a rotation matrix with the specified elements.
|
Quaternion |
fromRotationMatrix(Matrix3f matrix)
Sets the quaternion from the specified rotation matrix.
|
Vector3f |
getRotationColumn(int i)
Calculates one of the basis vectors of the rotation.
|
Vector3f |
getRotationColumn(int i,
Vector3f store)
Calculates one of the basis vectors of the rotation.
|
float |
getW()
Returns the W (real) component.
|
float |
getX()
Returns the X component.
|
float |
getY()
Returns the Y component.
|
float |
getZ()
Returns the Z component.
|
int |
hashCode()
Returns a hash code.
|
Quaternion |
inverse()
Returns the multiplicative inverse.
|
Quaternion |
inverseLocal()
Inverts the quaternion and returns the (modified) current instance.
|
boolean |
isIdentity()
Compares with the identity quaternion, without distinguishing -0 from 0.
|
boolean |
isSimilar(Quaternion other,
float epsilon)
Tests for approximate equality with the specified quaternion, using the
specified tolerance.
|
void |
loadIdentity()
Sets all components to zero except
w , which is set to 1. |
Quaternion |
lookAt(Vector3f direction,
Vector3f up)
Convenience method to set the quaternion based on a "look" (Z-axis)
direction and an "up" (Y-axis) direction.
|
Quaternion |
mult(float scalar)
Multiplies with the scalar argument and returns the product as a new
instance.
|
Quaternion |
mult(Quaternion q)
Multiplies by the argument and returns the product as a new instance.
|
Quaternion |
mult(Quaternion q,
Quaternion storeResult)
Multiplies by the specified quaternion and returns the product in a 3rd
quaternion.
|
Vector3f |
mult(Vector3f v)
Rotates the argument vector and returns the result as a new vector.
|
Vector3f |
mult(Vector3f v,
Vector3f store)
Rotates a specified vector and returns the result in another vector.
|
Quaternion |
multLocal(float scalar)
Multiplies by the scalar argument and returns the (modified) current
instance.
|
Quaternion |
multLocal(float qx,
float qy,
float qz,
float qw)
Multiplies by a quaternion with the specified components and returns the
(modified) current instance.
|
Quaternion |
multLocal(Quaternion q)
Multiplies by the argument and returns the (modified) current instance.
|
Vector3f |
multLocal(Vector3f v)
Rotates the argument vector.
|
void |
negate()
Deprecated.
The naming of this method doesn't follow convention. Please
use
normalizeLocal() instead. |
Quaternion |
negateLocal()
Negates all 4 components and returns the (modified) current instance.
|
void |
nlerp(Quaternion q2,
float blend)
Interpolates quickly between the current instance and
q2 using
nlerp, and stores the result in the current instance. |
float |
norm()
Returns the norm, defined as the dot product of the quaternion with
itself.
|
Quaternion |
normalizeLocal()
Scales the quaternion to have norm=1 and returns the (modified) current
instance.
|
Quaternion |
opposite() |
Quaternion |
opposite(Quaternion store)
Returns a rotation with the same axis and the angle increased by 180
degrees.
|
Quaternion |
oppositeLocal()
Changes the quaternion to a rotation with the same axis and the angle
increased by 180 degrees.
|
void |
read(JmeImporter importer)
De-serializes from the specified importer, for example when loading from
a J3O file.
|
void |
readExternal(java.io.ObjectInput in)
Sets the quaternion from an
ObjectInput object. |
Quaternion |
set(float x,
float y,
float z,
float w)
Sets all 4 components to specified values.
|
Quaternion |
set(Quaternion q)
Copies all 4 components from the argument.
|
void |
slerp(Quaternion q2,
float changeAmount)
Interpolates between the current instance and
q2 and stores the
result in the current instance. |
Quaternion |
slerp(Quaternion q1,
Quaternion q2,
float t)
Interpolates between the specified quaternions and stores the result in
the current instance.
|
Quaternion |
subtract(Quaternion q)
Subtracts the argument and returns difference as a new instance.
|
Quaternion |
subtractLocal(Quaternion q)
Subtracts the argument and returns the (modified) current instance.
|
float |
toAngleAxis(Vector3f axisStore)
Converts the quaternion to a rotation angle and axis of rotation, storing
the axis in the argument (if it's non-null) and returning the angle.
|
float[] |
toAngles(float[] angles)
Converts to equivalent Tait-Bryan angles, to be applied in x-z-y
intrinsic order or y-z'-x" extrinsic order, for instance by
fromAngles(float[]) . |
void |
toAxes(Vector3f[] axes)
Converts the quaternion to a rotated coordinate system and stores the
resulting axes in the argument.
|
Matrix3f |
toRotationMatrix()
Converts to an equivalent rotation matrix.
|
Matrix3f |
toRotationMatrix(Matrix3f result)
Converts to an equivalent rotation matrix.
|
Matrix4f |
toRotationMatrix(Matrix4f result)
Sets the rotation component of the specified transform matrix.
|
java.lang.String |
toString()
Returns a string representation.
|
Matrix4f |
toTransformMatrix(Matrix4f store)
Sets the rotation component of the specified transform matrix.
|
void |
write(JmeExporter e)
Serializes to the specified exporter, for example when saving to a J3O
file.
|
void |
writeExternal(java.io.ObjectOutput out)
Writes the quaternion to an
ObjectOutput object. |
public static final Quaternion IDENTITY
This is the usual representation for a null rotation.
public static final Quaternion DIRECTION_Z
public static final Quaternion ZERO
The zero quaternion doesn't represent any valid rotation.
protected float x
protected float y
protected float z
protected float w
public Quaternion()
w
, which is set to 1.public Quaternion(float x, float y, float z, float w)
x
- the desired X componenty
- the desired Y componentz
- the desired Z componentw
- the desired W componentpublic Quaternion(float[] angles)
angles
- an array of Tait-Bryan angles (in radians, exactly 3
elements, the X angle in angles[0]
, the Y angle in angles[1]
, and the Z angle in angles[2]
, not null,
unaffected)public Quaternion(Quaternion q1, Quaternion q2, float interp)
Uses
slerp(com.jme3.math.Quaternion, com.jme3.math.Quaternion, float)
,
which is fast but inaccurate.
q1
- the desired value when interp=0 (not null, unaffected)q2
- the desired value when interp=1 (not null, may be modified)interp
- the fractional change amountpublic Quaternion(Quaternion q)
q
- the quaternion to copy (not null, unaffected)public float getX()
x
componentpublic float getY()
y
componentpublic float getZ()
z
componentpublic float getW()
w
componentpublic Quaternion set(float x, float y, float z, float w)
x
- the desired X componenty
- the desired Y componentz
- the desired Z componentw
- the desired W componentpublic Quaternion set(Quaternion q)
q
- the quaternion to copy (not null, unaffected)public void loadIdentity()
w
, which is set to 1.public boolean isIdentity()
public Quaternion fromAngles(float[] angles)
angles
- an array of Tait-Bryan angles (in radians, exactly 3
elements, the X angle in angles[0]
, the Y angle in angles[1]
, and the Z angle in angles[2]
, not null,
unaffected)java.lang.IllegalArgumentException
- if angles.length != 3
public Quaternion fromAngles(float xAngle, float yAngle, float zAngle)
xAngle
- the X angle (in radians)yAngle
- the Y angle (in radians)zAngle
- the Z angle (in radians)public float[] toAngles(float[] angles)
fromAngles(float[])
. The current instance is unaffected.angles
- storage for the result, or null for a new float[3]angles
or a
new float[3], the X angle in angles[0], the Y angle in angles[1], and
the Z angle in angles[2])java.lang.IllegalArgumentException
- if angles.length != 3
public Quaternion fromRotationMatrix(Matrix3f matrix)
matrix
- the input matrix (not null, unaffected)public Quaternion fromRotationMatrix(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
m00
- the matrix element in row 0, column 0m01
- the matrix element in row 0, column 1m02
- the matrix element in row 0, column 2m10
- the matrix element in row 1, column 0m11
- the matrix element in row 1, column 1m12
- the matrix element in row 1, column 2m20
- the matrix element in row 2, column 0m21
- the matrix element in row 2, column 1m22
- the matrix element in row 2, column 2public Matrix3f toRotationMatrix()
Note: the result is created from a normalized version of the current instance.
public Matrix3f toRotationMatrix(Matrix3f result)
Note: the result is created from a normalized version of the current instance.
result
- storage for the result (not null)result
, configured as a 3x3 rotation matrixpublic Matrix4f toTransformMatrix(Matrix4f store)
Note: preserves the translation component of store
but not
its scaling component.
Note: the result is created from a normalized version of the current instance.
store
- storage for the result (not null)store
, with 9 of its 16 elements modifiedpublic Matrix4f toRotationMatrix(Matrix4f result)
Note: preserves the translation and scaling components of
result
.
Note: the result is created from a normalized version of the current instance.
result
- storage for the result (not null)result
, with 9 of its 16 elements modifiedpublic Vector3f getRotationColumn(int i)
Note: the result is created from a normalized version of the current instance.
i
- which basis vector to retrieve (≥0, <3, 0→X-axis,
1→Y-axis, 2→Z-axis)public Vector3f getRotationColumn(int i, Vector3f store)
Note: the result is created from a normalized version of the current instance.
i
- which basis vector to retrieve (≥0, <3, 0→X-axis,
1→Y-axis, 2→Z-axis)store
- storage for the result, or null for a new Vector3fstore
or a new Vector3f)java.lang.IllegalArgumentException
- if index is not 0, 1, or 2public Quaternion fromAngleAxis(float angle, Vector3f axis)
fromAngleNormalAxis(float, com.jme3.math.Vector3f)
if the axis
is known to be normalized.angle
- the desired rotation angle (in radians)axis
- the desired axis of rotation (not null, unaffected)public Quaternion fromAngleNormalAxis(float angle, Vector3f axis)
fromAngleAxis(float, com.jme3.math.Vector3f)
instead.angle
- the desired rotation angle (in radians)axis
- the desired axis of rotation (not null, length=1, unaffected)public float toAngleAxis(Vector3f axisStore)
If the quaternion has x*x + y*y + z*z == 0
, then (1,0,0) is
stored and 0 is returned. (This might happen if the rotation angle is
very close to 0.)
Otherwise, the quaternion is assumed to be normalized (norm=1). No error checking is performed; the caller must ensure that the quaternion is normalized.
In all cases, the current instance is unaffected.
axisStore
- storage for the axis (modified if not null)public Quaternion slerp(Quaternion q1, Quaternion q2, float t)
q1
- the desired value when interp=0 (not null, unaffected)q2
- the desired value when interp=1 (not null, may be modified)t
- the fractional change amountpublic void slerp(Quaternion q2, float changeAmount)
q2
and stores the
result in the current instance.
This method is often more accurate than
nlerp(com.jme3.math.Quaternion, float)
, but slower.
q2
- the desired value when changeAmnt=1 (not null, may be modified)changeAmount
- the fractional change amountpublic void nlerp(Quaternion q2, float blend)
q2
using
nlerp, and stores the result in the current instance.
This method is often faster than
slerp(com.jme3.math.Quaternion, float)
, but less accurate.
q2
- the desired value when blend=1 (not null, unaffected)blend
- the fractional change amountpublic Quaternion add(Quaternion q)
Seldom used. To combine rotations, use
mult(com.jme3.math.Quaternion)
instead of this method.
q
- the quaternion to add (not null, unaffected)public Quaternion addLocal(Quaternion q)
Seldom used. To combine rotations, use
multLocal(com.jme3.math.Quaternion)
or
mult(com.jme3.math.Quaternion, com.jme3.math.Quaternion)
instead of this method.
q
- the quaternion to add (not null, unaffected unless it's
this
)public Quaternion subtract(Quaternion q)
q
- the quaternion to subtract (not null, unaffected)public Quaternion subtractLocal(Quaternion q)
To quantify the similarity of 2 normalized quaternions, use
dot(com.jme3.math.Quaternion)
.
q
- the quaternion to subtract (not null, unaffected unless it's
this
)public Quaternion mult(Quaternion q)
This method is used to combine rotations. Note that quaternion multiplication is noncommutative, so generally q * p != p * q.
q
- the right factor (not null, unaffected)this * q
(a new Quaternion)public Quaternion mult(Quaternion q, Quaternion storeResult)
storeResult
.
This method is used to combine rotations. Note that quaternion multiplication is noncommutative, so generally q * p != p * q.
It is safe for q
and storeResult
to be the same object.
However, if this
and storeResult
are the same object, the result
is undefined.
q
- the right factor (not null, unaffected unless it's res
)storeResult
- storage for the product, or null for a new Quaternionthis * q
(either storeResult
or a new Quaternion)public void apply(Matrix3f matrix)
matrix
- the rotation matrix to apply (not null, unaffected)public Quaternion fromAxes(Vector3f[] axis)
The 3 basis vectors describe the axes of a rotated coordinate system. They are assumed to be normalized, mutually orthogonal, and in right-hand order. No error checking is performed; the caller must ensure that the specified vectors represent a right-handed coordinate system.
axis
- the array of desired basis vectors (not null, array length=3,
each vector having length=1, unaffected)java.lang.IllegalArgumentException
- if axis.length != 3
public Quaternion fromAxes(Vector3f xAxis, Vector3f yAxis, Vector3f zAxis)
The 3 basis vectors describe the axes of a rotated coordinate system. They are assumed to be normalized, mutually orthogonal, and in right-hand order. No error checking is performed; the caller must ensure that the specified vectors represent a right-handed coordinate system.
xAxis
- the X axis of the desired coordinate system (not null,
length=1, unaffected)yAxis
- the Y axis of the desired coordinate system (not null,
length=1, unaffected)zAxis
- the Z axis of the desired coordinate system (not null,
length=1, unaffected)public void toAxes(Vector3f[] axes)
The resulting vectors form the basis of a rotated coordinate system. They will be normalized, mutually orthogonal, and in right-hand order.
axes
- storage for the results (not null, length=3, each element
non-null, elements modified)java.lang.IllegalArgumentException
- if axes.length != 3
public Vector3f mult(Vector3f v)
Despite the name, the result differs from the mathematical definition of vector-quaternion multiplication.
v
- the vector to rotate (not null, unaffected)public Vector3f multLocal(Vector3f v)
Despite the name, the result differs from the mathematical definition of vector-quaternion multiplication.
v
- the vector to rotate (not null)v
public Quaternion multLocal(Quaternion q)
This method is used to combine rotations. Note that quaternion multiplication is noncommutative, so generally q * p != p * q.
q
- the right factor (not null, unaffected unless it's this
)public Quaternion multLocal(float qx, float qy, float qz, float qw)
This method is used to combine rotations. Note that quaternion multiplication is noncommutative, so generally q * p != p * q.
qx
- the X component of the right factorqy
- the Y component of the right factorqz
- the Z component of the right factorqw
- the W component of the right factorpublic Vector3f mult(Vector3f v, Vector3f store)
It is safe for v
and store
to be the same object.
Despite the name, the result differs from the mathematical definition of vector-quaternion multiplication.
v
- the vector to rotate (not null, unaffected unless it's
store
)store
- storage for the result, or null for a new Vector3fstore
or a new Vector3f)public Quaternion mult(float scalar)
scalar
- the scaling factorpublic Quaternion multLocal(float scalar)
scalar
- the scaling factorpublic float dot(Quaternion q)
This method can be used to quantify the similarity of 2 normalized quaternions.
q
- the quaternion to multiply (not null, unaffected)public float norm()
public Quaternion normalizeLocal()
public Quaternion inverse()
public Quaternion inverseLocal()
@Deprecated public void negate()
normalizeLocal()
instead.public Quaternion negateLocal()
public java.lang.String toString()
(X.XXXX, Y.YYYY, Z.ZZZZ, W.WWWW)
toString
in class java.lang.Object
Object.toString()
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
o
- the object to compare (may be null, unaffected)public boolean isSimilar(Quaternion other, float epsilon)
To quantify the similarity of 2 normalized quaternions, use
dot(com.jme3.math.Quaternion)
.
other
- the quaternion to compare (not null, unaffected)epsilon
- the tolerance for each componentpublic int hashCode()
hashCode
in class java.lang.Object
Object.hashCode()
public void readExternal(java.io.ObjectInput in) throws java.io.IOException
ObjectInput
object.
Used with serialization. Should not be invoked directly by application code.
in
- the object to read from (not null)java.io.IOException
- if the ObjectInput cannot read a floatExternalizable
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException
ObjectOutput
object.
Used with serialization. Should not be invoked directly by application code.
out
- the object to write to (not null)java.io.IOException
- if the ObjectOutput cannot write a floatExternalizable
public Quaternion lookAt(Vector3f direction, Vector3f up)
If either vector has length=0, the result is undefined.
If the vectors are parallel, the result is undefined.
direction
- the desired Z-axis direction (in local coordinates, not
null, length>0, unaffected)up
- the desired Y-axis direction (in local coordinates, not null,
length>0, unaffected, typically (0,1,0) )public void write(JmeExporter e) throws java.io.IOException
public void read(JmeImporter importer) throws java.io.IOException
public Quaternion opposite()
public Quaternion opposite(Quaternion store)
The current instance is unaffected, unless store
is
this
.
store
- storage for the result, or null for a new Quaternionstore
or a new Quaternionpublic Quaternion oppositeLocal()
public Quaternion clone()
clone
in class java.lang.Object