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