I don't get how you can write something to solve this puzzle, but then not know how to maintain a simple counter to animate a value.
Let's say you have a frequency F
which is moves per second, and time T
which is the number of seconds since the animation began.
The idea, then, is that move M
begins at T*F*M
and has a duration of 1
(or less, if you want pauses between moves).
Now, you just need an interpolation function to blend the values. Linear interpolation (LERP) is easiest, but you might want a spline function to accelerate/decelerate at the end points. However you do it, the function simply takes a beginning point, an end point, and a relative position t
which is between 0 and 1.
double blend( double from, double to, double t )
{
// linear interp:
return from * (t-1.0) + to * t;
}
So, you just calculate t
by computing the fractional part fmod(T*F*M, 1.0)
. If you have a duration of less than 1
, you do not change the 1.0
in that fmod
call. You simply clamp it to your duration D
and then divide by D
.