虚幻引擎初学者 FMath::Sin.

Unreal Engine Beginner FMath::Sin

本文关键字:Sin FMath 初学者 引擎      更新时间:2023-10-16

好的,所以我目前正在学习虚幻引擎编程教程。这是我感到困惑的代码:

void AFloatingActor::Tick( float DeltaTime )
{
    Super::Tick( DeltaTime );
    FVector NewLocation = GetActorLocation();
    float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
    NewLocation.Z += DeltaHeight * 20.0f; // Scale our height by a factor of 20
    RunningTime += DeltaTime;
    SetActorLocation(NewLocation);
}

我不明白它说这部分:

void AFloatingActor::Tick( float DeltaTime )
{
    Super::Tick( DeltaTime );

而这部分:

float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
    NewLocation.Z += DeltaHeight * 20.0f; // Scale our height by a factor of 20

它有什么作用?它是如何做到的?什么是FMath::sin?太令人困惑了。

就是这样!感谢您的时间(希望对您有所帮助)!

Super::Tick( DeltaTime );使用

Super 关键字调用父类的 Tick 方法。

DeltaTime是引擎中每一帧之间经过的时间量,这是游戏开发中一个非常常见且必要的概念,您可以在此处了解更多信息。

现在让我们看看:

float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - 
    FMath::Sin(RunningTime));
    NewLocation.Z += DeltaHeight * 20.0f; // Scale our height by a factor of 20

float DeltaHeight堆栈上创建新的float变量

= (FMath::Sin(RunningTime + DeltaTime) - 
    FMath::Sin(RunningTime));

使用 FMath 类的 FMath::Sin Sin 方法进行基本的 SIN 计算。你可以在这里做一个关于正弦和余弦的简单教程

最后让我们看看

   NewLocation.Z += DeltaHeight * 20.0f;

NewLocation是一个FVector,是虚幻引擎版本的矢量。这条线所做的只是将先前计算的称为 DeltaHeightfloat添加到 NewLocationZ值,并将该值缩放 20。

要了解有关基本向量数学的更多信息,我会推荐Daniel Shiffman的The Nature of Code。

FMath在UE4 API中用于许多核心数学函数。FMath::Abs 给你绝对值(例如 psotive 值)

FMath::Sin 表示您使用来自 FMath 类的 sin 函数。 :: 表示继承自或"来自",因此可以将其视为"FMath 有一个函数 im 调用称为 Sin"

Super::Tick(DeltaTime); 只是意味着你的 actor 类在 tick 函数中滴答(执行每一帧)。