Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/animation/animation3d.cc @ 4213

Last change on this file since 4213 was 4000, checked in by patrick, 20 years ago

orxonox/trunk: weapon animation not realy nice but working now.. :)

File size: 14.3 KB
RevLine 
[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
[3980]17   2005-04-25: Patrick Boenzli
18          Extended the framework to support quatSlerp rotations. Each frame now supports diff mov/rot types. Implemented mov/rot functions
[3851]19*/
20
[3863]21#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ANIM
[3851]22
23#include "animation3d.h"
24
25#include "p_node.h"
26
27using namespace std;
28
[3855]29/**
30   \brief standard constructor
31*/
[3852]32Animation3D::Animation3D(PNode* object)
[3851]33{
[3852]34  this->object = object;
35
[3851]36  // create a new List
37  this->keyFrameList = new tList<KeyFrame3D>();
38  KeyFrame3D* tmpKeyFrame = new KeyFrame3D;
39  tmpKeyFrame->position = Vector();
40  tmpKeyFrame->direction = Quaternion();
41  keyFrameList->add(tmpKeyFrame);
42
43  this->currentKeyFrame = tmpKeyFrame;
44  this->nextKeyFrame = tmpKeyFrame;
45
[3973]46  this->animFuncMov = &Animation3D::mLinear;
47  this->animFuncRot = &Animation3D::rLinear;
[3986]48
[3851]49}
50
[3855]51/**
52   \brief standard deconstructor
53   
54   deletes all the Keyframes
55*/
[3851]56Animation3D::~Animation3D(void)
57{
58  // delete all the KeyFrames
59  tIterator<KeyFrame3D>* itKF = keyFrameList->getIterator();
60  KeyFrame3D*  enumKF = itKF->nextElement();
61  while (enumKF)
62    {
63      delete enumKF;
64      enumKF = itKF->nextElement();
65    }
66  delete itKF;
67  delete this->keyFrameList;
68}
69
[3855]70/**
71   \brief rewinds the Animation to the beginning (first KeyFrame and time == 0)
72*/
[3851]73void Animation3D::rewind(void)
74{
75  this->currentKeyFrame = keyFrameList->firstElement();
76  this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement());
77  this->localTime = 0.0;
[3973]78  this->setAnimFuncMov(this->currentKeyFrame->animFuncMov);
79  this->setAnimFuncRot(this->currentKeyFrame->animFuncRot);
[3851]80}
81
[3855]82/**
83   \brief Appends a new Keyframe
84   \param position The position of the new Keyframe
85   \param direction The direction of the new Keyframe.
86   \param duration The duration from the new KeyFrame to the next one
[3978]87   \param animFuncMov The function to animate position between this keyFrame and the next one
88   \param animFuncMov The function to animate rotation between this keyFrame and the next one
[3855]89*/
[3973]90void Animation3D::addKeyFrame(Vector position, Quaternion direction, float duration, ANIM_FUNCTION animFuncMov, ANIM_FUNCTION animFuncRot)
[3851]91{
92  // some small check
93  if (duration <= 0.0)
94    duration = 1.0;
[3978]95  // if the Rotation-Animation-function is set ANIM_NULL, animFuncRot will match animFuncRot
[3979]96  if (animFuncMov == ANIM_NULL)
97    animFuncMov = ANIM_DEFAULT_FUNCTION;
[3978]98  if (animFuncRot == ANIM_NULL)
99    animFuncRot = animFuncMov;
[3851]100
101  KeyFrame3D* tmpKeyFrame;
102   
[3876]103  // when adding the first frame
104  if (this->keyFrameCount == 0)
[3851]105    {
[3876]106      tmpKeyFrame = this->keyFrameList->firstElement();
[4000]107      //this->setAnimFuncMov(animFuncMov);
108      //this->setAnimFuncRot(animFuncRot);
[3876]109    }
110  else
111    {
[3851]112      tmpKeyFrame = new KeyFrame3D;
[3876]113      // when adding the second frame
[3851]114      if (this->currentKeyFrame == this->nextKeyFrame)
115        this->nextKeyFrame = tmpKeyFrame;
116      this->keyFrameList->add(tmpKeyFrame);
117    }
118
119  tmpKeyFrame->position = position;
[3981]120  //tmpKeyFrame->lastPosition = position;
[3851]121  tmpKeyFrame->direction = direction;
122  tmpKeyFrame->duration = duration;
[3973]123  tmpKeyFrame->animFuncMov = animFuncMov;
124  tmpKeyFrame->animFuncRot = animFuncRot;
[3876]125  this->keyFrameCount++;
[3851]126}
127
[3973]128
129
[3855]130/**
131   \brief ticks the Animation
132   \param dt how much time to tick
133*/
[3852]134void Animation3D::tick(float dt)
[3851]135{
136  if (this->bRunning)
137    { 
[3852]138      this->localTime += dt;
[3851]139      if (localTime >= this->currentKeyFrame->duration)
140        {
[3982]141          if (likely(this->keyFramesToPlay != 0))
142            {
143              if (unlikely(this->keyFramesToPlay > 0))
144                --this->keyFramesToPlay;
145              // switching to the next Key-Frame
146              this->localTime -= this->currentKeyFrame->duration;
147              this->currentKeyFrame = this->nextKeyFrame;
148              // checking, if we should still Play the animation
149              if (this->currentKeyFrame == this->keyFrameList->lastElement())
150                this->handleInfinity();
151              this->nextKeyFrame = this->keyFrameList->nextElement(this->currentKeyFrame);
152              this->setAnimFuncMov(this->currentKeyFrame->animFuncMov);
153              this->setAnimFuncRot(this->currentKeyFrame->animFuncRot);
154            }
155          else
156            this->pause();
157        }     
[3851]158      /* now animate it */
[3973]159      (this->*animFuncMov)(this->localTime);
160      (this->*animFuncRot)(this->localTime);
[3851]161    }
162}
163
164
[3973]165/*==Movement Section==========================================================*/
166
[3855]167/**
[3973]168   \brief Sets The kind of movment Animation between this keyframe and the next one
[3855]169   \param animFunc The Type of Animation to set
170*/
[3973]171void Animation3D::setAnimFuncMov(ANIM_FUNCTION animFuncMov)
[3851]172{
[3973]173  switch (animFuncMov)
[3851]174    {
175    case ANIM_CONSTANT:
[3973]176      this->animFuncMov = &Animation3D::mConstant;
[3851]177      break;
178    case ANIM_LINEAR:
[3973]179      this->animFuncMov = &Animation3D::mLinear;
[3993]180      this->object->setRelCoor(this->currentKeyFrame->position);
[3981]181      this->currentKeyFrame->lastPosition = Vector();
[3851]182      break;
183    case ANIM_SINE:
[3973]184      this->animFuncMov = &Animation3D::mSine;
[3993]185      this->object->setRelCoor(this->currentKeyFrame->position);
[3984]186      this->currentKeyFrame->lastPosition = Vector();
[3851]187      break;
188    case ANIM_COSINE:
[3973]189      this->animFuncMov = &Animation3D::mCosine;
[3993]190      this->object->setRelCoor(this->currentKeyFrame->position);
[3985]191      this->currentKeyFrame->lastPosition = Vector();
[3851]192      break;
193    case ANIM_EXP:
[3993]194      this->object->setRelCoor(this->currentKeyFrame->position);
[3973]195      this->animFuncMov = &Animation3D::mExp;
[3851]196      break;
197    case ANIM_NEG_EXP:
[3973]198      this->animFuncMov = &Animation3D::mNegExp;
[3993]199      this->object->setRelCoor(this->currentKeyFrame->position);
[3977]200      this->expFactorMov = -1.0 / this->currentKeyFrame->duration * logf(DELTA_X_3D);
[3986]201      this->currentKeyFrame->lastPosition = Vector();
[3851]202      break;
203    case ANIM_QUADRATIC:
[3993]204      this->object->setRelCoor(this->currentKeyFrame->position);
[3973]205      this->animFuncMov = &Animation3D::mQuadratic;
[3851]206      break;
207    case ANIM_RANDOM:
[3993]208      this->object->setRelCoor(this->currentKeyFrame->position);
[3973]209      this->animFuncMov = &Animation3D::mRandom;
[3851]210      break;
[3986]211    default:
212      break;
[3851]213    }
214}
215
[3973]216
217
[3855]218/**
219   \brief stays at the value of the currentKeyFrame
220   \param timePassed The time passed since this Keyframe began
221*/
[3973]222void Animation3D::mConstant(float timePassed) const
[3851]223{
[3986]224  //this->object->setRelCoor(this->currentKeyFrame->position);
[3851]225
[3852]226  /*
227    this->tmpVect = this->nextKeyFrame->position - this->currentKeyFrame->position;
228    this->tmpVect = this->tmpVect * this->localTime / this->currentKeyFrame->duration;
229    this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect);
230    this->lastPosition = this->tmpVect;
231  */
[3851]232}
233
[3855]234/**
235   \brief linear interpolation between this keyframe and the next one
236   \param timePassed The time passed since this Keyframe began
[3856]237
238   \todo implement also do this for direction
[3855]239*/
[3973]240void Animation3D::mLinear(float timePassed) const
[3851]241{
[3981]242  Vector v = (this->nextKeyFrame->position - this->currentKeyFrame->position) * (timePassed/this->currentKeyFrame->duration);
243  this->object->shiftCoor(v - this->currentKeyFrame->lastPosition);
244  this->currentKeyFrame->lastPosition = v;
[3851]245}
246
[3855]247/**
248   \brief a Sinusodial Interpolation between this keyframe and the next one
249   \param timePassed The time passed since this Keyframe began
[3856]250
251   \todo implement
[3855]252*/
[3973]253void Animation3D::mSine(float timePassed) const
[3851]254{
[3984]255  Vector v;
256  if( timePassed  < this->currentKeyFrame->duration/2.0)
257    v = (this->nextKeyFrame->position - this->currentKeyFrame->position) * sin( M_PI * timePassed /this->currentKeyFrame->duration) / 2.0;
258  else
259    v = (this->nextKeyFrame->position - this->currentKeyFrame->position) * (2.0 + sin( M_PI * (- timePassed /this->currentKeyFrame->duration)) )/ 2.0;
260 
261  this->object->shiftCoor(v - this->currentKeyFrame->lastPosition);
262  this->currentKeyFrame->lastPosition = v;
[3851]263}
264
[3965]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*/
[3973]272void Animation3D::mCosine(float timePassed) const
[3851]273{
[3985]274  Vector v;
275  v = (this->nextKeyFrame->position - this->currentKeyFrame->position) * (1.0 + cos( M_PI * timePassed / this->currentKeyFrame->duration))/2.0;
276  this->object->shiftCoor(v - this->currentKeyFrame->lastPosition);
277  this->currentKeyFrame->lastPosition = v;
278
279
280  /*
[3967]281  this->object->setRelCoor( this->nextKeyFrame->position -
282                            (this->nextKeyFrame->position - this->currentKeyFrame->position) *
283                            (1.0 + cos( M_PI * timePassed / this->currentKeyFrame->duration))/2.0);
[3985]284  */
[3851]285}
286
[3967]287
288
[3855]289/**
290   \brief an exponential interpolation between this keyframe and the next one
291   \param timePassed The time passed since this Keyframe began
292*/
[3973]293void Animation3D::mExp(float timePassed) const
[3851]294{
[3968]295  PRINTF(0)("no exp animation3d defined\n");
[3973]296  this->mLinear(timePassed);
[3851]297}
298
[3855]299/**
300   \brief a negative exponential interpolation between this keyframe and the next one
301   \param timePassed The time passed since this Keyframe began
302*/
[3973]303void Animation3D::mNegExp(float timePassed) const
[3851]304{
[3986]305  Vector v;
306  v = (this->nextKeyFrame->position - this->currentKeyFrame->position) * (1.0 - expf(- timePassed * expFactorMov));
307  this->object->shiftCoor(v - this->currentKeyFrame->lastPosition);
308  this->currentKeyFrame->lastPosition = v;
309
310  /*
[3964]311  this->object->setRelCoor( this->currentKeyFrame->position +
312                            (this->nextKeyFrame->position - this->currentKeyFrame->position) *
[3977]313                            (1.0 - expf(- timePassed * expFactorMov)) );
[3986]314  */
[3851]315}
316
[3964]317
[3855]318/**
319   \brief a quadratic interpolation between this keyframe and the next one
320   \param timePassed The time passed since this Keyframe began
[3856]321
322   \todo implement
[3855]323*/
[3973]324void Animation3D::mQuadratic(float timePassed) const
[3851]325{
[3968]326  PRINTF(0)("no quadratic animation3d defined\n");
[3973]327  this->mLinear(timePassed);
[3851]328}
329
[3855]330/**
331   \brief some random animation (fluctuating)
332   \param timePassed The time passed since this Keyframe began
333*/
[3973]334void Animation3D::mRandom(float timePassed) const
[3851]335{
[3986]336  /*
[3874]337  this->object->setRelCoor(this->currentKeyFrame->position +
338                           (this->nextKeyFrame->position - this->currentKeyFrame->position) * (float)rand()/(float)RAND_MAX);
339  this->object->setRelDir(this->currentKeyFrame->direction +
340                          (this->nextKeyFrame->direction - this->currentKeyFrame->direction)* (float)rand()/(float)RAND_MAX);
[3986]341  */
[3851]342}
[3973]343
344
345/*==Rotation Section==========================================================*/
346
347
348/**
349   \brief Sets The kind of rotation Animation between this keyframe and the next one
350   \param animFunc The Type of Animation to set
351*/
352void Animation3D::setAnimFuncRot(ANIM_FUNCTION animFuncRot)
353{
354  switch (animFuncRot)
355    {
[3986]356    default:
[3973]357    case ANIM_CONSTANT:
358      this->animFuncRot = &Animation3D::rConstant;
359      break;
360    case ANIM_LINEAR:
361      this->animFuncRot = &Animation3D::rLinear;
362      break;
363    case ANIM_SINE:
364      this->animFuncRot = &Animation3D::rSine;
365      break;
366    case ANIM_COSINE:
367      this->animFuncRot = &Animation3D::rCosine;
368      break;
369    case ANIM_EXP:
370      this->animFuncRot = &Animation3D::rExp;
371      break;
372    case ANIM_NEG_EXP:
373      this->animFuncRot = &Animation3D::rNegExp;
[3977]374      this->expFactorRot = -1.0 / this->currentKeyFrame->duration * logf(DELTA_X_3D);
[3973]375      break;
376    case ANIM_QUADRATIC:
377      this->animFuncRot = &Animation3D::rQuadratic;
378      break;
379    case ANIM_RANDOM:
380      this->animFuncRot = &Animation3D::rRandom;
381      break;
[3986]382
[3973]383    }
384}
385
386
387/**
388   \brief stays at the value of the currentKeyFrame
389   \param timePassed The time passed since this Keyframe began
390*/
391void Animation3D::rConstant(float timePassed) const
392{
[3977]393  this->object->setRelDir(this->currentKeyFrame->direction);
[3973]394}
395
396/**
397   \brief linear interpolation between this keyframe and the next one
398   \param timePassed The time passed since this Keyframe began
399
400   \todo implement also do this for direction
401*/
402void Animation3D::rLinear(float timePassed) const
403{
404  this->object->setRelDir(quatSlerp( this->nextKeyFrame->direction, 
405                                     this->currentKeyFrame->direction, 
406                                     timePassed/this->currentKeyFrame->duration) );
407}
408
409/**
410   \brief a Sinusodial Interpolation between this keyframe and the next one
411   \param timePassed The time passed since this Keyframe began
412
413   \todo implement
414*/
415void Animation3D::rSine(float timePassed) const
416{
[3975]417  float scale;
418  if( timePassed < this->currentKeyFrame->duration / 2.0)
419    scale = sin( M_PI * timePassed / this->currentKeyFrame->duration);
420  else
421    scale = 1.0 - sin( M_PI * timePassed / this->currentKeyFrame->duration);
422
423  this->object->setRelDir(quatSlerp( this->nextKeyFrame->direction, 
424                                     this->currentKeyFrame->direction, 
425                                     scale) );
[3973]426}
427
428
429/**
430   \brief a cosine interpolation between this keyframe and the next one
431   \param timePassed The time passed since this Keyframe began
432
433   \todo implement
434*/
435void Animation3D::rCosine(float timePassed) const
436{
[3976]437  float scale = cos(M_PI * timePassed / this->currentKeyFrame->duration);
438  this->object->setRelDir(quatSlerp( this->nextKeyFrame->direction, 
439                                     this->currentKeyFrame->direction, 
440                                     scale) );
[3973]441}
442
443
444
445/**
446   \brief an exponential interpolation between this keyframe and the next one
447   \param timePassed The time passed since this Keyframe began
448*/
449void Animation3D::rExp(float timePassed) const
450{
[3980]451  PRINTF(0)("exp rotation function not implemented\n");
[3973]452}
453
454/**
455   \brief a negative exponential interpolation between this keyframe and the next one
456   \param timePassed The time passed since this Keyframe began
457*/
458void Animation3D::rNegExp(float timePassed) const
459{
[3977]460  float scale = (1.0 - expf(- timePassed * expFactorRot));
461  this->object->setRelDir(quatSlerp( this->nextKeyFrame->direction, 
462                                     this->currentKeyFrame->direction, 
463                                     scale) );
[3973]464}
465
466
467/**
468   \brief a quadratic interpolation between this keyframe and the next one
469   \param timePassed The time passed since this Keyframe began
470
471   \todo implement
472*/
473void Animation3D::rQuadratic(float timePassed) const
474{
[3980]475  PRINTF(0)("quadratic rotation alg not implemented\n");
[3973]476}
477
478/**
479   \brief some random animation (fluctuating)
480   \param timePassed The time passed since this Keyframe began
481*/
482void Animation3D::rRandom(float timePassed) const
483{
[3980]484  PRINTF(0)("random rotation alg not implemented\n");
[3973]485}
Note: See TracBrowser for help on using the repository browser.