Package icyllis.arc3d.core
Class Quaternion
java.lang.Object
icyllis.arc3d.core.Quaternion
Represents a Quaternion, mostly used for rotations.
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreate a zero quaternion.Quaternion
(float x, float y, float z, float w) Create a quaternion with given components. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(Quaternion q) Add a quaternion to this quaternion.void
Conjugate this quaternion[-x, -y, -z, w]
.copy()
static Quaternion
copy
(Quaternion q) Create a copy ofq
if it's not null, or an identity quaternion otherwise.float
dot
(float x, float y, float z, float w) Returns the dot product of this quaternion with the given x, y, z and w components.float
dot
(Quaternion q) Returns the dot product of this quaternion with the given quaternion.boolean
int
hashCode()
static Quaternion
identity()
Create a new identity quaternion.void
inverse()
Calculate the inverse of this quaternion.boolean
Returns whether this quaternion is equivalent to given quaternion.boolean
Calculate whether this quaternion is equivalent to an identity quaternion.boolean
Returns whether this quaternion is normalized.float
length()
Returns the magnitude of this quaternion, namely the 2-norm (euclidean norm) or the length.float
lengthSq()
static Quaternion
makeAxisAngle
(float axisX, float axisY, float axisZ, float angle) Create a quaternion from the given axis and angle.static Quaternion
makeAxisAngle
(Vector3 axis, float angle) Create a quaternion from the given axis and angle.static Quaternion
makeEulerAngles
(float rotationX, float rotationY, float rotationZ) Create a quaternion from the given Euler rotation angles in radians.void
multiply
(float s) Multiply this quaternion by a factor.void
Multiply this quaternion by some other quaternion.void
negate()
Negate this quaternion[-x, -y, -z, -w]
.void
Normalize this quaternion to unit length.void
rotateByAxis
(float axisX, float axisY, float axisZ, float angle) Rotate this quaternion by the given axis and angle.void
rotateByAxis
(Vector3 axis, float angle) Rotate this quaternion by the given axis and angle.void
rotateByEuler
(float rotationX, float rotationY, float rotationZ) Rotates this quaternion from the given Euler rotation angles in radians.void
rotateX
(float angle) Rotate this quaternion around X axis with the given angle in radians.void
rotateY
(float angle) Rotate this quaternion around Y axis with the given angle in radians.void
rotateZ
(float angle) Rotate this quaternion around Z axis with the given angle in radians.void
set
(float x, float y, float z, float w) Set all values of this quaternion using the given components.void
set
(Quaternion q) Set all values of this quaternion using the given quaternion.void
setFromAxisAngle
(float axisX, float axisY, float axisZ, float angle) Set this quaternion components from the given axis and angle.void
setFromAxisAngle
(Vector3 axis, float angle) Set this quaternion components from the given axis and angle.void
setFromEulerAngles
(float rotationX, float rotationY, float rotationZ) Set this quaternion components from the given Euler rotation angles in radians.void
Set this quaternion to the identity quaternion[0, 0, 0, 1]
.void
setZero()
Set this quaternion to the zero quaternion.void
slerp
(Quaternion a, float t) Set this quaternion to a spherical linear interpolation between this and given quaternion by a factor.void
slerp
(Quaternion a, Quaternion b, float t) Set this quaternion to a spherical linear interpolation between two quaternions by the given factor.void
Subtract a quaternion from this quaternion.float
toAxisAngle
(float[] axis) Transform the rotational quaternion to axis based rotation angles.float
toAxisAngle
(Vector3 axis) Transform the rotational quaternion to axis based rotation angles.void
toEulerAngles
(float[] angles) Transform this quaternion to Euler rotation angles in radians (pitchX, yawY and rollZ).void
toEulerAngles
(Vector3 result) Transform this quaternion to Euler rotation angles in radians (pitchX, yawY and rollZ).Transform this quaternion to a normalized 3x3 row-major matrix representing the rotation.Transform this quaternion to a normalized 3x3 row-major matrix representing the rotation.Transform this quaternion to a normalized 4x4 row-major matrix representing the rotation.Transform this quaternion to a normalized 4x4 row-major matrix representing the rotation.toString()
-
Field Details
-
x
protected float x -
y
protected float y -
z
protected float z -
w
protected float w
-
-
Constructor Details
-
Quaternion
public Quaternion()Create a zero quaternion. -
Quaternion
public Quaternion(float x, float y, float z, float w) Create a quaternion with given components.- Parameters:
x
- the x-component of the quaterniony
- the y-component of the quaternionz
- the z-component of the quaternionw
- the w-component of the quaternion
-
-
Method Details
-
copy
Create a copy ofq
if it's not null, or an identity quaternion otherwise.- Parameters:
q
- the quaternion to copy from- Returns:
- a copy of the quaternion
-
identity
Create a new identity quaternion.- Returns:
- an identity quaternion
-
makeEulerAngles
@Nonnull public static Quaternion makeEulerAngles(float rotationX, float rotationY, float rotationZ) Create a quaternion from the given Euler rotation angles in radians. The returned quaternion will be a normalized rotational quaternion.- Parameters:
rotationX
- the Euler pitch angle in radians. (rotation about the X axis)rotationY
- the Euler yaw angle in radians. (rotation about the Y axis)rotationZ
- the Euler roll angle in radians. (rotation about the Z axis)- Returns:
- the resulting quaternion
-
makeAxisAngle
Create a quaternion from the given axis and angle. The axis must be a normalized (unit) vector.- Parameters:
axis
- the rotation axisangle
- rotation angle in radians- Returns:
- the resulting quaternion
-
makeAxisAngle
Create a quaternion from the given axis and angle. The axis must be a normalized (unit) vector.- Parameters:
axisX
- x-coordinate of rotation axisaxisY
- y-coordinate of rotation axisaxisZ
- z-coordinate of rotation axisangle
- rotation angle in radians- Returns:
- the resulting quaternion
-
set
public void set(float x, float y, float z, float w) Set all values of this quaternion using the given components.- Parameters:
x
- the x-component to sety
- the y-component to setz
- the z-component to setw
- the w-component to set
-
set
Set all values of this quaternion using the given quaternion.- Parameters:
q
- the quaternion to copy from
-
add
Add a quaternion to this quaternion.- Parameters:
q
- the quaternion to add
-
subtract
Subtract a quaternion from this quaternion.- Parameters:
q
- the quaternion to subtract
-
multiply
public void multiply(float s) Multiply this quaternion by a factor.- Parameters:
s
- the factor to multiply.
-
multiply
Multiply this quaternion by some other quaternion.- Parameters:
q
- the quaternion to multiply with
-
length
public float length()Returns the magnitude of this quaternion, namely the 2-norm (euclidean norm) or the length.- Returns:
- the magnitude of this quaternion
-
lengthSq
public float lengthSq() -
dot
public float dot(float x, float y, float z, float w) Returns the dot product of this quaternion with the given x, y, z and w components. -
dot
Returns the dot product of this quaternion with the given quaternion.- Parameters:
q
- the quaternion to dot product with.
-
inverse
public void inverse()Calculate the inverse of this quaternion. If rotational, it will produce the inverse rotation. -
isNormalized
public boolean isNormalized()Returns whether this quaternion is normalized.- Returns:
true
if is normalized,false
otherwise
-
normalize
public void normalize()Normalize this quaternion to unit length. -
setZero
public void setZero()Set this quaternion to the zero quaternion. -
isIdentity
public boolean isIdentity()Calculate whether this quaternion is equivalent to an identity quaternion.- Returns:
true
if this quaternion is identity.
-
setIdentity
public void setIdentity()Set this quaternion to the identity quaternion[0, 0, 0, 1]
. -
conjugate
public void conjugate()Conjugate this quaternion[-x, -y, -z, w]
. -
negate
public void negate()Negate this quaternion[-x, -y, -z, -w]
. -
slerp
Set this quaternion to a spherical linear interpolation between this and given quaternion by a factor. The two quaternions should be rotational so they should be normalized. This method does not normalize this quaternion.- Parameters:
a
- the end quaterniont
- a value between 0.0 and 1.0 representing interpolation
-
slerp
Set this quaternion to a spherical linear interpolation between two quaternions by the given factor. The two quaternions should be rotational so they should be normalized. This method does not normalize this quaternion.- Parameters:
a
- the start quaternionb
- the end quaterniont
- a value between 0.0 and 1.0 representing interpolation
-
rotateByAxis
Rotate this quaternion by the given axis and angle. The axis must be a normalized (unit) vector.- Parameters:
axis
- the rotation axisangle
- rotation angle in radians
-
rotateByAxis
public void rotateByAxis(float axisX, float axisY, float axisZ, float angle) Rotate this quaternion by the given axis and angle. The axis must be a normalized (unit) vector.- Parameters:
axisX
- x-coordinate of rotation axisaxisY
- y-coordinate of rotation axisaxisZ
- z-coordinate of rotation axisangle
- rotation angle in radians
-
rotateX
public void rotateX(float angle) Rotate this quaternion around X axis with the given angle in radians.- Parameters:
angle
- rotation angle in radians
-
rotateY
public void rotateY(float angle) Rotate this quaternion around Y axis with the given angle in radians.- Parameters:
angle
- rotation angle in radians
-
rotateZ
public void rotateZ(float angle) Rotate this quaternion around Z axis with the given angle in radians.- Parameters:
angle
- rotation angle in radians
-
rotateByEuler
public void rotateByEuler(float rotationX, float rotationY, float rotationZ) Rotates this quaternion from the given Euler rotation angles in radians.The rotations are applied in the given order and using chained rotation per axis:
- x - pitch -
rotateX(float)
- y - yaw -
rotateY(float)
- z - roll -
rotateZ(float)
- Parameters:
rotationX
- the Euler pitch angle in radians. (rotation about the X axis)rotationY
- the Euler yaw angle in radians. (rotation about the Y axis)rotationZ
- the Euler roll angle in radians. (rotation about the Z axis)- See Also:
- x - pitch -
-
setFromAxisAngle
Set this quaternion components from the given axis and angle. The axis must be a normalized (unit) vector.- Parameters:
axis
- the rotation axisangle
- rotation angle in radians
-
setFromAxisAngle
public void setFromAxisAngle(float axisX, float axisY, float axisZ, float angle) Set this quaternion components from the given axis and angle. The axis must be a normalized (unit) vector.- Parameters:
axisX
- x-coordinate of rotation axisaxisY
- y-coordinate of rotation axisaxisZ
- z-coordinate of rotation axisangle
- rotation angle in radians
-
setFromEulerAngles
public void setFromEulerAngles(float rotationX, float rotationY, float rotationZ) Set this quaternion components from the given Euler rotation angles in radians.- Parameters:
rotationX
- the Euler pitch angle in radians. (rotation about the X axis)rotationY
- the Euler yaw angle in radians. (rotation about the Y axis)rotationZ
- the Euler roll angle in radians. (rotation about the Z axis)
-
toAxisAngle
Transform the rotational quaternion to axis based rotation angles.- Parameters:
axis
- the vector for storing the axis coordinates.- Returns:
- the rotation angle in radians.
-
toAxisAngle
public float toAxisAngle(@Nonnull float[] axis) Transform the rotational quaternion to axis based rotation angles.- Parameters:
axis
- the array for storing the axis coordinates.- Returns:
- the rotation angle in radians.
-
toEulerAngles
Transform this quaternion to Euler rotation angles in radians (pitchX, yawY and rollZ).- Parameters:
result
- the vector for storing the result.
-
toEulerAngles
public void toEulerAngles(@Nonnull float[] angles) Transform this quaternion to Euler rotation angles in radians (pitchX, yawY and rollZ).- Parameters:
angles
- the array for storing the result.
-
toMatrix3
Transform this quaternion to a normalized 3x3 row-major matrix representing the rotation.- Returns:
- the resulting matrix
-
toMatrix4
Transform this quaternion to a normalized 4x4 row-major matrix representing the rotation.- Returns:
- the resulting matrix
-
toMatrix3
Transform this quaternion to a normalized 3x3 row-major matrix representing the rotation. If recycle matrix is null, a new matrix will be returned.- Parameters:
out
- a matrix for storing the result- Returns:
- the resulting matrix
-
toMatrix4
Transform this quaternion to a normalized 4x4 row-major matrix representing the rotation. If recycle matrix is null, a new matrix will be returned.- Parameters:
out
- a matrix for storing the result- Returns:
- the resulting matrix
-
isApproxEqual
Returns whether this quaternion is equivalent to given quaternion.- Parameters:
q
- the quaternion to compare.- Returns:
true
if this quaternion is equivalent to other one.
-
equals
-
hashCode
public int hashCode() -
toString
-
copy
-