[3573] | 1 | |
---|
| 2 | |
---|
[3738] | 3 | |
---|
[3573] | 4 | /* |
---|
| 5 | orxonox - the future of 3D-vertical-scrollers |
---|
| 6 | |
---|
| 7 | Copyright (C) 2004 orx |
---|
| 8 | |
---|
| 9 | This program is free software; you can redistribute it and/or modify |
---|
| 10 | it under the terms of the GNU General Public License as published by |
---|
| 11 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 12 | any later version. |
---|
| 13 | |
---|
| 14 | ### File Specific: |
---|
| 15 | main-programmer: Patrick Boenzli |
---|
| 16 | co-programmer: ... |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | #include "simple_animation.h" |
---|
| 21 | #include "stdincl.h" |
---|
[3720] | 22 | #include "vector.h" |
---|
[3729] | 23 | #include "world_entity.h" |
---|
[3573] | 24 | |
---|
| 25 | using namespace std; |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | |
---|
[3727] | 29 | SimpleAnimation* SimpleAnimation::singletonRef = 0; |
---|
[3573] | 30 | /** |
---|
[3727] | 31 | \brief gets the singleton instance |
---|
| 32 | \returns singleton instance |
---|
| 33 | */ |
---|
| 34 | SimpleAnimation* SimpleAnimation::getInstance() |
---|
| 35 | { |
---|
| 36 | if( singletonRef == NULL) |
---|
| 37 | singletonRef = new SimpleAnimation(); |
---|
| 38 | return singletonRef; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
[3573] | 42 | \brief standard constructor |
---|
| 43 | */ |
---|
[3727] | 44 | SimpleAnimation::SimpleAnimation () |
---|
[3573] | 45 | { |
---|
| 46 | this->setClassName ("SimpleAnimation"); |
---|
| 47 | this->frames = new tList<KeyFrame>(); |
---|
[3738] | 48 | this->animators = new tList<Animation>(); |
---|
[3573] | 49 | this->localTime = 0; |
---|
[3719] | 50 | this->bRunning = false; |
---|
| 51 | this->currentFrame = NULL; |
---|
| 52 | this->lastFrame = NULL; |
---|
[3720] | 53 | |
---|
| 54 | this->tmpVect = new Vector(); |
---|
[3729] | 55 | this->lastPosition = new Vector(); |
---|
[3733] | 56 | this->deltaT = 0.2; |
---|
[3573] | 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | \brief standard deconstructor |
---|
| 62 | |
---|
| 63 | */ |
---|
| 64 | SimpleAnimation::~SimpleAnimation () |
---|
| 65 | { |
---|
[3661] | 66 | tIterator<KeyFrame>* iterator = this->frames->getIterator(); |
---|
| 67 | KeyFrame* frame = iterator->nextElement(); |
---|
[3573] | 68 | while( frame != NULL) |
---|
| 69 | { |
---|
| 70 | delete frame; |
---|
[3661] | 71 | frame = iterator->nextElement(); |
---|
[3573] | 72 | } |
---|
[3661] | 73 | delete iterator; |
---|
[3573] | 74 | delete this->frames; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | |
---|
[3727] | 78 | /** |
---|
| 79 | \brief this determines the start of an Animator Describtion |
---|
[3573] | 80 | |
---|
[3727] | 81 | this can then be followed by different commands like addKeyFrame(..) etc. and |
---|
| 82 | will be closed with AnimatiorEnd() |
---|
| 83 | */ |
---|
[3729] | 84 | void SimpleAnimation::animatorBegin() |
---|
[3727] | 85 | { |
---|
| 86 | this->bDescriptive = true; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
[3573] | 90 | /** |
---|
[3727] | 91 | \brief this determines the end of an Animator Describtion |
---|
| 92 | |
---|
| 93 | this can then be followed by different commands like addKeyFrame(..) etc. and |
---|
| 94 | will be closed with AnimatiorEnd() |
---|
| 95 | */ |
---|
[3729] | 96 | void SimpleAnimation::animatorEnd() |
---|
[3727] | 97 | { |
---|
| 98 | this->workingObject = NULL; |
---|
[3743] | 99 | this->workingAnimator = NULL; |
---|
[3727] | 100 | this->bDescriptive = false; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
[3739] | 104 | /* |
---|
| 105 | Vector* lastPosition; |
---|
| 106 | Vector* tmpVect; |
---|
| 107 | tList<KeyFrame>* frames; |
---|
| 108 | animationMode animMode; |
---|
| 109 | movementMode movMode; |
---|
| 110 | bool bRunning; |
---|
| 111 | float deltaT; |
---|
| 112 | */ |
---|
| 113 | |
---|
[3727] | 114 | /** |
---|
| 115 | \brief select an object to work on by using this function |
---|
| 116 | \param object wo work on |
---|
| 117 | */ |
---|
| 118 | void SimpleAnimation::selectObject(WorldEntity* entity) |
---|
| 119 | { |
---|
[3743] | 120 | printf("SimpleAnimation::selectObject() - selecing active object\n"); |
---|
[3739] | 121 | Animation* anim = getAnimationFromWorldEntity(entity); |
---|
| 122 | if( anim == NULL) |
---|
| 123 | { |
---|
| 124 | printf("SimpleAnimation::selectObject() - object not found, creating one\n"); |
---|
| 125 | anim = new Animation; |
---|
| 126 | anim->object = entity; |
---|
| 127 | anim->lastPosition = new Vector(); |
---|
| 128 | anim->tmpVect = new Vector(); |
---|
[3743] | 129 | anim->frames = new tList<KeyFrame>(); |
---|
[3739] | 130 | bRunning = false; |
---|
| 131 | deltaT = 0.0; |
---|
| 132 | this->animators->add(anim); |
---|
| 133 | } |
---|
[3743] | 134 | else |
---|
| 135 | printf("SimpleAnimation::selectObject() - animation already existent, using it\n"); |
---|
[3739] | 136 | this->workingAnimator = anim; |
---|
[3743] | 137 | printf("SimpleAnimation::selectObject() - selection completed\n"); |
---|
[3727] | 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | /** |
---|
[3573] | 143 | \brief adds a keyframe with properties |
---|
| 144 | \param the point of the object |
---|
[3729] | 145 | \param and the direction of it |
---|
[3573] | 146 | \param at this time |
---|
| 147 | */ |
---|
[3729] | 148 | void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time) |
---|
[3573] | 149 | { |
---|
[3743] | 150 | if( !this->bDescriptive || this->workingAnimator == NULL) |
---|
[3727] | 151 | { |
---|
| 152 | PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n"); |
---|
| 153 | return; |
---|
| 154 | } |
---|
[3726] | 155 | KeyFrame* frame = new KeyFrame; |
---|
| 156 | frame->position = point; |
---|
[3729] | 157 | frame->direction = direction; |
---|
[3726] | 158 | frame->time = time; |
---|
| 159 | frame->mode = DEFAULT_ANIMATION_MODE; |
---|
[3743] | 160 | frame->object = this->workingAnimator->object; |
---|
| 161 | this->workingAnimator->frames->add(frame); |
---|
[3573] | 162 | } |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | /** |
---|
| 166 | \brief adds a keyframe with properties |
---|
| 167 | \param the point of the object |
---|
[3729] | 168 | \param and the direction of it |
---|
[3573] | 169 | \param at this time |
---|
| 170 | \param function of the velocity of the movement |
---|
| 171 | */ |
---|
[3729] | 172 | void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time, movementMode mode) |
---|
[3573] | 173 | { |
---|
[3743] | 174 | if( !this->bDescriptive || this->workingAnimator == NULL) |
---|
[3727] | 175 | { |
---|
| 176 | PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n"); |
---|
| 177 | return; |
---|
| 178 | } |
---|
[3726] | 179 | KeyFrame* frame = new KeyFrame; |
---|
| 180 | frame->position = point; |
---|
[3729] | 181 | frame->direction = direction; |
---|
[3726] | 182 | frame->time = time; |
---|
| 183 | frame->mode = mode; |
---|
[3743] | 184 | frame->object = this->workingAnimator->object; |
---|
| 185 | this->workingAnimator->frames->add(frame); |
---|
[3573] | 186 | } |
---|
| 187 | |
---|
| 188 | /** |
---|
| 189 | \brief adds a already defined keyframe |
---|
| 190 | \param the keyframe to add |
---|
| 191 | */ |
---|
| 192 | void SimpleAnimation::addKeyFrame(KeyFrame* frame) |
---|
| 193 | { |
---|
[3743] | 194 | printf("SimpleAnimation::addKeyFrame() - adding frame\n"); |
---|
| 195 | if( !this->bDescriptive || this->workingAnimator == NULL) |
---|
[3727] | 196 | { |
---|
| 197 | PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n"); |
---|
| 198 | return; |
---|
| 199 | } |
---|
[3743] | 200 | frame->object = this->workingAnimator->object; |
---|
| 201 | this->workingAnimator->frames->add(frame); |
---|
| 202 | printf("SimpleAnimation::addKeyFrame() - addition completed\n"); |
---|
[3573] | 203 | } |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | /** |
---|
| 207 | \brief clear the list of keyframes, deleting all keyframes included |
---|
| 208 | */ |
---|
| 209 | void SimpleAnimation::reset() |
---|
| 210 | { |
---|
[3743] | 211 | /* |
---|
[3661] | 212 | tIterator<KeyFrame>* iterator = this->frames->getIterator(); |
---|
| 213 | KeyFrame* frame = iterator->nextElement(); |
---|
[3573] | 214 | while( frame != NULL) |
---|
| 215 | { |
---|
| 216 | delete frame; |
---|
[3661] | 217 | frame = iterator->nextElement(); |
---|
[3573] | 218 | } |
---|
[3661] | 219 | delete iterator; |
---|
[3573] | 220 | delete this->frames; |
---|
| 221 | |
---|
| 222 | this->frames = new tList<KeyFrame>(); |
---|
| 223 | this->localTime = 0; |
---|
[3719] | 224 | this->bRunning = false; |
---|
[3573] | 225 | |
---|
[3719] | 226 | this->currentFrame = NULL; |
---|
| 227 | this->lastFrame = NULL; |
---|
[3743] | 228 | */ |
---|
[3573] | 229 | } |
---|
| 230 | |
---|
| 231 | /** |
---|
| 232 | \brief starts the animation, therefore listens to tick signals |
---|
| 233 | */ |
---|
| 234 | void SimpleAnimation::start() |
---|
[3719] | 235 | { |
---|
| 236 | if( this->bRunning) |
---|
| 237 | { |
---|
| 238 | PRINTF(2)("SimpleAnimatin is already running. You are trying to start it again.\n"); |
---|
| 239 | return; |
---|
| 240 | } |
---|
| 241 | |
---|
[3744] | 242 | if( this->workingAnimator == NULL) |
---|
| 243 | { |
---|
| 244 | PRINTF(1)("You have no target selected to start: either do this with start(target) or by prev selecting it\n"); |
---|
| 245 | return; |
---|
| 246 | } |
---|
| 247 | this->workingAnimator->localTime = 0.0; |
---|
| 248 | this->workingAnimator->bRunning = true; |
---|
| 249 | this->workingAnimator->currentFrame = this->workingAnimator->frames->firstElement(); |
---|
| 250 | this->workingAnimator->lastFrame = this->workingAnimator->frames->nextElement(this->workingAnimator->currentFrame); |
---|
[3743] | 251 | |
---|
| 252 | /* |
---|
| 253 | tIterator<Animation>* iterator = this->animators->getIterator(); |
---|
| 254 | Animation* anim = iterator->nextElement(); |
---|
| 255 | while( anim != NULL) |
---|
| 256 | { |
---|
| 257 | printf("SimpleAnimation::start() - initializing an animaion\n"); |
---|
| 258 | anim->currentFrame = anim->frames->firstElement(); |
---|
| 259 | anim->lastFrame = anim->frames->nextElement(anim->currentFrame); |
---|
| 260 | anim = iterator->nextElement(); |
---|
| 261 | } |
---|
| 262 | */ |
---|
[3719] | 263 | } |
---|
[3573] | 264 | |
---|
| 265 | |
---|
| 266 | /** |
---|
| 267 | \brief stops the animation, immune to tick signals |
---|
| 268 | */ |
---|
| 269 | void SimpleAnimation::stop() |
---|
[3719] | 270 | { |
---|
| 271 | this->bRunning = false; |
---|
| 272 | } |
---|
[3573] | 273 | |
---|
| 274 | /** |
---|
| 275 | \brief stops and then starts the animation from begining |
---|
| 276 | */ |
---|
| 277 | void SimpleAnimation::restart() |
---|
| 278 | { |
---|
| 279 | this->localTime = 0; |
---|
[3743] | 280 | //this->lastFrame = this->frames->firstElement(); |
---|
| 281 | //this->currentFrame = this->frames->nextElement(this->currentFrame); |
---|
[3719] | 282 | this->bRunning = true; |
---|
[3573] | 283 | } |
---|
| 284 | |
---|
| 285 | /** |
---|
| 286 | \brief pauses the animation until resumed |
---|
| 287 | */ |
---|
| 288 | void SimpleAnimation::pause() |
---|
| 289 | { |
---|
[3719] | 290 | this->bRunning = false; |
---|
[3573] | 291 | } |
---|
| 292 | |
---|
| 293 | /** |
---|
| 294 | \brief resumes a pause, if not paused, no effect |
---|
| 295 | */ |
---|
| 296 | void SimpleAnimation::resume() |
---|
| 297 | { |
---|
[3719] | 298 | this->bRunning = true; |
---|
[3573] | 299 | } |
---|
| 300 | |
---|
| 301 | |
---|
| 302 | /** |
---|
| 303 | \brief heart beat, next animation step |
---|
| 304 | */ |
---|
| 305 | void SimpleAnimation::tick(float time) |
---|
| 306 | { |
---|
[3738] | 307 | tIterator<Animation>* iterator = this->animators->getIterator(); |
---|
| 308 | Animation* anim = iterator->nextElement(); |
---|
| 309 | while( anim != NULL) |
---|
[3573] | 310 | { |
---|
[3738] | 311 | if( anim->bRunning) |
---|
[3743] | 312 | { |
---|
[3744] | 313 | anim->localTime += time; |
---|
[3738] | 314 | /* first get the current frame via time-stamps */ |
---|
[3744] | 315 | while( anim->localTime > anim->currentFrame->time) |
---|
[3738] | 316 | { |
---|
| 317 | printf("SimpleAnimation::tick(...) - changing Frame\n"); |
---|
| 318 | |
---|
[3744] | 319 | anim->localTime -= anim->currentFrame->time; |
---|
[3738] | 320 | //this->currentFrame->object->setRelCoor(*this->currentFrame->position); |
---|
| 321 | *anim->lastPosition = *anim->currentFrame->position; |
---|
| 322 | |
---|
| 323 | anim->lastFrame = anim->currentFrame; |
---|
| 324 | anim->currentFrame = anim->frames->nextElement(anim->currentFrame); |
---|
| 325 | anim->movMode = anim->currentFrame->mode; |
---|
| 326 | if( anim->movMode == NEG_EXP) |
---|
| 327 | { |
---|
| 328 | *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; |
---|
| 329 | anim->deltaT = 1/anim->currentFrame->time * logf(1.0 + 600.0/anim->tmpVect->len()); |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | /* now animate it */ |
---|
| 334 | switch( anim->movMode) |
---|
| 335 | { |
---|
| 336 | case LINEAR: |
---|
| 337 | |
---|
| 338 | *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; |
---|
[3744] | 339 | *anim->tmpVect = *anim->tmpVect * anim->localTime / anim->currentFrame->time; |
---|
[3738] | 340 | anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect); |
---|
| 341 | *anim->lastPosition = *anim->tmpVect; |
---|
| 342 | break; |
---|
| 343 | case EXP: |
---|
| 344 | |
---|
| 345 | break; |
---|
| 346 | case NEG_EXP: |
---|
| 347 | *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; |
---|
[3744] | 348 | *anim->tmpVect = *anim->tmpVect * (1 - exp(- anim->localTime * anim->deltaT)); |
---|
[3738] | 349 | anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect); |
---|
| 350 | *anim->lastPosition = *anim->tmpVect; |
---|
| 351 | break; |
---|
| 352 | case SIN: |
---|
| 353 | *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; |
---|
[3744] | 354 | *anim->tmpVect = *anim->tmpVect * 0.5*(1 - cos(M_PI * anim->localTime / anim->currentFrame->time)); |
---|
[3738] | 355 | anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect); |
---|
| 356 | *anim->lastPosition = *anim->tmpVect; |
---|
| 357 | break; |
---|
| 358 | case COS: |
---|
| 359 | |
---|
| 360 | break; |
---|
| 361 | case QUADRATIC: |
---|
| 362 | *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position; |
---|
[3744] | 363 | *anim->tmpVect = *anim->tmpVect * 1/3 * ldexpf(anim->localTime, 3); |
---|
[3738] | 364 | break; |
---|
| 365 | default: |
---|
| 366 | break; |
---|
| 367 | } |
---|
[3733] | 368 | } |
---|
[3738] | 369 | anim = iterator->nextElement(); |
---|
[3719] | 370 | } |
---|
[3738] | 371 | delete anim; |
---|
[3573] | 372 | } |
---|
[3739] | 373 | |
---|
| 374 | |
---|
| 375 | |
---|
| 376 | Animation* SimpleAnimation::getAnimationFromWorldEntity(WorldEntity* entity) |
---|
| 377 | { |
---|
| 378 | tIterator<Animation>* iterator = this->animators->getIterator(); |
---|
| 379 | Animation* anim = iterator->nextElement(); |
---|
| 380 | while( anim != NULL) |
---|
| 381 | { |
---|
| 382 | if( anim->object == entity) |
---|
| 383 | return anim; |
---|
| 384 | anim = iterator->nextElement(); |
---|
| 385 | } |
---|
| 386 | delete iterator; |
---|
| 387 | return NULL; |
---|
| 388 | } |
---|