Remembering the past to build the future

" Remembering the past to build the future "

Friday 1 January 2016

Unreal Engine 4 : Actors and Transformations

In this post I want to share a little tutorial  about transformations using Unreal Engine. Unreal Engine is a big, great and professional engine that allows you to make amazing 3D and 2D games for every platform. The best way to start with this engine is to follow the first tutorial :


Fig 1. Level Designer Quick Start

After this tutorial you should have the basic concepts in your mind about levels, objects, materials, lighting and static transformations. The next step is making your own level and using C++ for funny dynamic behaviors like object rotations. My personal final result is:


Video 1. Rotation demo

As you can see the cube and the pyramid rotates about your Z axis. The pretty ambient is realized using free Unreal starter content as in the level designer quick start tutorial. The cube is located in X=0, Y=0 and Z=0 coordinates, while the pyramid is in a different other position. You can get the cube from the Modes panel inside the Basic tab:

Fig 2. Cube object
While you can find the pyramid in the Content Browser:


Fig 3. Pyramid



It's important to set, for cube and pyramid object, the mobility property to Movable value in the Details Panel. You can apply the material you like to this object. Now from the File menu, we have to choose New C++ class (Note you need a Visual Studio 2013 Express Edition installation, in this tutorial I have Unreal 4.8 as reference). You have to follow the quick wizard and choose AActor as base class. Unreal will launch Visual Studio with the initial C++ code. Now suppose the class name choosed during the wizard is RotatorActor, well we have to edit RotatorActor.h adding the RotationDemo method :


#pragma once



#include "GameFramework/Actor.h"

#include "RotatorActor.generated.h"



UCLASS()
class MYPROJECT3_API ARotatorActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ARotatorActor();

// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;

private:
// Rotation demo
virtual void RotationDemo(float DeltaSeconds);
};



and implement the method in the RotatorActor.cpp class:



#include "MyProject3.h"
#include "RotatorActor.h"


// Sets default values
ARotatorActor::ARotatorActor()
{
  // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void ARotatorActor::BeginPlay()
{
Super::BeginPlay();
}

// Called every frame
void ARotatorActor::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
RotationDemo(DeltaTime);
}

void ARotatorActor::RotationDemo(float DeltaTime){
AActor* pyramid = RootComponent->GetChildComponent(1)->GetOwner();

FRotator fRotator  = pyramid->GetActorRotation();
FVector  fLocation = pyramid->GetActorLocation();
FVector  fLocationOrigin(0, 0, 0);

fRotator.Yaw += 20 * DeltaTime;

pyramid->SetActorLocation(fLocationOrigin);
pyramid->SetActorRotation(fRotator);
pyramid->SetActorLocation(fLocation);

AActor* cube = RootComponent->GetChildComponent(0)->GetOwner();
cube->SetActorRotation(fRotator);
}


The implementation retrieves the cube and pyramid Actor reference and apply trasformations to this objects. In particular the cube is located in (0,0,0)  coordinate, so we can apply the rotation trasfomation around Z axis without any other trasfomation. The pyramid is not located to the origin, so to get a rotation about its own Z axis we need a trasfomation compositions (affine trasformation):

Traslation to the origin + Rotation around the Z axis + Traslation to the original position

The code is completed, now we need to realize the hierarchy for RotatorActor. We want that the cube and pyramid are children of RotatorActor. The first step is to add the RotatorActor class to the level, add the Scene component to RotatorActor and than moving the cube and pyramid on RotatorActor in the tab World Outliner :



If you like this post, or you want the project code you can write me. Thanks a lot for reading.

My Android Games, much are free