# Movement EquationDeltaHeight = (Sin(RunningTime + DeltaTime) - Sin(RunningTime));

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 the Mathematical/Physical basis behind this equation.

An oldy, but a goody.

DelatHeight is addressing the frame by frame adjustment that we want, it is being ascribed to the NewLocation.Z so we are adjusting the vertical component of the position of the actor we are working with.

If the issue we are running into here is to do with the output of Sin, consider that sine is providing a value to an angle. If we look at a circle on a graph with 0,0 in the middle and a radius of 1 [a unit circle], then the furthest right, up, left, and down positions are (1,0), (0,1), (-1,0), and (0,-1) respectively. The angles that address these locations are 0, 90, 180, and 270 degrees. So if we plug those angles in to Sin, then we get the Y value of our unit circle. 0, 1, 0, and -1. Since we are feeding a form of time in to this, the number is always going up, so that means we are just cycling around the circle over and over again.

If we were to graph out the sin values over a constantly growing time value, then it would create a wave form, starting at zero, climbing to one gradually, descending to zero, further on to negative one, and then back to zero. . . over and over.

The uninitialized value for RunningTime could be anything, and so we don’t want to get that value back from Sin by itself. So, we look at Sin(RunningTime + DeltaTime) [The time since the last frame was drawn] and subtract Sin(RunningTime). The guarantees that no matter what value was stored in RunningTime to begin with, we can make incremental adjustments.

That is the creation of the sin wave, and we are taking a DeltaTime adjustment to that and subtracting just the regular sin wave back out. That means we are only getting that difference value, which is always just ahead of where the RunningTime Sin wave would be. This helps us step forward frame over frame.

This is all scaled by 20.0f, making the movement go beyond one to negative one and scale up to 20 through -20 [a range of 40].