Quaternions sound intimidating, but they are extremely easy to use. You may want to review the theoretical math behind them using the following videos:
However, we won't cover the math background here! In fact, you don't need to understand much about the math background quaternions to use them extremely effectively.
Have a project ready and an Actor
with an override ::Tick()
function that we can enter the C++ code into.
FQuat( FVector Axis, float AngleRad );
For example, to define a twisting rotation:
Quaternions have quaternion addition, quaternion subtraction, multiplication by a scalar, and division by a scalar defined for them, amongst other functions. They are extremely useful to rotate things at arbitrary angles, and point objects at one another.
Quaterions are a bit strange, but using them is quite simple. If v is the axis around which to rotate, and is the magnitude of the angle of rotation, then we get the following equations for the components of a quaternion:
So, for example, rotation about by an angle of will have the following quaternion components:
Three of the four components of the quaternion (x, y, and z) define the axis around which to rotate (scaled by the sine of half the angle of rotation), while the fourth component (w) has only the cosine of half the angle to rotate with.
Quaternions, being themselves vectors, can be rotated. Simply extract the (x, y, z) components of the quaternion, normalize, and then rotate that vector. Construct a new quaternion from that new unit vector with the desired angle of rotation.
Multiplying quaternions together represents a series of rotations that happen subsequently. For example, rotation of 45º about the X axis, followed by a rotation of 45º about the Y axis will be composed by the following:
FQuat( FVector( 1, 0, 0 ), PI/4.f ) * FQuat( FVector( 0, 1, 0 ), PI/4.f );
3.15.226.248