Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/simple_animation.cc @ 3866

Last change on this file since 3866 was 3851, checked in by bensch, 19 years ago

orxonox/trunk: animation: more adaptions to the new Framework

File size: 10.1 KB
RevLine 
[3573]1
[3851]2/*
[3573]3   orxonox - the future of 3D-vertical-scrollers
4
5   Copyright (C) 2004 orx
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2, or (at your option)
10   any later version.
11
12   ### File Specific:
13   main-programmer: Patrick Boenzli
[3851]14   co-programmer: Benjamin Grauer
15
16   2005-04-17: Benjamin Grauer
17          Rewritte all functions, so it will fit into the Animation-class
[3573]18*/
19
20
21#include "simple_animation.h"
22
23
[3727]24SimpleAnimation* SimpleAnimation::singletonRef = 0;
[3573]25/**
[3727]26   \brief gets the singleton instance
27   \returns singleton instance
28*/
29SimpleAnimation* SimpleAnimation::getInstance()
30{
31  if( singletonRef == NULL)
32    singletonRef = new SimpleAnimation();
33  return singletonRef;
34}
35
36/**
[3573]37   \brief standard constructor
38*/
[3727]39SimpleAnimation::SimpleAnimation () 
[3573]40{
41   this->setClassName ("SimpleAnimation");
[3848]42   this->frames = new tList<KeyFrame3D>();
[3847]43   this->animators = new tList<Animation3D>();
[3573]44   this->localTime = 0;
[3719]45   this->bRunning = false;
46   this->currentFrame = NULL;
47   this->lastFrame = NULL;
[3720]48
49   this->tmpVect = new Vector();
[3729]50   this->lastPosition = new Vector();
[3733]51   this->deltaT = 0.2;
[3573]52}
53
54
55/**
56   \brief standard deconstructor
57
58*/
59SimpleAnimation::~SimpleAnimation () 
60{
[3848]61  tIterator<KeyFrame3D>* iterator = this->frames->getIterator();
62  KeyFrame3D* frame = iterator->nextElement(); 
[3573]63  while( frame != NULL) 
64    { 
65      delete frame;
[3661]66      frame = iterator->nextElement();
[3573]67    }
[3661]68  delete iterator;
[3573]69  delete this->frames;
[3752]70  singletonRef = NULL;
[3573]71}
72
73
[3727]74/**
75   \brief this determines the start of an Animator Describtion
[3573]76
[3727]77   this can then be followed by different commands like addKeyFrame(..) etc. and
78   will be closed with AnimatiorEnd()
79*/
[3729]80void SimpleAnimation::animatorBegin()
[3727]81{
82  this->bDescriptive = true;
83}
84
85
[3573]86/**
[3727]87   \brief this determines the end of an Animator Describtion
88
89   this can then be followed by different commands like addKeyFrame(..) etc. and
90   will be closed with AnimatiorEnd()
91*/
[3729]92void SimpleAnimation::animatorEnd()
[3727]93{
94  this->workingObject = NULL;
[3743]95  this->workingAnimator = NULL;
[3727]96  this->bDescriptive = false;
97}
98
99
[3739]100/*
101  Vector* lastPosition;
102  Vector* tmpVect;
[3848]103  tList<KeyFrame3D>* frames;
[3739]104  animationMode animMode;
105  movementMode movMode;
106  bool bRunning;
107  float deltaT;
108*/
109
[3727]110/**
111   \brief select an object to work on by using this function
112   \param object wo work on
113*/
[3851]114void SimpleAnimation::selectObject(PNode* entity)
[3727]115{
[3851]116  Animation3D* anim = getAnimationFromPNode(entity);
[3739]117  if( anim == NULL)
118    {
[3847]119      anim = new Animation3D;
[3739]120      anim->object = entity;
121      anim->lastPosition = new Vector();
122      anim->tmpVect = new Vector();
[3848]123      anim->frames = new tList<KeyFrame3D>();
[3752]124      anim->animMode = LOOP;
[3755]125      anim->bRunning = false;
[3739]126      deltaT = 0.0;
127      this->animators->add(anim);
128    }
129  this->workingAnimator = anim;
[3727]130}
131
132
133
134/**
[3573]135   \brief adds a keyframe with properties
136   \param the point of the object
[3729]137   \param and the direction of it
[3573]138   \param at this time
139*/
[3729]140void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time)
[3573]141{
[3743]142  if( !this->bDescriptive || this->workingAnimator == NULL)
[3727]143    {
144      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
145      return;
146    }
[3848]147  KeyFrame3D* frame = new KeyFrame3D;
[3726]148  frame->position = point;
[3729]149  frame->direction = direction;
[3726]150  frame->time = time;
151  frame->mode = DEFAULT_ANIMATION_MODE;
[3743]152  frame->object = this->workingAnimator->object;
153  this->workingAnimator->frames->add(frame);
[3573]154}
155
156
157/**
158   \brief adds a keyframe with properties
159   \param the point of the object
[3729]160   \param and the direction of it
[3573]161   \param at this time
162   \param function of the velocity of the movement
163*/
[3729]164void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time, movementMode mode)
[3573]165{
[3743]166  if( !this->bDescriptive || this->workingAnimator == NULL)
[3727]167    {
168      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
169      return;
170    }
[3848]171  KeyFrame3D* frame = new KeyFrame3D;
[3726]172  frame->position = point;
[3729]173  frame->direction = direction;
[3726]174  frame->time = time;
175  frame->mode = mode;
[3743]176  frame->object = this->workingAnimator->object;
177  this->workingAnimator->frames->add(frame);
[3573]178}
179
180/**
181   \brief adds a already defined keyframe
182   \param the keyframe to add
183*/
[3848]184void SimpleAnimation::addKeyFrame(KeyFrame3D* frame)
[3573]185{
[3743]186  if( !this->bDescriptive || this->workingAnimator == NULL)
[3727]187    {
188      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
189      return;
190    }
[3743]191  frame->object = this->workingAnimator->object;
192  this->workingAnimator->frames->add(frame);
[3573]193}
194
195
[3752]196void SimpleAnimation::setAnimationMode(animationMode mode)
197{
198  if( !this->bDescriptive || this->workingAnimator == NULL)
199    {
200      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
201      return;
202    }
203  this->workingAnimator->animMode = mode;
204}
205
[3573]206/**
207   \brief clear the list of keyframes, deleting all keyframes included
208*/
209void SimpleAnimation::reset()
210{
[3743]211  /*
[3848]212  tIterator<KeyFrame3D>* iterator = this->frames->getIterator();
213  KeyFrame3D* 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
[3848]222  this->frames = new tList<KeyFrame3D>();
[3573]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*/
234void 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*/
269void SimpleAnimation::stop()
[3719]270{
271  this->bRunning = false;
272}
[3573]273
274/**
275   \brief stops and then starts the animation from begining
276*/
277void 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*/
288void SimpleAnimation::pause()
289{
[3719]290  this->bRunning = false;
[3573]291}
292
293/**
294   \brief resumes a pause, if not paused, no effect
295*/
296void SimpleAnimation::resume()
297{
[3719]298  this->bRunning = true;
[3573]299}
300
301
302/**
303   \brief heart beat, next animation step
304*/
305void SimpleAnimation::tick(float time)
306{
[3847]307  tIterator<Animation3D>* iterator = this->animators->getIterator();
308  Animation3D* anim = iterator->nextElement();
[3738]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            {
[3755]317              PRINTF(4)("SimpleAnimation::tick(...) - changing Frame\n");
[3738]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);
[3752]325              if( anim->currentFrame == anim->frames->firstElement() && anim->animMode == SINGLE)
326                {
327                  anim->bRunning = false;
328                  return;
329                }
[3738]330              anim->movMode = anim->currentFrame->mode;
331              if( anim->movMode == NEG_EXP)
332                {
333                  *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
334                  anim->deltaT = 1/anim->currentFrame->time * logf(1.0 + 600.0/anim->tmpVect->len());
335                }
336            }
337         
338          /* now animate it */
339          switch( anim->movMode)
340            {
341            case LINEAR:
342              *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
[3744]343              *anim->tmpVect = *anim->tmpVect * anim->localTime / anim->currentFrame->time;
[3738]344              anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect);
345              *anim->lastPosition = *anim->tmpVect;
346              break;
347            case EXP:
348             
349              break;
350            case NEG_EXP:
351              *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
[3832]352              *anim->tmpVect = *anim->tmpVect * (1 - expf(- anim->localTime * anim->deltaT));     
[3738]353              anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect);
354              *anim->lastPosition = *anim->tmpVect;
355              break;
356            case SIN:
357              *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
[3744]358              *anim->tmpVect = *anim->tmpVect * 0.5*(1 - cos(M_PI * anim->localTime / anim->currentFrame->time));     
[3738]359              anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect);
360              *anim->lastPosition = *anim->tmpVect;
361              break;
362            case COS:
363             
364              break;
365            case QUADRATIC:
366              *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
[3744]367              *anim->tmpVect = *anim->tmpVect * 1/3 * ldexpf(anim->localTime, 3);
[3738]368              break;
369            default:
370              break;
371            }
[3733]372        }
[3738]373      anim = iterator->nextElement();
[3719]374    }
[3738]375  delete anim;
[3573]376}
[3739]377
378
379
[3851]380Animation3D* SimpleAnimation::getAnimationFromPNode(PNode* entity)
[3739]381{
[3847]382  tIterator<Animation3D>* iterator = this->animators->getIterator();
383  Animation3D* anim = iterator->nextElement();
[3739]384  while( anim != NULL)
385    {
386      if( anim->object == entity)
387        return anim;
388      anim = iterator->nextElement();
389    }
390  delete iterator;
391  return NULL;
392}
Note: See TracBrowser for help on using the repository browser.