Core/Math API – Rotation using FQuat

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.

Getting ready

Have a project ready and an Actor with an override ::Tick() function that we can enter the C++ code into.

How to do it…

  1. To construct a quaternion, the best constructor to use is as follows:
    FQuat( FVector Axis, float AngleRad );

    Note

    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.

How it works…

Quaterions are a bit strange, but using them is quite simple. If v is the axis around which to rotate, and How it works… is the magnitude of the angle of rotation, then we get the following equations for the components of a quaternion:

How it works…

So, for example, rotation about How it works… by an angle of How it works… will have the following quaternion components:

How it works…

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.

There's more…

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 );
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.226.248