Class Quaternion
- All Implemented Interfaces:
Serializable
,Cloneable
Mathematically, quaternions are an extension of complex numbers. In mathematics texts, W often appears first, but in JME it always comes last.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Quaternion
Another shared instance of the identity quaternion (0, 0, 0, 1).static final Quaternion
Shared instance of the identity quaternion (0, 0, 0, 1).protected float
The real (W) component.protected float
The first imaginary (X) component.protected float
The 2nd imaginary (Y) component.protected float
The 3rd imaginary (Z) component.static final Quaternion
Shared instance of the zero quaternion (0, 0, 0, 0). -
Constructor Summary
ConstructorDescriptionInstantiates an identity quaternion: all components zeroed exceptw
, which is set to 1.Quaternion
(float x, float y, float z, float w) Instantiates a quaternion with the specified components.Instantiates a copy of the argument. -
Method Summary
Modifier and TypeMethodDescriptionAdds the argument and returns the (modified) current instance.clone()
Creates a copy.boolean
Tests for exact equality with the argument, distinguishing -0 from 0.fromAngleNormalAxis
(float angle, Vector3f axis) Sets the quaternion from the specified rotation angle and normalized axis of rotation.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.Sets the quaternion from the specified orthonormal basis.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.fromRotationMatrix
(Matrix3f matrix) Sets the quaternion from the specified rotation matrix.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.inverse()
Returns the multiplicative inverse.static boolean
isValidQuaternion
(Quaternion quaternion) Tests whether the argument is a valid quaternion, returning false if it's null or if any component is NaN or infinite.void
Sets all components to zero exceptw
, which is set to 1.mult
(Quaternion q) Multiplies by the argument and returns the product as a new instance.mult
(Quaternion q, Quaternion storeResult) Multiplies by the specified quaternion and returns the product in a 3rd quaternion.multLocal
(float scalar) Multiplies by the scalar argument and returns the (modified) current instance.multLocal
(float qx, float qy, float qz, float qw) Multiplies by a quaternion with the specified components and returns the (modified) current instance.Multiplies by the argument and returns the (modified) current instance.float
norm()
Returns the norm, defined as the dot product of the quaternion with itself.Scales the quaternion to have norm=1 and returns the (modified) current instance.set
(float x, float y, float z, float w) Sets all 4 components to specified values.set
(Quaternion q) Copies all 4 components from the argument.Converts to an equivalent rotation matrix.toRotationMatrix
(Matrix3f result) Converts to an equivalent rotation matrix.toRotationMatrix
(Matrix4f result) Sets the rotation component of the specified transform matrix.toString()
Returns a string representation of the quaternion, which is unaffected.toTransformMatrix
(Matrix4f store) Sets the rotation component of the specified transform matrix.
-
Field Details
-
IDENTITY
Shared instance of the identity quaternion (0, 0, 0, 1). Do not modify!This is the usual representation for a null rotation.
-
DIRECTION_Z
Another shared instance of the identity quaternion (0, 0, 0, 1). Do not modify! -
ZERO
Shared instance of the zero quaternion (0, 0, 0, 0). Do not modify!The zero quaternion doesn't represent any valid rotation.
-
x
protected float xThe first imaginary (X) component. Not an angle! -
y
protected float yThe 2nd imaginary (Y) component. Not an angle! -
z
protected float zThe 3rd imaginary (Z) component. Not an angle! -
w
protected float wThe real (W) component. Not an angle!
-
-
Constructor Details
-
Quaternion
public Quaternion()Instantiates an identity quaternion: all components zeroed exceptw
, which is set to 1. -
Quaternion
public Quaternion(float x, float y, float z, float w) Instantiates a quaternion with the specified components.- Parameters:
x
- the desired X componenty
- the desired Y componentz
- the desired Z componentw
- the desired W component
-
Quaternion
Instantiates a copy of the argument.- Parameters:
q
- the quaternion to copy (not null, unaffected)
-
-
Method Details
-
getX
public float getX()Returns the X component. The quaternion is unaffected.- Returns:
- the value of the
x
component
-
getY
public float getY()Returns the Y component. The quaternion is unaffected.- Returns:
- the value of the
y
component
-
getZ
public float getZ()Returns the Z component. The quaternion is unaffected.- Returns:
- the value of the
z
component
-
getW
public float getW()Returns the W (real) component. The quaternion is unaffected.- Returns:
- the value of the
w
component
-
set
Sets all 4 components to specified values.- Parameters:
x
- the desired X componenty
- the desired Y componentz
- the desired Z componentw
- the desired W component- Returns:
- the (modified) current instance (for chaining)
-
set
Copies all 4 components from the argument.- Parameters:
q
- the quaternion to copy (not null, unaffected)- Returns:
- the (modified) current instance (for chaining)
-
loadIdentity
public void loadIdentity()Sets all components to zero exceptw
, which is set to 1. -
fromAngles
Sets the quaternion from the specified Tait-Bryan angles, applying the rotations in x-z-y extrinsic order or y-z'-x" intrinsic order.- Parameters:
xAngle
- the X angle (in radians)yAngle
- the Y angle (in radians)zAngle
- the Z angle (in radians)- Returns:
- the (modified) current instance (for chaining)
- See Also:
-
fromRotationMatrix
Sets the quaternion from the specified rotation matrix.Does not verify that the argument is a valid rotation matrix. Positive scaling is compensated for, but not reflection or shear.
- Parameters:
matrix
- the input matrix (not null, unaffected)- Returns:
- the (modified) current instance (for chaining)
-
fromRotationMatrix
public 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.Does not verify that the arguments form a valid rotation matrix. Positive scaling is compensated for, but not reflection or shear.
- Parameters:
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 2- Returns:
- the (modified) current instance (for chaining)
-
toRotationMatrix
Converts to an equivalent rotation matrix. The current instance is unaffected.Note: the result is created from a normalized version of the current instance.
- Returns:
- a new 3x3 rotation matrix
-
toRotationMatrix
Converts to an equivalent rotation matrix. The current instance is unaffected.Note: the result is created from a normalized version of the current instance.
- Parameters:
result
- storage for the result (not null)- Returns:
result
, configured as a 3x3 rotation matrix
-
toTransformMatrix
Sets the rotation component of the specified transform matrix. The current instance is unaffected.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.
- Parameters:
store
- storage for the result (not null)- Returns:
store
, with 9 of its 16 elements modified
-
toRotationMatrix
Sets the rotation component of the specified transform matrix. The current instance is unaffected.Note: preserves the translation and scaling components of
result
unlessresult
includes reflection.Note: the result is created from a normalized version of the current instance.
- Parameters:
result
- storage for the result (not null)- Returns:
result
, with 9 of its 16 elements modified
-
fromAngleNormalAxis
Sets the quaternion from the specified rotation angle and normalized axis of rotation.- Parameters:
angle
- the desired rotation angle (in radians)axis
- the desired axis of rotation (not null, length=1, unaffected)- Returns:
- the (modified) current instance (for chaining)
-
addLocal
Adds the argument and returns the (modified) current instance.Seldom used. To combine rotations, use
multLocal(com.jme3.math.Quaternion)
ormult(com.jme3.math.Quaternion, com.jme3.math.Quaternion)
instead of this method.- Parameters:
q
- the quaternion to add (not null, unaffected unless it'sthis
)- Returns:
- the (modified) current instance (for chaining)
-
mult
Multiplies by the argument and returns the product as a new instance. The current instance is unaffected.This method is used to combine rotations. Note that quaternion multiplication is noncommutative, so generally q * p != p * q.
- Parameters:
q
- the right factor (not null, unaffected)- Returns:
this * q
(a new Quaternion)
-
mult
Multiplies by the specified quaternion and returns the product in a 3rd quaternion. The current instance is unaffected, unless it'sstoreResult
.This method is used to combine rotations. Note that quaternion multiplication is noncommutative, so generally q * p != p * q.
It is safe for
q
andstoreResult
to be the same object. However, ifthis
andstoreResult
are the same object, the result is undefined.- Parameters:
q
- the right factor (not null, unaffected unless it'sstoreResult
)storeResult
- storage for the product, or null for a new Quaternion- Returns:
this * q
(eitherstoreResult
or a new Quaternion)
-
fromAxes
Sets the quaternion from the specified orthonormal basis.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.
- Parameters:
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)- Returns:
- the (modified) current instance (for chaining)
-
multLocal
Multiplies by the argument and returns the (modified) current instance.This method is used to combine rotations. Note that quaternion multiplication is noncommutative, so generally q * p != p * q.
- Parameters:
q
- the right factor (not null, unaffected unless it'sthis
)- Returns:
- the (modified) current instance (for chaining)
-
multLocal
Multiplies by a quaternion with the specified components and returns the (modified) current instance.This method is used to combine rotations. Note that quaternion multiplication is noncommutative, so generally q * p != p * q.
- Parameters:
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 factor- Returns:
- the (modified) current instance (for chaining)
-
multLocal
Multiplies by the scalar argument and returns the (modified) current instance.- Parameters:
scalar
- the scaling factor- Returns:
- the (modified) current instance (for chaining)
-
norm
public float norm()Returns the norm, defined as the dot product of the quaternion with itself. The current instance is unaffected.- Returns:
- the sum of the squared components (not negative)
-
normalizeLocal
Scales the quaternion to have norm=1 and returns the (modified) current instance. For a quaternion with norm=0, the result is undefined.- Returns:
- the (modified) current instance (for chaining)
-
inverse
Returns the multiplicative inverse. For a quaternion with norm=0, null is returned. Either way, the current instance is unaffected.- Returns:
- a new Quaternion or null
-
toString
Returns a string representation of the quaternion, which is unaffected. For example, the identity quaternion is represented by:(0.0, 0.0, 0.0, 1.0)
-
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. -
hashCode
public int hashCode()Returns a hash code. If two quaternions have identical values, they will have the same hash code. The current instance is unaffected. -
clone
Creates a copy. The current instance is unaffected. -
isValidQuaternion
Tests whether the argument is a valid quaternion, returning false if it's null or if any component is NaN or infinite.- Parameters:
quaternion
- the quaternion to test (unaffected)- Returns:
- true if non-null and finite, otherwise false
-