How to SetControlRotation using LerpRotator in C++?

An Example in Blueprint.

A Simple lerp looks like this:
FRotator NewRotation = FMath::Lerp(AltReleasedRotation, AltPressedRotation, Percentage, true); //Lerp

But how to do a Lerp Rotator?

3 Likes

If I remember it right, if you lerp between FQuats, it will automatically do shortest path.

FQuat NewRotation = FMath::Lerp(FQuat(AltReleasedRotation), FQuat(AltPressedRotation), Percentage);

You can use FQuat in SetActorRotation() etc. instead of FRotator.

3 Likes

BTW, if you hover over a node, in most cases you can see where the underlying function is. In this case its KismetMathLibrary. You can open KismetMathLibrary.h and find this function Lerp (Rotator), see it’s RLerp(); Go to KismetMathLibrary.cpp, find RLerp(), and see what is does.

With most nodes, you can see how it works in the code, and write your code respectively.

2 Likes

Thank You very much for helping and replying with a detailed reply.
But I can’t set FQuat in SetControlRotation.

1 Like

I am now trying KismetMathLibrary

FRotator NewRotation = UKismetMathLibrary::RLerp(AltPressedRotation, AltReleasedRotation, Percentage, true);

but not sure why I am using a blueprint library if I am developing in C++ :smiley:

I didn’t suggest using Kismet library, I said you can see how it’s all done in there. Kismet libraries still use default engine stuff that you can use in c++.
As for control rotation, maybe there’s no converter from FQuat to FRotator, like in actors. You can always convert it manually. After all, that’s exactly what KismetMathLibrary does:

FRotator UKismetMathLibrary::RLerp(FRotator A, FRotator B, float Alpha, bool bShortestPath)
{
    // if shortest path, we use Quaternion to interpolate instead of using FRotator
    if (bShortestPath)
    {
        FQuat AQuat(A);
        FQuat BQuat(B);
        FQuat Result = FQuat::Slerp(AQuat, BQuat, Alpha);
        return Result.Rotator();
    }
    const FRotator DeltaAngle = B - A;
    return A + Alpha*DeltaAngle;
}
1 Like

Yes, Thank You very much Sir for helping me in my study progress, You just solved my problem )

FRotator NewRotation (EForceInit::ForceInit);
NewRotation = FRotator(FQuat::Slerp(FQuat(AltReleasedRotation), FQuat(AltPressedRotation), Percentage));

This works for me, Thank You very very much for helping, I really appreciate it :smiley :slight_smile:

1 Like