Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3752 was 3752, checked in by patrick, 19 years ago

orxonox/trunk: weapon now works perfectly: it shoots and moves to it. now i will have to change the projectile model itself….

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