Search code examples
c++unreal-engine4

Unreal Engine Beginner FMath::Sin


Okay, so I'm currently learning an Unreal Engine programming tutorial. Here is the code I'm confused with:

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);
}

I don't understand the part where it says this:

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

and this part:

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

What does it do? How does it do that? What is FMath::Sin? It's so confusing.

That's it! Thank you for your time (and hopefully, help)!


Solution

  • Super::Tick( DeltaTime ); is calling the Tick method of the parent class with the Super keyword.

    DeltaTime is the amount of time that is passed between in each frame in the engine, a very common and necessary concept in game development that you can learn more about here.

    Now lets look at :

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

    float DeltaHeight creates a new float variable on the stack

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

    uses the FMath::Sin Sin method of the FMath class to do a basic sin calculation. You can do an easy tutorial on sin and cosine here

    And finally lets look at

       NewLocation.Z += DeltaHeight * 20.0f;
    

    NewLocation is an FVector, which is Unreal's version of a Vector. All this line does is add the previously calculated float called DeltaHeight to the Z value of NewLocation, as well as scaling that value by 20.

    To learn more about basic vector math I would recomend The Nature of Code by Daniel Shiffman.