Game Development Reference
In-Depth Information
Finally, the animation must be updated, this is the main function in the system, it is where
the ease curves are used and the interpolation takes place. It is also where we determine if
theanimationhasfinished,andifithas,weinvokethefinishfunctionjustbeforetheanim-
ation is removed from the interpolator.
void animation::Update(float deltaTime)
{
const float dt = math::Min(m_duration - m_time, deltaTime);
m_time += dt;
const float t = m_easeFunction( m_time / m_duration );
const float result = math::Lerp(m_start, m_end, t);
m_updateFunction(result);
if ( m_time >= m_duration )
{
m_finishFunction();
}
}
The parameter dt is the time remaining to advance the animation, or if the frame's time is
larger than what is needed to complete the interpolation, we take the lesser amount.
Callingtheeasefunctionrequiresaparameterfrom0.0to1.0,forthiswedividethepresent
animation'stimebyitsintendedduration,andwethendoalinearinterpolation betweenthe
starting value and the ending value, with the t from the curve function.
We call the update function to make sure our calculated values are used, whether to update
the reference that the user provided, or to perform a custom update function.
Finally if the time is greater than the duration, if a finish function was provided we use it.
Using the system requires creating an interpolator object and adding animations to it.
ui::animation anim(m_currentAngle, targetAngle, 5.f, ui::curves::ease_in_out<ui::curves::quadratic>,
[&]{ m_animating=false; } );
m_interpolator.Add(anim);
Inthisexample ananimation iscreated whichwill interpolate from m_currentAngle to tar-
getAngle overthecourseoffiveseconds.Itwilluseaneasein/outquadraticcurveandonce
the animation is finished, it will reset a flag that indicates that the object was animating.
It is also possible to chain animations together, the finish function of one animation could
add a new animation into the interpolator. This can be used to achieve varied effects.
Search WWH ::




Custom Search