diff options
author | rachytski <siarhei.rachytski@gmail.com> | 2012-09-26 20:34:47 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:44:16 +0300 |
commit | eefeba649d48a433bc291e07e771b56a66603500 (patch) | |
tree | 9b5db7026a39d69acab2e40a0cc094925ecce1c2 /anim | |
parent | ff3bc1517475102ccac59dc2364fa2a292a906e6 (diff) |
added anim::AngleInterpolation task for animation of angle rotations.
Diffstat (limited to 'anim')
-rw-r--r-- | anim/angle_interpolation.cpp | 64 | ||||
-rw-r--r-- | anim/angle_interpolation.hpp | 39 | ||||
-rw-r--r-- | anim/anim.pro | 9 |
3 files changed, 107 insertions, 5 deletions
diff --git a/anim/angle_interpolation.cpp b/anim/angle_interpolation.cpp new file mode 100644 index 0000000000..b31a55fe82 --- /dev/null +++ b/anim/angle_interpolation.cpp @@ -0,0 +1,64 @@ +#include "angle_interpolation.hpp" + +#include "../geometry/angles.hpp" + +namespace anim +{ + AngleInterpolation::AngleInterpolation(double start, + double end, + double speed, + double & out) + : m_startAngle(start), + m_outAngle(out) + { + m_speed = speed; + m_startTime = 0; + m_dist = ang::GetShortestDistance(start, end); + m_curAngle = m_startAngle; + m_endAngle = m_startAngle + m_dist; + m_interval = fabs(m_dist) / (2 * math::pi) * m_speed; + } + + void AngleInterpolation::OnStart(double ts) + { + m_startTime = ts; + Task::OnStart(ts); + } + + void AngleInterpolation::OnStep(double ts) + { + if (ts - m_startTime > m_interval) + { + End(); + return; + } + + if (!IsRunning()) + return; + + double elapsedSec = ts - m_startTime; + m_curAngle = m_outAngle = m_startAngle + m_dist * elapsedSec / m_interval; + + Task::OnStep(ts); + } + + void AngleInterpolation::OnEnd(double ts) + { + // ensuring that the final value was reached + m_outAngle = m_endAngle; + Task::OnEnd(ts); + } + + double AngleInterpolation::EndAngle() const + { + return m_endAngle; + } + + void AngleInterpolation::SetEndAngle(double val) + { + m_startAngle = m_curAngle; + m_dist = ang::GetShortestDistance(m_startAngle, val); + m_endAngle = m_startAngle + m_dist; + m_interval = fabs(m_dist) / (2 * math::pi) * m_speed; + } +} diff --git a/anim/angle_interpolation.hpp b/anim/angle_interpolation.hpp new file mode 100644 index 0000000000..55a2102557 --- /dev/null +++ b/anim/angle_interpolation.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include "task.hpp" +#include "../std/function.hpp" + +namespace anim +{ + class AngleInterpolation : public Task + { + public: + + typedef function<void()> TCallback; + + private: + + double m_startAngle; + double m_curAngle; + double & m_outAngle; + double m_startTime; + double m_endAngle; + double m_interval; + double m_dist; + double m_speed; + + public: + + AngleInterpolation(double start, + double end, + double speed, + double & out); + + void OnStart(double ts); + void OnStep(double ts); + void OnEnd(double ts); + + double EndAngle() const; + void SetEndAngle(double val); + }; +} diff --git a/anim/anim.pro b/anim/anim.pro index a7954cbf5a..477a63413a 100644 --- a/anim/anim.pro +++ b/anim/anim.pro @@ -11,11 +11,10 @@ include($$ROOT_DIR/common.pri) HEADERS += \ controller.hpp \ - task.hpp + task.hpp \ + angle_interpolation.hpp \ SOURCES += \ controller.cpp \ - task.cpp - - - + task.cpp \ + angle_interpolation.cpp \ |