Search code examples
c++unreal-engine4

Unity's 'Vector3.Slerp' equivalent in Unreal Engine 4 C++?


Does UE4 C++ has a built-in function similar to Vector3.Slerp?

If not is there an 'Unreal optimized way' to do that?
I got 2 vectors that I want to get some locations between them on a circular arc path.


Solution

  • I would implement the vector slerp equation (see wikipedia page) like so:

    static FVector Slerp(const FVector& a, const FVector& b, const float t)
    {
        float omega = FGenericPlatformMath::Acos(FVector::DotProduct(
                a.GetSafeNormal(), 
                b.GetSafeNormal()
                ));
        float sinOmega = FGenericPlatformMath::Sin(omega);
        FVector termOne = a * (FGenericPlatformMath::Sin(omega * (1.0 - t)) / sinOmega);
        FVector termTwo = b * (FGenericPlatformMath::Sin(omega * (      t)) / sinOmega);
        return termOne + termTwo;
    }