Quaternion¶
Quaternion.
Description¶
A unit quaternion used for representing 3D rotations. Quaternions need to be normalized to be used for rotation.
It is similar to Basis, which implements matrix representation of rotations, and can be parametrized using both an axis-angle pair or Euler angles. Basis stores rotation, scale, and shearing, while Quaternion only stores rotation.
Due to its compactness and the way it is stored in memory, certain operations (obtaining axis-angle and performing SLERP, in particular) are more efficient and robust against floating-point errors.
Tutorials¶
Properties¶
|
||
|
||
|
||
|
Constructors¶
Quaternion ( ) |
|
Quaternion ( Quaternion from ) |
|
Quaternion ( Vector3 arc_from, Vector3 arc_to ) |
|
Quaternion ( Vector3 axis, float angle ) |
|
Quaternion ( Vector3 euler_yxz ) |
|
Quaternion ( Basis from ) |
|
Quaternion ( float x, float y, float z, float w ) |
Methods¶
angle_to ( Quaternion to ) const |
|
cubic_slerp ( Quaternion b, Quaternion pre_a, Quaternion post_b, float weight ) const |
|
dot ( Quaternion with ) const |
|
get_angle ( ) const |
|
get_axis ( ) const |
|
get_euler ( ) const |
|
inverse ( ) const |
|
is_equal_approx ( Quaternion to ) const |
|
is_normalized ( ) const |
|
length ( ) const |
|
length_squared ( ) const |
|
normalized ( ) const |
|
slerp ( Quaternion to, float weight ) const |
|
slerpni ( Quaternion to, float weight ) const |
Operators¶
operator != ( ) |
|
operator != ( Quaternion right ) |
|
operator * ( Quaternion right ) |
|
operator * ( Vector3 right ) |
|
operator * ( float right ) |
|
operator * ( int right ) |
|
operator + ( Quaternion right ) |
|
operator - ( Quaternion right ) |
|
operator / ( float right ) |
|
operator / ( int right ) |
|
operator == ( ) |
|
operator == ( Quaternion right ) |
|
operator [] ( int index ) |
|
operator unary+ ( ) |
|
operator unary- ( ) |
Constants¶
IDENTITY = Quaternion(0, 0, 0, 1) --- The identity quaternion, representing no rotation. Equivalent to an identity Basis matrix. If a vector is transformed by an identity quaternion, it will not change.
Property Descriptions¶
float w
Default |
|
W component of the quaternion (real part).
Quaternion components should usually not be manipulated directly.
float x
Default |
|
X component of the quaternion (imaginary i
axis part).
Quaternion components should usually not be manipulated directly.
float y
Default |
|
Y component of the quaternion (imaginary j
axis part).
Quaternion components should usually not be manipulated directly.
float z
Default |
|
Z component of the quaternion (imaginary k
axis part).
Quaternion components should usually not be manipulated directly.
Constructor Descriptions¶
Quaternion Quaternion ( )
Constructs a default-initialized quaternion with all components set to 0
.
Quaternion Quaternion ( Quaternion from )
Constructs a Quaternion
as a copy of the given Quaternion
.
Quaternion Quaternion ( Vector3 arc_from, Vector3 arc_to )
Quaternion Quaternion ( Vector3 axis, float angle )
Constructs a quaternion that will rotate around the given axis by the specified angle. The axis must be a normalized vector.
Quaternion Quaternion ( Vector3 euler_yxz )
Quaternion Quaternion ( Basis from )
Constructs a quaternion from the given Basis.
Quaternion Quaternion ( float x, float y, float z, float w )
Constructs a quaternion defined by the given values.
Method Descriptions¶
float angle_to ( Quaternion to ) const
Returns the angle between this quaternion and to
. This is the magnitude of the angle you would need to rotate by to get from one to the other.
Note: This method has an abnormally high amount of floating-point error, so methods such as is_zero_approx
will not work reliably.
Quaternion cubic_slerp ( Quaternion b, Quaternion pre_a, Quaternion post_b, float weight ) const
Performs a cubic spherical interpolation between quaternions pre_a
, this vector, b
, and post_b
, by the given amount weight
.
float dot ( Quaternion with ) const
Returns the dot product of two quaternions.
float get_angle ( ) const
Vector3 get_axis ( ) const
Vector3 get_euler ( ) const
Returns Euler angles (in the YXZ convention: when decomposing, first Z, then X, and Y last) corresponding to the rotation represented by the unit quaternion. Returned vector contains the rotation angles in the format (X angle, Y angle, Z angle).
Quaternion inverse ( ) const
Returns the inverse of the quaternion.
bool is_equal_approx ( Quaternion to ) const
Returns true
if this quaternion and quat
are approximately equal, by running @GlobalScope.is_equal_approx on each component.
bool is_normalized ( ) const
Returns whether the quaternion is normalized or not.
float length ( ) const
Returns the length of the quaternion.
float length_squared ( ) const
Returns the length of the quaternion, squared.
Quaternion normalized ( ) const
Returns a copy of the quaternion, normalized to unit length.
Quaternion slerp ( Quaternion to, float weight ) const
Returns the result of the spherical linear interpolation between this quaternion and to
by amount weight
.
Note: Both quaternions must be normalized.
Quaternion slerpni ( Quaternion to, float weight ) const
Returns the result of the spherical linear interpolation between this quaternion and to
by amount weight
, but without checking if the rotation path is not bigger than 90 degrees.
Operator Descriptions¶
bool operator != ( )
bool operator != ( Quaternion right )
Returns true
if the quaternions are not equal.
Note: Due to floating-point precision errors, consider using is_equal_approx instead, which is more reliable.
Quaternion operator * ( Quaternion right )
Composes these two quaternions by multiplying them together. This has the effect of rotating the second quaternion (the child) by the first quaternion (the parent).
Rotates (multiplies) the Vector3 by the given Quaternion
.
Quaternion operator * ( float right )
Multiplies each component of the Quaternion
by the given value. This operation is not meaningful on its own, but it can be used as a part of a larger expression.
Quaternion operator * ( int right )
Multiplies each component of the Quaternion
by the given value. This operation is not meaningful on its own, but it can be used as a part of a larger expression.
Quaternion operator + ( Quaternion right )
Adds each component of the left Quaternion
to the right Quaternion
. This operation is not meaningful on its own, but it can be used as a part of a larger expression, such as approximating an intermediate rotation between two nearby rotations.
Quaternion operator - ( Quaternion right )
Subtracts each component of the left Quaternion
by the right Quaternion
. This operation is not meaningful on its own, but it can be used as a part of a larger expression.
Quaternion operator / ( float right )
Divides each component of the Quaternion
by the given value. This operation is not meaningful on its own, but it can be used as a part of a larger expression.
Quaternion operator / ( int right )
Divides each component of the Quaternion
by the given value. This operation is not meaningful on its own, but it can be used as a part of a larger expression.
bool operator == ( )
bool operator == ( Quaternion right )
Returns true
if the quaternions are exactly equal.
Note: Due to floating-point precision errors, consider using is_equal_approx instead, which is more reliable.
Access quaternion components using their index. q[0]
is equivalent to q.x
, q[1]
is equivalent to q.y
, q[2]
is equivalent to q.z
, and q[3]
is equivalent to q.w
.
Quaternion operator unary+ ( )
Returns the same value as if the +
was not there. Unary +
does nothing, but sometimes it can make your code more readable.
Quaternion operator unary- ( )
Returns the negative value of the Quaternion
. This is the same as writing Quaternion(-q.x, -q.y, -q.z, -q.w)
. This operation results in a quaternion that represents the same rotation.