Questions about how InterpTo() works

if (!RecoilValue.IsZero())
	{
		RecoilValue = FMath::Vector2DInterpTo(RecoilValue, FVector2D(0.f), DeltaTime, 12.f);
		Player->AddControllerYawInput(RecoilValue.X);
		Player->AddControllerPitchInput(-RecoilValue.Y);
	}

This is my gun recoil code, and it’s in Tick().
It works well as I want, but I have a question while using InterpTo().
If the variable interpolated by InterpTo() is interpolated again, I think it only gets infinitely close to zero(Target) and cannot be zero(Target).
However, when I printed out the log, I could see that it became smaller and then zero.

So I looked inside the interpTo() function, it was as follows.

CORE_API FVector2D FMath::Vector2DInterpTo( const FVector2D& Current, const FVector2D& Target, float DeltaTime, float InterpSpeed )
{
	if( InterpSpeed <= 0.f )
	{
		return Target;
	}

	const FVector2D Dist = Target - Current;
	if( Dist.SizeSquared() < UE_KINDA_SMALL_NUMBER )
	{
		return Target;
	}

	const FVector2D DeltaMove = Dist * FMath::Clamp<float>(DeltaTime * InterpSpeed, 0.f, 1.f);
	return Current + DeltaMove;
}

I don’t really understand what SizeSquared() is about, but if this starting point and the target point are very close, it seems to just return the target point.

Is my conclusion correct? I am confused because I am not good at math.

It’s awkward to talk because I turned on the translator. Sorry.

Hi,

Yes, UE_KINDA_SMALL_NUMBER is set as follows in UnrealMathUtility.h:

#define UE_KINDA_SMALL_NUMBER (1.e-4f)

So if the squared length of the distance between the target and the current is less than this value, it will just return Target.

I hope this answers the question :slight_smile:

1 Like
FORCEINLINE T TVector2<T>::SizeSquared() const
{
	return X*X + Y*Y;
}

I’d like to add that with float values you should normally not use operators like == due to float precision. You can get a different result from == by ‘chance’. You could use FMath::IsNearlyZero or NearlyEqual, not a test to exact 0.

1 Like