keywords: [UE4][Linear Algebra]Lerp calculation formula and usage example

UnrealMathUnlity.h:

/** Performs a linear interpolation between two values, Alpha ranges from 0-1 */
template< class T, class U > 
static FORCEINLINE_DEBUGGABLE T Lerp( const T& A, const T& B, const U& Alpha )
{
    return (T)(A + Alpha * (B-A));
}

/** Performs a linear interpolation between two values, Alpha ranges from 0-1. Handles full numeric range of T */
template< class T > 
static FORCEINLINE_DEBUGGABLE T LerpStable( const T& A, const T& B, double Alpha )
{
    return (T)((A * (1.0 - Alpha)) + (B * Alpha));
}

/** Performs a linear interpolation between two values, Alpha ranges from 0-1. Handles full numeric range of T */
template< class T >
static FORCEINLINE_DEBUGGABLE T LerpStable(const T& A, const T& B, float Alpha)
{
    return (T)((A * (1.0f - Alpha)) + (B * Alpha));
}

/** Performs a 2D linear interpolation between four values values, FracX, FracY ranges from 0-1 */
template< class T, class U > 
static FORCEINLINE_DEBUGGABLE T BiLerp(const T& P00,const T& P10,const T& P01,const T& P11, const U& FracX, const U& FracY)
{
    return Lerp(
        Lerp(P00,P10,FracX),
        Lerp(P01,P11,FracX),
        FracY
        );
}

/** Interpolate vector from Current to Target. Scaled by distance to Target, so it has a strong start speed and ease out. */
static CORE_API FVector VInterpTo( const FVector& Current, const FVector& Target, float DeltaTime, float InterpSpeed );

/** Interpolate rotator from Current to Target. Scaled by distance to Target, so it has a strong start speed and ease out. */
static CORE_API FRotator RInterpTo( const FRotator& Current, const FRotator& Target, float DeltaTime, float InterpSpeed);

/** Interpolate float from Current to Target. Scaled by distance to Target, so it has a strong start speed and ease out. */
static CORE_API float FInterpTo( float Current, float Target, float DeltaTime, float InterpSpeed );
Usage example: Uniform velocity mode

Define two member variables:

float LerpTime = 0.f;
const float LerpDuration = 2.5f;

Then calculate in Tick:

void AMyActor::Tick(float DeltaSecond)
{
    if(LerpTime < LerpDuration)
    {
        LerpTime += DeltaSecond;
        
        FVector NewLoc = FMath::Lerp<FVector>(SrcLoc, DistLoc, LerpTime / LerpDuration);
    
        MyCharacter->SetActorLocation(NewLoc);
    }
}
Usage example: Damping mode (Cushion mode: strong at first, then soft)

Rotating example 01 (by speed):

void AMyActor::Tick(float DeltaSeconds)
{
    FRotator DestRot_WorldSpace(100.f, 100.f, 0.f);
    FRotator RotOffset =
        UKismetMathLibrary::InverseTransformRotation(GetActorTransform(), DestRot_WorldSpace);
    if (!FMath::IsNearlyEqual(RotOffset, FRotator::ZeroRotator, 0.1f))
    {
        SetActorRotation(
            FMath::RInterpTo(GetActorRotation(), DestRot_WorldSpace, DeltaSeconds, 3.0f /*InterpSpeed*/ ));
    }
}

Rotating example 02 (by duration):

void AMyActor::Tick(float DeltaSeconds)
{
    if (LerpDelta <= LerpDuration)
    {
        LerpDelta += DeltaSeconds;
        float Alpha = LerpDelta / LerpDuration;
        Alpha = FMath::Clamp(Alpha, 0.f, 1.f);
        NewRot = FMath::Lerp<FRotator>(GetActorRotation(), DestRot_WorldSpace, Alpha);

        SetActorRotation(NewRot);
    }
}

Lerp Example:

NewLoc = FMath::Lerp<FVector>(NewLoc, DistLoc, LerpTime / LerpDuration);

VInterpTo Example:

NewLoc = FMath::VInterpTo(NewLoc, DistLoc, DeltaTime, InterpSpeed);

Difference between Lerp and VInterpTo: VInterpTo using InterpSpeed instead of LerpTime / LerpDuration.

Other types similar VInterpTo: RInterpTo for Rotator, FInterpTo for float.