[3851] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
[3849] | 3 | |
---|
[3851] | 4 | Copyright (C) 2004 orx |
---|
[3849] | 5 | |
---|
[3851] | 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
[3849] | 10 | |
---|
[3851] | 11 | ### File Specific: |
---|
| 12 | main-programmer: Benjamin Grauer |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | /*! |
---|
| 17 | \file t_animation.h |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | #ifndef _T_ANIMATION_H |
---|
| 21 | #define _T_ANIMATION_H |
---|
| 22 | |
---|
[3849] | 23 | #include "animation.h" |
---|
| 24 | |
---|
| 25 | //! A Class to handle some animation for single floated values. |
---|
| 26 | template<class T> class tAnimation : public Animation |
---|
| 27 | { |
---|
| 28 | public: |
---|
| 29 | tAnimation(T* object = NULL, void (T::*funcToAnim)(float) = NULL); |
---|
| 30 | virtual ~tAnimation(); |
---|
| 31 | |
---|
| 32 | virtual void rewind(); |
---|
| 33 | |
---|
| 34 | void setFuncToAnim(T* object, void (T::*funcToAnim)(float)); |
---|
| 35 | void addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc = ANIM_LINEAR); |
---|
| 36 | |
---|
[3852] | 37 | virtual void tick(float dt); |
---|
[3849] | 38 | |
---|
| 39 | // animation functions |
---|
| 40 | void setAnimFunc(ANIM_FUNCTION animFunc); |
---|
| 41 | |
---|
[3851] | 42 | private: |
---|
| 43 | |
---|
[3849] | 44 | float constant(float timePassed) const; |
---|
| 45 | float linear(float timePassed) const; |
---|
| 46 | float sine(float timePassed) const; |
---|
| 47 | float cosine(float timePassed) const; |
---|
| 48 | float exp(float timePassed) const; |
---|
| 49 | float negExp(float timePassed) const; |
---|
| 50 | float quadratic(float timePassed) const; |
---|
| 51 | float random(float timePassed) const; |
---|
[3851] | 52 | |
---|
| 53 | |
---|
[3849] | 54 | // ANIM_FUNCTION animFunc; |
---|
[3854] | 55 | float (tAnimation<T>::*animFunc)(float) const; //!< A Function for the AnimationType |
---|
[3855] | 56 | |
---|
[3854] | 57 | KeyFrameF* currentKeyFrame; //!< The current KeyFrame |
---|
| 58 | KeyFrameF* nextKeyFrame; //!< The KeyFrame we iterate to |
---|
| 59 | tList<KeyFrameF>* keyFrameList; //!< The KeyFrameList |
---|
[3849] | 60 | |
---|
[3854] | 61 | T* object; //!< The Object from which to Animate something |
---|
| 62 | void (T::*funcToAnim)(float); //!< The function to Animate |
---|
| 63 | |
---|
| 64 | float expFactor; //!< some factors |
---|
| 65 | |
---|
[3849] | 66 | }; |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | \brief standard constructor |
---|
| 72 | */ |
---|
| 73 | template<class T> |
---|
| 74 | tAnimation<T>::tAnimation (T* object, void (T::*funcToAnim)(float)) |
---|
| 75 | { |
---|
| 76 | // create a new List |
---|
| 77 | this->keyFrameList = new tList<KeyFrameF>(); |
---|
| 78 | KeyFrameF* tmpKeyFrame = new KeyFrameF; |
---|
| 79 | tmpKeyFrame->value = 0.0; |
---|
| 80 | tmpKeyFrame->duration = 1.0; |
---|
| 81 | keyFrameList->add(tmpKeyFrame); |
---|
| 82 | |
---|
| 83 | this->currentKeyFrame = tmpKeyFrame; |
---|
| 84 | this->nextKeyFrame = tmpKeyFrame; |
---|
| 85 | |
---|
| 86 | this->animFunc = &tAnimation<T>::linear; |
---|
| 87 | |
---|
| 88 | this->setFuncToAnim(object, funcToAnim); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | \brief standard deconstructor |
---|
[3854] | 94 | |
---|
| 95 | deletes all the Keyframes |
---|
[3849] | 96 | */ |
---|
| 97 | template<class T> |
---|
| 98 | tAnimation<T>::~tAnimation () |
---|
| 99 | { |
---|
| 100 | // delete all the KeyFrames |
---|
| 101 | tIterator<KeyFrameF>* itKF = keyFrameList->getIterator(); |
---|
| 102 | KeyFrameF* enumKF = itKF->nextElement(); |
---|
| 103 | while (enumKF) |
---|
| 104 | { |
---|
| 105 | delete enumKF; |
---|
| 106 | enumKF = itKF->nextElement(); |
---|
| 107 | } |
---|
| 108 | delete itKF; |
---|
| 109 | delete this->keyFrameList; |
---|
| 110 | } |
---|
| 111 | |
---|
[3854] | 112 | /** |
---|
| 113 | \brief rewinds the Animation to the beginning (first KeyFrame and time == 0) |
---|
| 114 | */ |
---|
[3849] | 115 | template<class T> |
---|
| 116 | void tAnimation<T>::rewind(void) |
---|
| 117 | { |
---|
| 118 | this->currentKeyFrame = keyFrameList->firstElement(); |
---|
| 119 | this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement()); |
---|
| 120 | this->localTime = 0.0; |
---|
| 121 | } |
---|
| 122 | |
---|
[3854] | 123 | /** |
---|
| 124 | \brief sets the Function we want to animate |
---|
| 125 | \param object from what object do we want to animate |
---|
| 126 | \param funcToAnim which function |
---|
| 127 | */ |
---|
[3849] | 128 | template<class T> |
---|
| 129 | void tAnimation<T>::setFuncToAnim(T* object, void (T::*funcToAnim)(float)) |
---|
| 130 | { |
---|
| 131 | this->baseObject = this->object = object; |
---|
| 132 | this->funcToAnim = funcToAnim; |
---|
| 133 | } |
---|
| 134 | |
---|
[3854] | 135 | /** |
---|
| 136 | \brief Appends a new Keyframe |
---|
| 137 | \param value the value of the new KeyFrame |
---|
| 138 | \param duration The duration from the new KeyFrame to the next one |
---|
| 139 | \param animFunc The function to animate between this keyFrame and the next one |
---|
| 140 | */ |
---|
[3849] | 141 | template<class T> |
---|
| 142 | void tAnimation<T>::addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc) |
---|
| 143 | { |
---|
| 144 | // some small check |
---|
| 145 | if (duration <= 0.0) |
---|
| 146 | duration = 1.0; |
---|
| 147 | |
---|
| 148 | KeyFrameF* tmpKeyFrame; |
---|
| 149 | |
---|
| 150 | if (bHasKeys) |
---|
| 151 | { |
---|
| 152 | tmpKeyFrame = new KeyFrameF; |
---|
| 153 | if (this->currentKeyFrame == this->nextKeyFrame) |
---|
| 154 | this->nextKeyFrame = tmpKeyFrame; |
---|
| 155 | this->keyFrameList->add(tmpKeyFrame); |
---|
| 156 | |
---|
| 157 | } |
---|
| 158 | else |
---|
| 159 | { |
---|
| 160 | tmpKeyFrame = this->keyFrameList->firstElement(); |
---|
| 161 | bHasKeys = true; |
---|
| 162 | this->setAnimFunc(animFunc); |
---|
| 163 | } |
---|
| 164 | tmpKeyFrame->value = value; |
---|
| 165 | tmpKeyFrame->duration = duration; |
---|
| 166 | tmpKeyFrame->animFunc = animFunc; |
---|
| 167 | } |
---|
| 168 | |
---|
[3854] | 169 | /** |
---|
| 170 | \brief ticks the Animation |
---|
| 171 | \param dt how much time to tick |
---|
| 172 | */ |
---|
[3849] | 173 | template<class T> |
---|
[3852] | 174 | void tAnimation<T>::tick(float dt) |
---|
[3849] | 175 | { |
---|
| 176 | if (this->bRunning) |
---|
| 177 | { |
---|
[3852] | 178 | this->localTime += dt; |
---|
[3849] | 179 | if (localTime >= this->currentKeyFrame->duration) |
---|
| 180 | { |
---|
| 181 | // switching to the next Key-Frame |
---|
[3851] | 182 | this->localTime -= this->currentKeyFrame->duration; |
---|
| 183 | |
---|
[3849] | 184 | this->currentKeyFrame = this->nextKeyFrame; |
---|
| 185 | // checking, if we should still Play the animation |
---|
| 186 | if (this->currentKeyFrame == this->keyFrameList->lastElement()) |
---|
| 187 | { |
---|
| 188 | switch (this->postInfinity) |
---|
| 189 | { |
---|
| 190 | case ANIM_INF_CONSTANT: |
---|
[3853] | 191 | this->localTime = 0.0; |
---|
[3849] | 192 | this->bRunning = false; |
---|
| 193 | break; |
---|
| 194 | case ANIM_INF_REWIND: |
---|
| 195 | break; |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame); |
---|
| 199 | printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value); |
---|
| 200 | this->setAnimFunc(this->currentKeyFrame->animFunc); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | (this->object->*(funcToAnim))((this->*animFunc)(this->localTime)); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
[3854] | 207 | /** |
---|
| 208 | \brief Sets The kind of Animation between this keyframe and the next one |
---|
| 209 | \param animFunc The Type of Animation to set |
---|
| 210 | */ |
---|
[3849] | 211 | template<class T> |
---|
| 212 | void tAnimation<T>::setAnimFunc(ANIM_FUNCTION animFunc) |
---|
| 213 | { |
---|
| 214 | switch (animFunc) |
---|
| 215 | { |
---|
| 216 | default: |
---|
| 217 | case ANIM_CONSTANT: |
---|
| 218 | this->animFunc = &tAnimation<T>::constant; |
---|
| 219 | break; |
---|
| 220 | case ANIM_LINEAR: |
---|
| 221 | this->animFunc = &tAnimation<T>::linear; |
---|
| 222 | break; |
---|
| 223 | case ANIM_SINE: |
---|
| 224 | this->animFunc = &tAnimation<T>::sine; |
---|
| 225 | break; |
---|
| 226 | case ANIM_COSINE: |
---|
| 227 | this->animFunc = &tAnimation<T>::cosine; |
---|
| 228 | break; |
---|
| 229 | case ANIM_EXP: |
---|
| 230 | this->animFunc = &tAnimation<T>::exp; |
---|
| 231 | break; |
---|
| 232 | case ANIM_NEG_EXP: |
---|
| 233 | { |
---|
| 234 | this->animFunc = &tAnimation<T>::negExp; |
---|
| 235 | float d = fabs(this->currentKeyFrame->value - this->nextKeyFrame->value); |
---|
| 236 | expFactor = - 1.0 / this->currentKeyFrame->duration * logf(DELTA_X); |
---|
| 237 | break; |
---|
| 238 | } |
---|
| 239 | case ANIM_QUADRATIC: |
---|
| 240 | this->animFunc = &tAnimation<T>::quadratic; |
---|
| 241 | break; |
---|
| 242 | case ANIM_RANDOM: |
---|
| 243 | this->animFunc = &tAnimation<T>::random; |
---|
| 244 | break; |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | // animation functions |
---|
[3854] | 250 | /** |
---|
| 251 | \brief stays at the value of the currentKeyFrame |
---|
| 252 | \param timePassed The time passed since this Keyframe began |
---|
| 253 | */ |
---|
[3849] | 254 | template<class T> |
---|
| 255 | float tAnimation<T>::constant(float timePassed) const |
---|
| 256 | { |
---|
| 257 | return this->currentKeyFrame->value; |
---|
| 258 | } |
---|
| 259 | |
---|
[3854] | 260 | /** |
---|
| 261 | \brief linear interpolation between this keyframe and the next one |
---|
| 262 | \param timePassed The time passed since this Keyframe began |
---|
| 263 | */ |
---|
[3849] | 264 | template<class T> |
---|
| 265 | float tAnimation<T>::linear(float timePassed) const |
---|
| 266 | { |
---|
| 267 | return this->currentKeyFrame->value + (this->nextKeyFrame->value - this->currentKeyFrame->value) |
---|
| 268 | * (timePassed / this->currentKeyFrame->duration); |
---|
| 269 | // PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame); |
---|
| 270 | // return val; |
---|
| 271 | } |
---|
| 272 | |
---|
[3854] | 273 | /** |
---|
| 274 | \brief a Sinusodial Interpolation between this keyframe and the next one |
---|
| 275 | \param timePassed The time passed since this Keyframe began |
---|
| 276 | */ |
---|
[3849] | 277 | template<class T> |
---|
| 278 | float tAnimation<T>::sine(float timePassed) const |
---|
| 279 | { |
---|
| 280 | float d = this->currentKeyFrame->value - this->nextKeyFrame->value; |
---|
| 281 | float e = 0.5 * d * (1 - cos(M_PI * timePassed / this->currentKeyFrame->duration)); |
---|
| 282 | return this->currentKeyFrame->value - e; |
---|
| 283 | /* |
---|
| 284 | return his->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) |
---|
| 285 | * sin(timePassed / this->currentKeyFrame->duration * M_PI); |
---|
| 286 | */ |
---|
| 287 | } |
---|
| 288 | |
---|
[3854] | 289 | /** |
---|
| 290 | \brief a cosine interpolation between this keyframe and the next one |
---|
| 291 | \param timePassed The time passed since this Keyframe began |
---|
| 292 | */ |
---|
[3849] | 293 | template<class T> |
---|
| 294 | float tAnimation<T>::cosine(float timePassed) const |
---|
| 295 | { |
---|
| 296 | float d = this->currentKeyFrame->value - this->nextKeyFrame->value; |
---|
| 297 | float e = 0.5 * d * (sin(M_PI * timePassed / this->currentKeyFrame->duration)); |
---|
| 298 | if( timePassed > 0.5*this->currentKeyFrame->duration) e = (d - e); |
---|
| 299 | return this->currentKeyFrame->value - e; |
---|
| 300 | /* |
---|
| 301 | return this->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) |
---|
| 302 | * cos(timePassed / this->currentKeyFrame->duration * M_PI); |
---|
| 303 | */ |
---|
| 304 | } |
---|
| 305 | |
---|
[3854] | 306 | /** |
---|
| 307 | \brief an exponential interpolation between this keyframe and the next one |
---|
| 308 | \param timePassed The time passed since this Keyframe began |
---|
| 309 | */ |
---|
[3849] | 310 | template<class T> |
---|
| 311 | float tAnimation<T>::exp(float timePassed) const |
---|
| 312 | { |
---|
| 313 | } |
---|
| 314 | |
---|
[3854] | 315 | /** |
---|
| 316 | \brief a negative exponential interpolation between this keyframe and the next one |
---|
| 317 | \param timePassed The time passed since this Keyframe began |
---|
| 318 | */ |
---|
[3849] | 319 | template<class T> |
---|
| 320 | float tAnimation<T>::negExp(float timePassed) const |
---|
| 321 | { |
---|
| 322 | float d = this->currentKeyFrame->value - this->nextKeyFrame->value; |
---|
| 323 | float e = d * (1.0 - expf(- timePassed * expFactor)); |
---|
| 324 | return this->currentKeyFrame->value - e; |
---|
| 325 | } |
---|
| 326 | |
---|
[3854] | 327 | /** |
---|
| 328 | \brief a quadratic interpolation between this keyframe and the next one |
---|
| 329 | \param timePassed The time passed since this Keyframe began |
---|
| 330 | */ |
---|
[3849] | 331 | template<class T> |
---|
| 332 | float tAnimation<T>::quadratic(float timePassed) const |
---|
| 333 | { |
---|
[3856] | 334 | this->linear(timePassed); |
---|
[3849] | 335 | } |
---|
[3851] | 336 | |
---|
[3855] | 337 | /** |
---|
| 338 | \brief some random animation (fluctuating) |
---|
| 339 | \param timePassed The time passed since this Keyframe began |
---|
| 340 | */ |
---|
| 341 | template<class T> |
---|
| 342 | float tAnimation<T>::random(float timePassed) const |
---|
| 343 | { |
---|
| 344 | return this->currentKeyFrame->value * (float)rand()/(float)RAND_MAX; |
---|
| 345 | } |
---|
[3851] | 346 | |
---|
| 347 | #endif /* _T_ANIMATION_H */ |
---|