[3851] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 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. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
| 13 | co-programmer: Benjamin Grauer |
---|
| 14 | |
---|
| 15 | 2005-04-17: Benjamin Grauer |
---|
| 16 | Rewritte all functions, so it will fit into the Animation-class |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | #include "animation3d.h" |
---|
| 21 | |
---|
| 22 | #include "p_node.h" |
---|
| 23 | |
---|
| 24 | using namespace std; |
---|
| 25 | |
---|
[3855] | 26 | /** |
---|
| 27 | \brief standard constructor |
---|
| 28 | */ |
---|
[3852] | 29 | Animation3D::Animation3D(PNode* object) |
---|
[3851] | 30 | { |
---|
[3852] | 31 | this->object = object; |
---|
| 32 | |
---|
[3851] | 33 | // create a new List |
---|
| 34 | this->keyFrameList = new tList<KeyFrame3D>(); |
---|
| 35 | KeyFrame3D* tmpKeyFrame = new KeyFrame3D; |
---|
| 36 | tmpKeyFrame->position = Vector(); |
---|
| 37 | tmpKeyFrame->direction = Quaternion(); |
---|
| 38 | keyFrameList->add(tmpKeyFrame); |
---|
| 39 | |
---|
| 40 | this->currentKeyFrame = tmpKeyFrame; |
---|
| 41 | this->nextKeyFrame = tmpKeyFrame; |
---|
| 42 | |
---|
| 43 | this->animFunc = &Animation3D::linear; |
---|
| 44 | } |
---|
| 45 | |
---|
[3855] | 46 | /** |
---|
| 47 | \brief standard deconstructor |
---|
| 48 | |
---|
| 49 | deletes all the Keyframes |
---|
| 50 | */ |
---|
[3851] | 51 | Animation3D::~Animation3D(void) |
---|
| 52 | { |
---|
| 53 | // delete all the KeyFrames |
---|
| 54 | tIterator<KeyFrame3D>* itKF = keyFrameList->getIterator(); |
---|
| 55 | KeyFrame3D* enumKF = itKF->nextElement(); |
---|
| 56 | while (enumKF) |
---|
| 57 | { |
---|
| 58 | delete enumKF; |
---|
| 59 | enumKF = itKF->nextElement(); |
---|
| 60 | } |
---|
| 61 | delete itKF; |
---|
| 62 | delete this->keyFrameList; |
---|
| 63 | } |
---|
| 64 | |
---|
[3855] | 65 | /** |
---|
| 66 | \brief rewinds the Animation to the beginning (first KeyFrame and time == 0) |
---|
| 67 | */ |
---|
[3851] | 68 | void Animation3D::rewind(void) |
---|
| 69 | { |
---|
| 70 | this->currentKeyFrame = keyFrameList->firstElement(); |
---|
| 71 | this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement()); |
---|
| 72 | this->localTime = 0.0; |
---|
| 73 | } |
---|
| 74 | |
---|
[3855] | 75 | /** |
---|
| 76 | \brief Appends a new Keyframe |
---|
| 77 | \param position The position of the new Keyframe |
---|
| 78 | \param direction The direction of the new Keyframe. |
---|
| 79 | \param duration The duration from the new KeyFrame to the next one |
---|
| 80 | \param animFunc The function to animate between this keyFrame and the next one |
---|
| 81 | */ |
---|
| 82 | void Animation3D::addKeyFrame(Vector position, Quaternion direction, float duration, ANIM_FUNCTION animFunc) |
---|
[3851] | 83 | { |
---|
| 84 | // some small check |
---|
| 85 | if (duration <= 0.0) |
---|
| 86 | duration = 1.0; |
---|
| 87 | |
---|
| 88 | KeyFrame3D* tmpKeyFrame; |
---|
| 89 | |
---|
| 90 | if (bHasKeys) |
---|
| 91 | { |
---|
| 92 | tmpKeyFrame = new KeyFrame3D; |
---|
| 93 | if (this->currentKeyFrame == this->nextKeyFrame) |
---|
| 94 | this->nextKeyFrame = tmpKeyFrame; |
---|
| 95 | this->keyFrameList->add(tmpKeyFrame); |
---|
| 96 | |
---|
| 97 | } |
---|
| 98 | else |
---|
| 99 | { |
---|
| 100 | tmpKeyFrame = this->keyFrameList->firstElement(); |
---|
| 101 | bHasKeys = true; |
---|
| 102 | this->setAnimFunc(animFunc); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | tmpKeyFrame->position = position; |
---|
| 106 | tmpKeyFrame->direction = direction; |
---|
| 107 | tmpKeyFrame->duration = duration; |
---|
| 108 | tmpKeyFrame->animFunc = animFunc; |
---|
| 109 | |
---|
| 110 | } |
---|
| 111 | |
---|
[3855] | 112 | /** |
---|
| 113 | \brief ticks the Animation |
---|
| 114 | \param dt how much time to tick |
---|
| 115 | */ |
---|
[3852] | 116 | void Animation3D::tick(float dt) |
---|
[3851] | 117 | { |
---|
| 118 | if (this->bRunning) |
---|
| 119 | { |
---|
[3852] | 120 | this->localTime += dt; |
---|
[3851] | 121 | if (localTime >= this->currentKeyFrame->duration) |
---|
| 122 | { |
---|
| 123 | // switching to the next Key-Frame |
---|
| 124 | this->localTime -= this->currentKeyFrame->duration; |
---|
| 125 | this->currentKeyFrame = this->nextKeyFrame; |
---|
| 126 | // checking, if we should still Play the animation |
---|
| 127 | if (this->currentKeyFrame == this->keyFrameList->lastElement()) |
---|
| 128 | { |
---|
| 129 | switch (this->postInfinity) |
---|
| 130 | { |
---|
| 131 | case ANIM_INF_CONSTANT: |
---|
[3853] | 132 | this->localTime = 0.0; |
---|
[3851] | 133 | this->bRunning = false; |
---|
| 134 | break; |
---|
| 135 | case ANIM_INF_REWIND: |
---|
| 136 | break; |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame); |
---|
| 140 | this->setAnimFunc(this->currentKeyFrame->animFunc); |
---|
| 141 | |
---|
| 142 | if( this->currentKeyFrame->animFunc == ANIM_NEG_EXP) |
---|
| 143 | { |
---|
| 144 | this->tmpVect = this->nextKeyFrame->position - this->currentKeyFrame->position; |
---|
| 145 | this->deltaT = 1/this->currentKeyFrame->duration * logf(1.0 + 600.0/this->tmpVect.len()); |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /* now animate it */ |
---|
| 150 | (this->*animFunc)(this->localTime); |
---|
| 151 | /* |
---|
| 152 | switch( this->movMode) |
---|
| 153 | { |
---|
| 154 | case LINEAR: |
---|
| 155 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
| 156 | *this->tmpVect = *this->tmpVect * this->localTime / this->currentFrame->time; |
---|
| 157 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
---|
| 158 | *this->lastPosition = *this->tmpVect; |
---|
| 159 | break; |
---|
| 160 | case EXP: |
---|
| 161 | |
---|
| 162 | break; |
---|
| 163 | case NEG_EXP: |
---|
| 164 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
| 165 | *this->tmpVect = *this->tmpVect * (1 - expf(- this->localTime * this->deltaT)); |
---|
| 166 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
---|
| 167 | *this->lastPosition = *this->tmpVect; |
---|
| 168 | break; |
---|
| 169 | case SIN: |
---|
| 170 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
| 171 | *this->tmpVect = *this->tmpVect * 0.5*(1 - cos(M_PI * this->localTime / this->currentFrame->time)); |
---|
| 172 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
---|
| 173 | *this->lastPosition = *this->tmpVect; |
---|
| 174 | break; |
---|
| 175 | case COS: |
---|
| 176 | |
---|
| 177 | break; |
---|
| 178 | case QUADRATIC: |
---|
| 179 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
| 180 | *this->tmpVect = *this->tmpVect * 1/3 * ldexpf(this->localTime, 3); |
---|
| 181 | break; |
---|
| 182 | default: |
---|
| 183 | break; |
---|
| 184 | } |
---|
| 185 | */ |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | |
---|
[3855] | 190 | /** |
---|
| 191 | \brief Sets The kind of Animation between this keyframe and the next one |
---|
| 192 | \param animFunc The Type of Animation to set |
---|
| 193 | */ |
---|
[3851] | 194 | void Animation3D::setAnimFunc(ANIM_FUNCTION animFunc) |
---|
| 195 | { |
---|
| 196 | switch (animFunc) |
---|
| 197 | { |
---|
| 198 | default: |
---|
| 199 | case ANIM_CONSTANT: |
---|
| 200 | this->animFunc = &Animation3D::constant; |
---|
| 201 | break; |
---|
| 202 | case ANIM_LINEAR: |
---|
| 203 | this->animFunc = &Animation3D::linear; |
---|
| 204 | break; |
---|
| 205 | case ANIM_SINE: |
---|
| 206 | this->animFunc = &Animation3D::sine; |
---|
| 207 | break; |
---|
| 208 | case ANIM_COSINE: |
---|
| 209 | this->animFunc = &Animation3D::cosine; |
---|
| 210 | break; |
---|
| 211 | case ANIM_EXP: |
---|
| 212 | this->animFunc = &Animation3D::exp; |
---|
| 213 | break; |
---|
| 214 | case ANIM_NEG_EXP: |
---|
| 215 | this->animFunc = &Animation3D::negExp; |
---|
| 216 | break; |
---|
| 217 | case ANIM_QUADRATIC: |
---|
| 218 | this->animFunc = &Animation3D::quadratic; |
---|
| 219 | break; |
---|
| 220 | case ANIM_RANDOM: |
---|
| 221 | this->animFunc = &Animation3D::random; |
---|
| 222 | break; |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | |
---|
[3855] | 226 | /** |
---|
| 227 | \brief stays at the value of the currentKeyFrame |
---|
| 228 | \param timePassed The time passed since this Keyframe began |
---|
| 229 | */ |
---|
[3852] | 230 | void Animation3D::constant(float timePassed) const |
---|
[3851] | 231 | { |
---|
[3852] | 232 | this->object->setRelCoor(this->currentKeyFrame->position); |
---|
[3851] | 233 | |
---|
[3852] | 234 | /* |
---|
| 235 | this->tmpVect = this->nextKeyFrame->position - this->currentKeyFrame->position; |
---|
| 236 | this->tmpVect = this->tmpVect * this->localTime / this->currentKeyFrame->duration; |
---|
| 237 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
---|
| 238 | this->lastPosition = this->tmpVect; |
---|
| 239 | */ |
---|
[3851] | 240 | } |
---|
| 241 | |
---|
[3855] | 242 | /** |
---|
| 243 | \brief linear interpolation between this keyframe and the next one |
---|
| 244 | \param timePassed The time passed since this Keyframe began |
---|
[3856] | 245 | |
---|
| 246 | \todo implement also do this for direction |
---|
[3855] | 247 | */ |
---|
[3852] | 248 | void Animation3D::linear(float timePassed) const |
---|
[3851] | 249 | { |
---|
[3852] | 250 | this->object->setRelCoor(this->currentKeyFrame->position + |
---|
| 251 | (this->nextKeyFrame->position - this->currentKeyFrame->position) * |
---|
| 252 | (timePassed/this->currentKeyFrame->duration)); |
---|
[3851] | 253 | } |
---|
| 254 | |
---|
[3855] | 255 | /** |
---|
| 256 | \brief a Sinusodial Interpolation between this keyframe and the next one |
---|
| 257 | \param timePassed The time passed since this Keyframe began |
---|
[3856] | 258 | |
---|
| 259 | \todo implement |
---|
[3855] | 260 | */ |
---|
[3852] | 261 | void Animation3D::sine(float timePassed) const |
---|
[3851] | 262 | { |
---|
[3856] | 263 | this->linear(timePassed); |
---|
[3851] | 264 | } |
---|
| 265 | |
---|
[3855] | 266 | /** |
---|
| 267 | \brief a cosine interpolation between this keyframe and the next one |
---|
| 268 | \param timePassed The time passed since this Keyframe began |
---|
[3856] | 269 | |
---|
| 270 | \todo implement |
---|
[3855] | 271 | */ |
---|
[3852] | 272 | void Animation3D::cosine(float timePassed) const |
---|
[3851] | 273 | { |
---|
[3856] | 274 | this->linear(timePassed); |
---|
[3851] | 275 | } |
---|
| 276 | |
---|
[3855] | 277 | /** |
---|
| 278 | \brief an exponential interpolation between this keyframe and the next one |
---|
| 279 | \param timePassed The time passed since this Keyframe began |
---|
| 280 | */ |
---|
[3852] | 281 | void Animation3D::exp(float timePassed) const |
---|
[3851] | 282 | { |
---|
[3856] | 283 | this->linear(timePassed); |
---|
[3851] | 284 | } |
---|
| 285 | |
---|
[3855] | 286 | /** |
---|
| 287 | \brief a negative exponential interpolation between this keyframe and the next one |
---|
| 288 | \param timePassed The time passed since this Keyframe began |
---|
[3856] | 289 | |
---|
| 290 | \todo implement |
---|
[3855] | 291 | */ |
---|
[3852] | 292 | void Animation3D::negExp(float timePassed) const |
---|
[3851] | 293 | { |
---|
[3855] | 294 | this->linear(timePassed); |
---|
[3851] | 295 | } |
---|
| 296 | |
---|
[3855] | 297 | /** |
---|
| 298 | \brief a quadratic interpolation between this keyframe and the next one |
---|
| 299 | \param timePassed The time passed since this Keyframe began |
---|
[3856] | 300 | |
---|
| 301 | \todo implement |
---|
[3855] | 302 | */ |
---|
[3852] | 303 | void Animation3D::quadratic(float timePassed) const |
---|
[3851] | 304 | { |
---|
[3856] | 305 | this->linear(timePassed); |
---|
[3851] | 306 | } |
---|
| 307 | |
---|
[3855] | 308 | /** |
---|
| 309 | \brief some random animation (fluctuating) |
---|
| 310 | \param timePassed The time passed since this Keyframe began |
---|
| 311 | */ |
---|
[3852] | 312 | void Animation3D::random(float timePassed) const |
---|
[3851] | 313 | { |
---|
[3856] | 314 | this->object->setRelCoor(this->currentKeyFrame->position * (float)rand()/(float)RAND_MAX); |
---|
| 315 | this->object->setRelDir(this->currentKeyFrame->direction * (float)rand()/(float)RAND_MAX); |
---|
[3851] | 316 | } |
---|