FMath::Lerp esegue un'interpolazione lineare tra due valori, l'Alpha varia da 0 a 1.
In Unreal Engine, FMath::Lerp รจ una funzione che consente di eseguire una "linear interpolation" (interpolazione lineare) tra due valori. L'interpolazione lineare รจ un metodo che calcola un valore intermedio tra due estremi basandosi su un fattore di interpolazione compreso tra 0 e 1.
La firma della funzione FMath::Lerp รจ la seguente:
template <typename T>
static FORCEINLINE T Lerp(const T& A, const T& B, float Alpha);
Dove:
Aรจ il valore di partenzaBรจ il valore di destinazioneAlphaรจ il fattore di interpolazione che determina la quantitร di "mescolamento" tra i due valori. Un valore di 0 restituisce il valore di partenzaA, un valore di 1 restituisce il valore di destinazioneB, mentre un valore compreso tra 0 e 1 restituisce un valore intermedio traAeBin base al fattore di interpolazione.
Ad esempio, se si desidera eseguire l'interpolazione lineare tra due valori float StartValue e TargetValue con un fattore di interpolazione di 0.5, si puรฒ utilizzare la funzione FMath::Lerp nel seguente modo:
float Result = FMath::Lerp(StartValue, TargetValue, 0.5f);
Il risultato sarร un valore che rappresenta l'interpolazione lineare al 50% tra StartValue e TargetValue.
| Module | Core |
| Header | /Engine/Source/Runtime/Core/Public/Math/UnrealMathUtility.h |
| Include | #include "Math/UnrealMathUtility.h" |
Esempio
Script di esempio per la rotazione.
MioScript.h:
private: float TargetYaw = 10.f; // 360.f float InitialYaw; // 270.f float CurrentYaw;
MioScript.cpp:
// Called when the game starts
void UMioScript::BeginPlay()
{
Super::BeginPlay();
// HERE BEGIN CODE
InitialYaw = GetOwner()->GetActorRotation().Yaw;
CurrentYaw = InitialYaw;
TargetYaw = InitialYaw + 90.f;
}
// Called every frame
void UMioScript::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// HERE BEGIN CODE
CurrentYaw = FMath::Lerp(CurrentYaw, TargetYaw, 0.02f);
FRotator DoorRotation = GetOwner()->GetActorRotation();
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation);
}







0 Commenti