\

The original tutorial is based on UE 4.18, I am based on UE 4.25.

English original Address

Follow this tutorial, in which you will learn how to use the RotateAngleAxis function. Start by creating a new C++ Actor class and calling it RotateAngleAxis. In the header file, we will create two floating point variables and two FVector variables and set them to EditAnywhere so that we can edit them later in the editor.

Here is the final header code.

RotateAngleAxis.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "RotatingAngleAxis.generated.h"

UCLASS(a)class UNREALCPP_API ARotatingAngleAxis : public AActor
{
	GENERATED_BODY(a)public:	
	// Sets default values for this actor's properties
	ARotatingAngleAxis(a);protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay(a) override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// declare our float variables
	UPROPERTY(EditAnywhere, Category = Movement)
	float AngleAxis;

	UPROPERTY(EditAnywhere, Category = Movement)
	FVector Dimensions;

	UPROPERTY(EditAnywhere, Category = Movement)
	FVector AxisVector;

	UPROPERTY(EditAnywhere, Category = Movement)
	float Multiplier;
};
Copy the code

In.cpp, let’s first set some defaults for actor.

// Sets default values
ARotatingAngleAxis::ARotatingAngleAxis()
{
 	// 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;

	Dimensions = FVector (300.0.0); ///< radius of rotation
	AxisVector = FVector (0.0.1);
	Multiplier = 50.f;

}
Copy the code

All of our logic is in the Tick function. In this example, we will be using a fixed vector point, so every frame we want to initialize our fixed position by setting NewLocation to a fixed FVector.

Next, we want to add to the AngleAxis variable, which will indicate the Angle the actor should rotate. We added DeltaTime times our Multiplier to smooth the movement. If AngleAxis is greater than or equal to 360, we reset AngleAxis to 0.

Next, we will get the RotateValue from Dimensions (radius of rotation) by using the RotateAngleAxis function. This returns the number of units needed to move the actor to the next location. The X, Y, and Z values of RotateValue are added to the NewLocation variable accordingly. We will print the RotateValue and AngleAxis variables to the screen to view these values in real time.

Finally, set the role’s location to NewLocation with SetActorLocation.

FVector RotateAngleAxis
(
    const float AngleDeg, ///< rotation Angle
    const FVector & Axis  ///< axis of rotation around
) con
Copy the code

Definition of Tick function

// Called every frame
void ARotatingAngleAxis::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	FVector NewLocation = FVector (0.0.800);

	AngleAxis += DeltaTime * Multiplier;

	if(AngleAxis >= 360.0 f) 
	{
		AngleAxis = 0;
	}

	FVector RotateValue = Dimensions.RotateAngleAxis(AngleAxis, AxisVector);

	GEngine->AddOnScreenDebugMessage(- 1.5.f, FColor::Red, FString::Printf(TEXT("RotateValue: %s"), *RotateValue.ToString()));	
	GEngine->AddOnScreenDebugMessage(- 1.5.f, FColor::Green, FString::Printf(TEXT("AngleAxis: %f"), AngleAxis));

	NewLocation.X += RotateValue.X;
	NewLocation.Y += RotateValue.Y;
	NewLocation.Z += RotateValue.Z;

	SetActorLocation(NewLocation, false.0, ETeleportType::None);
	
}
Copy the code

The complete CPP code is shown below

#include "RotatingAngleAxis.h"

// Sets default values
ARotatingAngleAxis::ARotatingAngleAxis()
{
 	// 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;

	Dimensions = FVector (300.0.0);
	AxisVector = FVector (0.0.1);
	Multiplier = 50.f;

}

// Called when the game starts or when spawned
void ARotatingAngleAxis::BeginPlay(a)
{
	Super::BeginPlay(a); }// Called every frame
void ARotatingAngleAxis::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	FVector NewLocation = FVector (0.0.800);

	AngleAxis += DeltaTime * Multiplier;

	if(AngleAxis >= 360.0 f) 
	{
		AngleAxis = 0;
	}


	FVector myCharacter = GetWorld() - >GetFirstPlayerController() - >GetPawn() - >GetActorLocation(a); FVector RotateValue = Dimensions.RotateAngleAxis(AngleAxis, AxisVector);

	GEngine->AddOnScreenDebugMessage(- 1.5.f, FColor::Red, FString::Printf(TEXT("RotateValue: %s"), *RotateValue.ToString()));	
	GEngine->AddOnScreenDebugMessage(- 1.5.f, FColor::Green, FString::Printf(TEXT("AngleAxis: %f"), AngleAxis));

	NewLocation.X += RotateValue.X;
	NewLocation.Y += RotateValue.Y;
	NewLocation.Z += RotateValue.Z;

	SetActorLocation(NewLocation, false.0, ETeleportType::None);
	
}
Copy the code

Compile the code. Drag and drop the new actor into the game world. Add a static grid component (such as a cube) to the actor. When you press Play, the actor finds and rotates around the given vector point.

Run as follows

\