I have started working in Unreal Engine 4 and opened up the most basic tutorial with c++ on their web site. So this is the code they provided
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);
}
And this is the explanation of what it should do: The code we've just written will cause FloatingActors to bob up and down smoothly, using the RunningTime variable we created to keep track of our movement over time.
And when you compile it it does that, but it doesn't tell me anything about how or why it works. The thing that bugs me is as the title say the movement equation :
DeltaHeight = sin(RunningTime + DeltaTime) - sin(RunningTime)
If anyone could explain this to me it would be greatly appreciated. What I'm asking would be Mathematical/Physical explanation behind this equation or the explanation where does this equation come from. Why is it like that.
This function controls the movement (height specifically) of the character such that the amount of movement is controlled via a sine wave over time. That is, at time 0 it starts moving upwards relatively quickly, then slows down upwards movement until it stops moving up and then starts moving down, speeds up downwards eventually slowing down and moving up again. It continues periodically moving up and down in this manner.
So, how this functions works, is that it's called periodically and then time change between this call and the last is DeltaTime. The last time it was called was RunningTime. So compute the new position:
FMath::Sin(RunningTime + DeltaTime)
and the old position:
FMath::Sin(RunningTime)
then calculate the change in position by taking the difference:
float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));