- java.lang.Object
-
- org.spongepowered.math.imaginary.Quaternionf
-
- All Implemented Interfaces:
Serializable
,Comparable<Quaternionf>
,Imaginaryf
@Immutable public final class Quaternionf extends Object implements Imaginaryf, Comparable<Quaternionf>, Serializable
Represent a quaternion of the formxi + yj + zk + w
.The x, y, z and w components are stored as
float
s. This class is immutable.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static Quaternionf
IDENTITY
An immutable identity (0, 0, 0, 1) quaternion.static Quaternionf
ZERO
An immutable identity (0, 0, 0, 0) quaternion.
-
Constructor Summary
Constructors Constructor Description Quaternionf(double x, double y, double z, double w)
Constructs a new quaternion from the double components.Quaternionf(float x, float y, float z, float w)
Constructs a new quaternion from the float components.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Quaternionf
add(double x, double y, double z, double w)
Adds the double components of another quaternion to this one.Quaternionf
add(float x, float y, float z, float w)
Adds the float components of another quaternion to this one.Quaternionf
add(Quaternionf q)
Adds another quaternion to this one.Vector3f
axesAnglesDeg()
Returns the angles in degrees around the x, y and z axes that correspond to the rotation represented by this quaternion.Vector3f
axesAnglesRad()
Returns the angles in radians around the x, y and z axes that correspond to the rotation represented by this quaternion.Vector3f
axis()
Returns the axis of rotation for this quaternion.int
compareTo(Quaternionf q)
Quaternionf
conjugate()
Conjugates the quaternion.Vector3f
direction()
Returns a unit vector representing the direction of this quaternion, which isVector3f.FORWARD
rotated by this quaternion.Quaternionf
div(double a)
Divides the components of this quaternion by a double scalar.Quaternionf
div(double x, double y, double z, double w)
Divides this quaternions by the double components of another one.Quaternionf
div(float a)
Divides the components of this quaternion by a float scalar.Quaternionf
div(float x, float y, float z, float w)
Divides this quaternions by the float components of another one.Quaternionf
div(Quaternionf q)
Divides this quaternions by another one.float
dot(double x, double y, double z, double w)
Returns the dot product of this quaternion with the double components of another one.float
dot(float x, float y, float z, float w)
Returns the dot product of this quaternion with the float components of another one.float
dot(Quaternionf q)
Returns the dot product of this quaternion with another one.boolean
equals(Object other)
static Quaternionf
from(float x, float y, float z, float w)
Creates a new quaternion from the float components.static Quaternionf
fromAngleDegAxis(double angle, double x, double y, double z)
Creates a new quaternion from the rotation double angle in degrees around the axis vector double components.static Quaternionf
fromAngleDegAxis(double angle, Vector3f axis)
Creates a new quaternion from the rotation double angle in degrees around the axis vector.static Quaternionf
fromAngleDegAxis(float angle, float x, float y, float z)
Creates a new quaternion from the rotation float angle in degrees around the axis vector float components.static Quaternionf
fromAngleDegAxis(float angle, Vector3f axis)
Creates a new quaternion from the rotation float angle in degrees around the axis vector.static Quaternionf
fromAngleRadAxis(double angle, double x, double y, double z)
Creates a new quaternion from the rotation double angle in radians around the axis vector double components.static Quaternionf
fromAngleRadAxis(double angle, Vector3f axis)
Creates a new quaternion from the rotation double angle in radians around the axis vector.static Quaternionf
fromAngleRadAxis(float angle, float x, float y, float z)
Creates a new quaternion from the rotation float angle in radians around the axis vector float components.static Quaternionf
fromAngleRadAxis(float angle, Vector3f axis)
Creates a new quaternion from the rotation float angle in radians around the axis vector.static Quaternionf
fromAxesAnglesDeg(double pitch, double yaw, double roll)
Creates a new quaternion from the double angles in degrees around the x, y and z axes.static Quaternionf
fromAxesAnglesDeg(float pitch, float yaw, float roll)
Creates a new quaternion from the float angles in degrees around the x, y and z axes.static Quaternionf
fromAxesAnglesRad(double pitch, double yaw, double roll)
Creates a new quaternion from the double angles in radians around the x, y and z axes.static Quaternionf
fromAxesAnglesRad(float pitch, float yaw, float roll)
Creates a new quaternion from the float angles in radians around the x, y and z axes.static Quaternionf
fromImaginary(float x, float y, float z)
Creates a new quaternion from the float imaginary components.static Quaternionf
fromReal(float w)
Creates a new quaternion from the float real component.static Quaternionf
fromRotationMatrix(Matrix3f matrix)
Creates a new quaternion from the rotation matrix.static Quaternionf
fromRotationTo(Vector3f from, Vector3f to)
Creates a new quaternion from the angle-axis rotation defined from the first to the second vector.int
hashCode()
Quaternionf
invert()
Inverts the quaternion.float
length()
Returns the length of this quaternion.float
lengthSquared()
Returns the square of the length of this quaternion.Quaternionf
mul(double a)
Multiplies the components of this quaternion by a double scalar.Quaternionf
mul(double x, double y, double z, double w)
Multiplies the double components of another quaternion with this one.Quaternionf
mul(float a)
Multiplies the components of this quaternion by a float scalar.Quaternionf
mul(float x, float y, float z, float w)
Multiplies the float components of another quaternion with this one.Quaternionf
mul(Quaternionf q)
Multiplies another quaternion with this one.Quaternionf
normalize()
Normalizes this quaternion.Vector3f
rotate(double x, double y, double z)
Rotates the double components of a vector by this quaternion.Vector3f
rotate(float x, float y, float z)
Rotates the float components of a vector by this quaternion.Vector3f
rotate(Vector3f v)
Rotates a vector by this quaternion.Quaternionf
sub(double x, double y, double z, double w)
Subtracts the double components of another quaternion from this one.Quaternionf
sub(float x, float y, float z, float w)
Subtracts the float components of another quaternion from this one.Quaternionf
sub(Quaternionf q)
Subtracts another quaternion from this one.Complexf
toComplex()
Converts this quaternion to a complex by extracting the rotation around the axis and returning it as a rotation in the plane perpendicular to the rotation axis.Quaterniond
toDouble()
Quaternionf
toFloat()
String
toString()
float
w()
Gets the w (real) component of this quaternion.float
x()
Gets the x (imaginary) component of this quaternion.float
y()
Gets the y (imaginary) component of this quaternion.float
z()
Gets the z (imaginary) component of this quaternion.
-
-
-
Field Detail
-
ZERO
public static final Quaternionf ZERO
An immutable identity (0, 0, 0, 0) quaternion.
-
IDENTITY
public static final Quaternionf IDENTITY
An immutable identity (0, 0, 0, 1) quaternion.
-
-
Constructor Detail
-
Quaternionf
public Quaternionf(double x, double y, double z, double w)
Constructs a new quaternion from the double components.- Parameters:
x
- The x (imaginary) componenty
- The y (imaginary) componentz
- The z (imaginary) componentw
- The w (real) component
-
Quaternionf
public Quaternionf(float x, float y, float z, float w)
Constructs a new quaternion from the float components.- Parameters:
x
- The x (imaginary) componenty
- The y (imaginary) componentz
- The z (imaginary) componentw
- The w (real) component
-
-
Method Detail
-
x
public float x()
Gets the x (imaginary) component of this quaternion.- Returns:
- The x (imaginary) component
-
y
public float y()
Gets the y (imaginary) component of this quaternion.- Returns:
- The y (imaginary) component
-
z
public float z()
Gets the z (imaginary) component of this quaternion.- Returns:
- The z (imaginary) component
-
w
public float w()
Gets the w (real) component of this quaternion.- Returns:
- The w (real) component
-
add
public Quaternionf add(Quaternionf q)
Adds another quaternion to this one.- Parameters:
q
- The quaternion to add- Returns:
- A new quaternion, which is the sum of both
-
add
public Quaternionf add(double x, double y, double z, double w)
Adds the double components of another quaternion to this one.- Parameters:
x
- The x (imaginary) component of the quaternion to addy
- The y (imaginary) component of the quaternion to addz
- The z (imaginary) component of the quaternion to addw
- The w (real) component of the quaternion to add- Returns:
- A new quaternion, which is the sum of both
-
add
public Quaternionf add(float x, float y, float z, float w)
Adds the float components of another quaternion to this one.- Parameters:
x
- The x (imaginary) component of the quaternion to addy
- The y (imaginary) component of the quaternion to addz
- The z (imaginary) component of the quaternion to addw
- The w (real) component of the quaternion to add- Returns:
- A new quaternion, which is the sum of both
-
sub
public Quaternionf sub(Quaternionf q)
Subtracts another quaternion from this one.- Parameters:
q
- The quaternion to subtract- Returns:
- A new quaternion, which is the difference of both
-
sub
public Quaternionf sub(double x, double y, double z, double w)
Subtracts the double components of another quaternion from this one.- Parameters:
x
- The x (imaginary) component of the quaternion to subtracty
- The y (imaginary) component of the quaternion to subtractz
- The z (imaginary) component of the quaternion to subtractw
- The w (real) component of the quaternion to subtract- Returns:
- A new quaternion, which is the difference of both
-
sub
public Quaternionf sub(float x, float y, float z, float w)
Subtracts the float components of another quaternion from this one.- Parameters:
x
- The x (imaginary) component of the quaternion to subtracty
- The y (imaginary) component of the quaternion to subtractz
- The z (imaginary) component of the quaternion to subtractw
- The w (real) component of the quaternion to subtract- Returns:
- A new quaternion, which is the difference of both
-
mul
public Quaternionf mul(double a)
Multiplies the components of this quaternion by a double scalar.- Parameters:
a
- The multiplication scalar- Returns:
- A new quaternion, which has each component multiplied by the scalar
-
mul
public Quaternionf mul(float a)
Multiplies the components of this quaternion by a float scalar.- Specified by:
mul
in interfaceImaginaryf
- Parameters:
a
- The multiplication scalar- Returns:
- A new quaternion, which has each component multiplied by the scalar
-
mul
public Quaternionf mul(Quaternionf q)
Multiplies another quaternion with this one.- Parameters:
q
- The quaternion to multiply with- Returns:
- A new quaternion, which is the product of both
-
mul
public Quaternionf mul(double x, double y, double z, double w)
Multiplies the double components of another quaternion with this one.- Parameters:
x
- The x (imaginary) component of the quaternion to multiply withy
- The y (imaginary) component of the quaternion to multiply withz
- The z (imaginary) component of the quaternion to multiply withw
- The w (real) component of the quaternion to multiply with- Returns:
- A new quaternion, which is the product of both
-
mul
public Quaternionf mul(float x, float y, float z, float w)
Multiplies the float components of another quaternion with this one.- Parameters:
x
- The x (imaginary) component of the quaternion to multiply withy
- The y (imaginary) component of the quaternion to multiply withz
- The z (imaginary) component of the quaternion to multiply withw
- The w (real) component of the quaternion to multiply with- Returns:
- A new quaternion, which is the product of both
-
div
public Quaternionf div(double a)
Divides the components of this quaternion by a double scalar.- Parameters:
a
- The division scalar- Returns:
- A new quaternion, which has each component divided by the scalar
-
div
public Quaternionf div(float a)
Divides the components of this quaternion by a float scalar.- Specified by:
div
in interfaceImaginaryf
- Parameters:
a
- The division scalar- Returns:
- A new quaternion, which has each component divided by the scalar
-
div
public Quaternionf div(Quaternionf q)
Divides this quaternions by another one.- Parameters:
q
- The quaternion to divide with- Returns:
- The quotient of the two quaternions
-
div
public Quaternionf div(double x, double y, double z, double w)
Divides this quaternions by the double components of another one.- Parameters:
x
- The x (imaginary) component of the quaternion to divide withy
- The y (imaginary) component of the quaternion to divide withz
- The z (imaginary) component of the quaternion to divide withw
- The w (real) component of the quaternion to divide with- Returns:
- The quotient of the two quaternions
-
div
public Quaternionf div(float x, float y, float z, float w)
Divides this quaternions by the float components of another one.- Parameters:
x
- The x (imaginary) component of the quaternion to divide withy
- The y (imaginary) component of the quaternion to divide withz
- The z (imaginary) component of the quaternion to divide withw
- The w (real) component of the quaternion to divide with- Returns:
- The quotient of the two quaternions
-
dot
public float dot(Quaternionf q)
Returns the dot product of this quaternion with another one.- Parameters:
q
- The quaternion to calculate the dot product with- Returns:
- The dot product of the two quaternions
-
dot
public float dot(double x, double y, double z, double w)
Returns the dot product of this quaternion with the double components of another one.- Parameters:
x
- The x (imaginary) component of the quaternion to calculate the dot product withy
- The y (imaginary) component of the quaternion to calculate the dot product withz
- The z (imaginary) component of the quaternion to calculate the dot product withw
- The w (real) component of the quaternion to calculate the dot product with- Returns:
- The dot product of the two quaternions
-
dot
public float dot(float x, float y, float z, float w)
Returns the dot product of this quaternion with the float components of another one.- Parameters:
x
- The x (imaginary) component of the quaternion to calculate the dot product withy
- The y (imaginary) component of the quaternion to calculate the dot product withz
- The z (imaginary) component of the quaternion to calculate the dot product withw
- The w (real) component of the quaternion to calculate the dot product with- Returns:
- The dot product of the two quaternions
-
rotate
public Vector3f rotate(Vector3f v)
Rotates a vector by this quaternion.- Parameters:
v
- The vector to rotate- Returns:
- The rotated vector
-
rotate
public Vector3f rotate(double x, double y, double z)
Rotates the double components of a vector by this quaternion.- Parameters:
x
- The x component of the vectory
- The y component of the vectorz
- The z component of the vector- Returns:
- The rotated vector
-
rotate
public Vector3f rotate(float x, float y, float z)
Rotates the float components of a vector by this quaternion.- Parameters:
x
- The x component of the vectory
- The y component of the vectorz
- The z component of the vector- Returns:
- The rotated vector
-
direction
public Vector3f direction()
Returns a unit vector representing the direction of this quaternion, which isVector3f.FORWARD
rotated by this quaternion.- Returns:
- The vector representing the direction this quaternion is pointing to
-
axis
public Vector3f axis()
Returns the axis of rotation for this quaternion.- Returns:
- The axis of rotation
-
axesAnglesDeg
public Vector3f axesAnglesDeg()
Returns the angles in degrees around the x, y and z axes that correspond to the rotation represented by this quaternion.- Returns:
- The angle in degrees for each axis, stored in a vector, in the corresponding component
-
axesAnglesRad
public Vector3f axesAnglesRad()
Returns the angles in radians around the x, y and z axes that correspond to the rotation represented by this quaternion.- Returns:
- The angle in radians for each axis, stored in a vector, in the corresponding component
-
conjugate
public Quaternionf conjugate()
Conjugates the quaternion.Conjugation of a quaternion
a
is an operation returning quaterniona'
such thata' * a = a * a' = |a|<sup>2</sup>
where|a|<sup>2</sup>
is squared length ofa
.- Specified by:
conjugate
in interfaceImaginaryf
- Returns:
- the conjugated quaternion
-
invert
public Quaternionf invert()
Inverts the quaternion.Inversion of a quaternion
a
returns quaterniona<sup>-1</sup> = a' / |a|<sup>2</sup>
wherea'
isconjugation
ofa
, and|a|<sup>2</sup>
is squared length ofa
.For any quaternions
a, b, c
such thata * b = c
, the equationsa<sup>-1</sup> * c = b
andc * b<sup>-1</sup> = a
are true.- Specified by:
invert
in interfaceImaginaryf
- Returns:
- the inverted quaternion
-
lengthSquared
public float lengthSquared()
Returns the square of the length of this quaternion.- Specified by:
lengthSquared
in interfaceImaginaryf
- Returns:
- The square of the length
-
length
public float length()
Returns the length of this quaternion.- Specified by:
length
in interfaceImaginaryf
- Returns:
- The length
-
normalize
public Quaternionf normalize()
Normalizes this quaternion.- Specified by:
normalize
in interfaceImaginaryf
- Returns:
- A new quaternion of unit length
-
toComplex
public Complexf toComplex()
Converts this quaternion to a complex by extracting the rotation around the axis and returning it as a rotation in the plane perpendicular to the rotation axis.- Returns:
- The rotation without the axis as a complex
-
toFloat
public Quaternionf toFloat()
- Specified by:
toFloat
in interfaceImaginaryf
-
toDouble
public Quaterniond toDouble()
- Specified by:
toDouble
in interfaceImaginaryf
-
compareTo
public int compareTo(Quaternionf q)
- Specified by:
compareTo
in interfaceComparable<Quaternionf>
-
fromReal
public static Quaternionf fromReal(float w)
Creates a new quaternion from the float real component.The
ZERO
constant is re-used whenw
is 0.- Parameters:
w
- The w (real) component- Returns:
- The quaternion created from the float real component
-
fromImaginary
public static Quaternionf fromImaginary(float x, float y, float z)
Creates a new quaternion from the float imaginary components.The
ZERO
constant is re-used whenx
,y
, andz
are 0.- Parameters:
x
- The x (imaginary) componenty
- The y (imaginary) componentz
- The z (imaginary) component- Returns:
- The quaternion created from the float imaginary components
-
from
public static Quaternionf from(float x, float y, float z, float w)
Creates a new quaternion from the float components.The
ZERO
constant is re-used whenx
,y
,z
, andw
are 0.- Parameters:
x
- The x (imaginary) componenty
- The y (imaginary) componentz
- The z (imaginary) componentw
- The w (real) component- Returns:
- The quaternion created from the float components
-
fromAxesAnglesDeg
public static Quaternionf fromAxesAnglesDeg(double pitch, double yaw, double roll)
Creates a new quaternion from the double angles in degrees around the x, y and z axes.- Parameters:
pitch
- The rotation around xyaw
- The rotation around yroll
- The rotation around z- Returns:
- The quaternion defined by the rotations around the axes
-
fromAxesAnglesRad
public static Quaternionf fromAxesAnglesRad(double pitch, double yaw, double roll)
Creates a new quaternion from the double angles in radians around the x, y and z axes.- Parameters:
pitch
- The rotation around xyaw
- The rotation around yroll
- The rotation around z- Returns:
- The quaternion defined by the rotations around the axes
-
fromAxesAnglesDeg
public static Quaternionf fromAxesAnglesDeg(float pitch, float yaw, float roll)
Creates a new quaternion from the float angles in degrees around the x, y and z axes.- Parameters:
pitch
- The rotation around xyaw
- The rotation around yroll
- The rotation around z- Returns:
- The quaternion defined by the rotations around the axes
-
fromAxesAnglesRad
public static Quaternionf fromAxesAnglesRad(float pitch, float yaw, float roll)
Creates a new quaternion from the float angles in radians around the x, y and z axes.- Parameters:
pitch
- The rotation around xyaw
- The rotation around yroll
- The rotation around z- Returns:
- The quaternion defined by the rotations around the axes
-
fromRotationTo
public static Quaternionf fromRotationTo(Vector3f from, Vector3f to)
Creates a new quaternion from the angle-axis rotation defined from the first to the second vector.- Parameters:
from
- The first vectorto
- The second vector- Returns:
- The quaternion defined by the angle-axis rotation between the vectors
-
fromAngleDegAxis
public static Quaternionf fromAngleDegAxis(double angle, Vector3f axis)
Creates a new quaternion from the rotation double angle in degrees around the axis vector.- Parameters:
angle
- The rotation angle in degreesaxis
- The axis of rotation- Returns:
- The quaternion defined by the rotation around the axis
-
fromAngleRadAxis
public static Quaternionf fromAngleRadAxis(double angle, Vector3f axis)
Creates a new quaternion from the rotation double angle in radians around the axis vector.- Parameters:
angle
- The rotation angle in radiansaxis
- The axis of rotation- Returns:
- The quaternion defined by the rotation around the axis
-
fromAngleDegAxis
public static Quaternionf fromAngleDegAxis(float angle, Vector3f axis)
Creates a new quaternion from the rotation float angle in degrees around the axis vector.- Parameters:
angle
- The rotation angle in degreesaxis
- The axis of rotation- Returns:
- The quaternion defined by the rotation around the axis
-
fromAngleRadAxis
public static Quaternionf fromAngleRadAxis(float angle, Vector3f axis)
Creates a new quaternion from the rotation float angle in radians around the axis vector.- Parameters:
angle
- The rotation angle in radiansaxis
- The axis of rotation- Returns:
- The quaternion defined by the rotation around the axis
-
fromAngleDegAxis
public static Quaternionf fromAngleDegAxis(double angle, double x, double y, double z)
Creates a new quaternion from the rotation double angle in degrees around the axis vector double components.- Parameters:
angle
- The rotation angle in degreesx
- The x component of the axis vectory
- The y component of the axis vectorz
- The z component of the axis vector- Returns:
- The quaternion defined by the rotation around the axis
-
fromAngleRadAxis
public static Quaternionf fromAngleRadAxis(double angle, double x, double y, double z)
Creates a new quaternion from the rotation double angle in radians around the axis vector double components.- Parameters:
angle
- The rotation angle in radiansx
- The x component of the axis vectory
- The y component of the axis vectorz
- The z component of the axis vector- Returns:
- The quaternion defined by the rotation around the axis
-
fromAngleDegAxis
public static Quaternionf fromAngleDegAxis(float angle, float x, float y, float z)
Creates a new quaternion from the rotation float angle in degrees around the axis vector float components.- Parameters:
angle
- The rotation angle in degreesx
- The x component of the axis vectory
- The y component of the axis vectorz
- The z component of the axis vector- Returns:
- The quaternion defined by the rotation around the axis
-
fromAngleRadAxis
public static Quaternionf fromAngleRadAxis(float angle, float x, float y, float z)
Creates a new quaternion from the rotation float angle in radians around the axis vector float components.- Parameters:
angle
- The rotation angle in radiansx
- The x component of the axis vectory
- The y component of the axis vectorz
- The z component of the axis vector- Returns:
- The quaternion defined by the rotation around the axis
-
fromRotationMatrix
public static Quaternionf fromRotationMatrix(Matrix3f matrix)
Creates a new quaternion from the rotation matrix. The matrix will be interpreted as a rotation matrix even if it is not.- Parameters:
matrix
- The rotation matrix- Returns:
- The quaternion defined by the rotation matrix
-
-