ある値の初期値と終了値を指定して補間してつなぐような処理をtweenというらしい。もともとはflashのライブラリ?
ゲームではアニメーションのリソースを作成するまでもないけど、ちょっとしたアニメーションをしたい場合が多いので有用な機能だと思う。
C++のtweenライブラリ
- cpptweener
http://code.google.com/p/cpptweener/ - Claw tweeners
http://libclaw.sourceforge.net/tweeners.html
ライブラリを使うほど大げさなものが必要じゃなかったので自分でも書いてみた。
- シンプルである
- イージングを指定できる(補間方法)
- フレーム数指定できる
float EaseIn(float t) { return t * t; } float EaseOut(float t) { return 1.0f - t * t; } float Linear(float t) { return t; } typedef float (*EaseMethod)(float t); template <typename T, typename U = T> class Tween { public: Tween(T &v, U end, float d, EaseMethod ease = Linear) : value(v), start(v), end(end), step(1.0f), duration(d), frame(0.0f), ease(ease) {} Tween(T &v, U start, U end, float d, EaseMethod ease = Linear) : value(v), start(start), end(end), step(1.0f), duration(d), frame(0.0f), ease(ease) {} bool Update() { frame += step; float t = ease(frame / duration); value = start * (1.0f - t) + end * t; return frame <= duration; } private: T &value; U start, end; float time; float step; float duration; float frame; EaseMethod ease; };
使うときは
float a = 10.0f; Tween<float> tweenFloat(a, 100.0f, 5.0f); while(tweenFloat.Update()) { std::cout << a << std::endl; }
28
46
64
82
100
広告
コメントを残す