임의의 축을 기준으로 회전(쿼터니안)

쿼터니안 회전

언리얼 엔진에서 FQuat이라는 쿼터니안 구조체 제공한다. 이 구조체를 이용하여 임의의 축에 대한 회전을 만들 수 있다.

FQuat(RotationAxis, UKismetMathLibrary::GetPI() / 4);

RotationAxis는 FVector 변수이고 이 회전축에 대해 pi/4, (double이기 때문에)약 45도정도 회전하는 쿼터니안을 생성하는 생성자다. 여기서 나처럼 회전 Rotator를 구해 지속적으로 회전을 구현할 경우, 위와 같이 각을 작게 설정해 주는게 사용하기 편하다.

최종적으로 구현한 결과물 임의의 회전축에 대해 Tick함수를 통해 계속해서 회전하는 것 이다.

void ASummon::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	// 월드를 기준으로 회전
	AddActorWorldRotation(TickRotator * DeltaTime * RotationSpeed);
}

void ASummon::BeginPlay()
{
	Super::BeginPlay();
	// 액터를 최초로 임의로 회전
	FRotator InitRotator;
	InitRotator.Yaw = FMath::RandRange(0, 360);
	InitRotator.Pitch = FMath::RandRange(0, 360);
	InitRotator.Roll = FMath::RandRange(0, 360);
	StaticMeshComponent->SetRelativeRotation(InitRotator);

	// 회전한 액터를 임의의 축을 기준으로 회전
	FVector RotationAxis;
	RotationAxis.X = FMath::FRand();
	RotationAxis.Y = FMath::FRand();
	RotationAxis.Z = FMath::FRand();
	RotationSpeed = FMath::RandRange(MinRotateSpeed, MaxRotateSpeed);
	FQuat RotationQuat = FQuat(RotationAxis, UKismetMathLibrary::GetPI() / 4);
	TickRotator = RotationQuat.Rotator();
}