Core/Math API – Rotation using FRotator

Rotation in UE4 has such complete implementation that it can be hard to choose how to rotate your objects. There are three main methods—FRotator, FQuat, and FRotationMatrix. This recipe outlines the construction and use of the first of the three different methods for the rotation of objects—the FRotator. Using this, and the following two recipes, you can select at a glance a method to use to rotate your objects.

Getting ready

Have a UE4 project that has an object you can get a C++ interface with. For example, you can construct a C++ class Coin that derives from Actor to test out rotations with. Override the Coin::Tick() method to apply your rotations from the C++ code. Alternatively, you can call these rotation functions in the Tick event from Blueprints.

In this example, we will rotate an object at a rate of one degree per second. The actual rotation will be the accumulated time since the object was created. To get this value, we'll just call GetWorld()->TimeSeconds.

How to do it…

  1. Create a custom C++ derivative of the Actor class called Coin.
  2. In the C++ code, override the ::Tick() function of the Coin actor derivative. This will allow you to effect a change to the actor in each frame.
  3. Construct your FRotator. FRotators can be constructed using a stock pitch, yaw, and roll constructor, as shown in the following example:
    FRotator( float InPitch, float InYaw, float InRoll );
  4. Your FRotator will be constructed as follows:
    FRotator rotator( 0, GetWorld()->TimeSeconds, 0 );
  5. The standard orientation for an object in UE4 is with Forward facing down the +X axis. Right is the +Y axis, and Up is +Z.
    How to do it…
  6. Pitch is rotation about the Y axis (across), yaw is rotation about the Z axis (up), and roll is rotation about the X axis. This is best understood in the following three points:
    • Pitch: If you think of an airplane in UE4 standard coordinates, the Y axis goes along the wingspan (pitching tilts it forward and backward)
    • Yaw: The Z axis goes straight up and down (yawing turns it left and right)
    • Roll: The X axis goes straight along the fuselage of the plane (rolling does barrel rolls)

      Tip

      You should note that in other conventions, the X axis is pitch, the Y axis is yaw, and the Z axis is roll.

  7. Apply your FRotator to your actor using the SetActorRotation member function, as follows:
    FRotator rotator( 0, GetWorld()->TimeSeconds, 0 );
    SetActorRotation( rotation );
..................Content has been hidden....................

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