Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: SimpleAnimation - work is progressing

File size: 9.6 KB
Line 
1
2
3
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"
22#include "vector.h"
23#include "world_entity.h"
24
25using namespace std;
26
27
28
29SimpleAnimation* SimpleAnimation::singletonRef = 0;
30/**
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/**
42   \brief standard constructor
43*/
44SimpleAnimation::SimpleAnimation () 
45{
46   this->setClassName ("SimpleAnimation");
47   this->frames = new tList<KeyFrame>();
48   this->animators = new tList<Animation>();
49   this->localTime = 0;
50   this->bRunning = false;
51   this->currentFrame = NULL;
52   this->lastFrame = NULL;
53
54   this->tmpVect = new Vector();
55   this->lastPosition = new Vector();
56   this->deltaT = 0.2;
57}
58
59
60/**
61   \brief standard deconstructor
62
63*/
64SimpleAnimation::~SimpleAnimation () 
65{
66  tIterator<KeyFrame>* iterator = this->frames->getIterator();
67  KeyFrame* frame = iterator->nextElement(); 
68  while( frame != NULL) 
69    { 
70      delete frame;
71      frame = iterator->nextElement();
72    }
73  delete iterator;
74  delete this->frames;
75}
76
77
78/**
79   \brief this determines the start of an Animator Describtion
80
81   this can then be followed by different commands like addKeyFrame(..) etc. and
82   will be closed with AnimatiorEnd()
83*/
84void SimpleAnimation::animatorBegin()
85{
86  this->bDescriptive = true;
87}
88
89
90/**
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*/
96void SimpleAnimation::animatorEnd()
97{
98  this->workingObject = NULL;
99  this->workingAnimator = NULL;
100  this->bDescriptive = false;
101}
102
103
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
114/**
115   \brief select an object to work on by using this function
116   \param object wo work on
117*/
118void SimpleAnimation::selectObject(WorldEntity* entity)
119{
120  printf("SimpleAnimation::selectObject() - selecing active object\n");
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();
129      anim->frames = new tList<KeyFrame>();
130      bRunning = false;
131      deltaT = 0.0;
132      this->animators->add(anim);
133    }
134  else 
135    printf("SimpleAnimation::selectObject() - animation already existent, using it\n");
136  this->workingAnimator = anim;
137  printf("SimpleAnimation::selectObject() - selection completed\n");
138}
139
140
141
142/**
143   \brief adds a keyframe with properties
144   \param the point of the object
145   \param and the direction of it
146   \param at this time
147*/
148void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time)
149{
150  if( !this->bDescriptive || this->workingAnimator == NULL)
151    {
152      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
153      return;
154    }
155  KeyFrame* frame = new KeyFrame;
156  frame->position = point;
157  frame->direction = direction;
158  frame->time = time;
159  frame->mode = DEFAULT_ANIMATION_MODE;
160  frame->object = this->workingAnimator->object;
161  this->workingAnimator->frames->add(frame);
162}
163
164
165/**
166   \brief adds a keyframe with properties
167   \param the point of the object
168   \param and the direction of it
169   \param at this time
170   \param function of the velocity of the movement
171*/
172void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time, movementMode mode)
173{
174  if( !this->bDescriptive || this->workingAnimator == NULL)
175    {
176      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
177      return;
178    }
179  KeyFrame* frame = new KeyFrame;
180  frame->position = point;
181  frame->direction = direction;
182  frame->time = time;
183  frame->mode = mode;
184  frame->object = this->workingAnimator->object;
185  this->workingAnimator->frames->add(frame);
186}
187
188/**
189   \brief adds a already defined keyframe
190   \param the keyframe to add
191*/
192void SimpleAnimation::addKeyFrame(KeyFrame* frame)
193{
194  printf("SimpleAnimation::addKeyFrame() - adding frame\n");
195  if( !this->bDescriptive || this->workingAnimator == NULL)
196    {
197      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
198      return;
199    }
200  frame->object = this->workingAnimator->object;
201  this->workingAnimator->frames->add(frame);
202  printf("SimpleAnimation::addKeyFrame() - addition completed\n");
203}
204
205
206/**
207   \brief clear the list of keyframes, deleting all keyframes included
208*/
209void SimpleAnimation::reset()
210{
211  /*
212  tIterator<KeyFrame>* iterator = this->frames->getIterator();
213  KeyFrame* frame = iterator->nextElement();
214  while( frame != NULL)
215    {
216      delete frame;
217      frame = iterator->nextElement();
218    }
219  delete iterator;
220  delete this->frames;
221
222  this->frames = new tList<KeyFrame>();
223  this->localTime = 0;
224  this->bRunning = false;
225
226  this->currentFrame = NULL;
227  this->lastFrame = NULL;
228  */
229}
230
231/**
232   \brief starts the animation, therefore listens to tick signals
233*/
234void SimpleAnimation::start()
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 
242  this->localTime = 0;
243
244  /*
245  tIterator<Animation>* iterator = this->animators->getIterator();
246  Animation* anim = iterator->nextElement();
247  while( anim != NULL)
248    {
249      printf("SimpleAnimation::start() - initializing an animaion\n");
250      anim->currentFrame = anim->frames->firstElement();
251      anim->lastFrame = anim->frames->nextElement(anim->currentFrame);
252      anim = iterator->nextElement();
253    }
254  */
255
256  this->bRunning = true;
257}
258
259
260/**
261   \brief stops the animation, immune to tick signals
262*/
263void SimpleAnimation::stop()
264{
265  this->bRunning = false;
266}
267
268/**
269   \brief stops and then starts the animation from begining
270*/
271void SimpleAnimation::restart()
272{
273  this->localTime = 0;
274  //this->lastFrame = this->frames->firstElement();
275  //this->currentFrame = this->frames->nextElement(this->currentFrame);
276  this->bRunning = true;
277}
278
279/**
280   \brief pauses the animation until resumed
281*/
282void SimpleAnimation::pause()
283{
284  this->bRunning = false;
285}
286
287/**
288   \brief resumes a pause, if not paused, no effect
289*/
290void SimpleAnimation::resume()
291{
292  this->bRunning = true;
293}
294
295
296/**
297   \brief heart beat, next animation step
298*/
299void SimpleAnimation::tick(float time)
300{
301  this->localTime += time;
302  tIterator<Animation>* iterator = this->animators->getIterator();
303  Animation* anim = iterator->nextElement();
304  while( anim != NULL)
305    {
306      if( anim->bRunning)
307        { 
308         
309          /* first get the current frame via time-stamps */ 
310          while( this->localTime > anim->currentFrame->time)
311            {
312              printf("SimpleAnimation::tick(...) - changing Frame\n");
313             
314              //this->currentFrame->object->setRelCoor(*this->currentFrame->position);
315              *anim->lastPosition = *anim->currentFrame->position;
316             
317              anim->lastFrame = anim->currentFrame;
318              anim->currentFrame = anim->frames->nextElement(anim->currentFrame);
319              anim->movMode = anim->currentFrame->mode;
320              if( anim->movMode == NEG_EXP)
321                {
322                  *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
323                  anim->deltaT = 1/anim->currentFrame->time * logf(1.0 + 600.0/anim->tmpVect->len());
324                }
325            }
326         
327          /* now animate it */
328          switch( anim->movMode)
329            {
330            case LINEAR:
331             
332              *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
333              *anim->tmpVect = *anim->tmpVect * this->localTime / anim->currentFrame->time;
334              anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect);
335              *anim->lastPosition = *anim->tmpVect;
336              break;
337            case EXP:
338             
339              break;
340            case NEG_EXP:
341              *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
342              *anim->tmpVect = *anim->tmpVect * (1 - exp(- this->localTime * anim->deltaT));     
343              anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect);
344              *anim->lastPosition = *anim->tmpVect;
345              break;
346            case SIN:
347              *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
348              *anim->tmpVect = *anim->tmpVect * 0.5*(1 - cos(M_PI * this->localTime / anim->currentFrame->time));     
349              anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect);
350              *anim->lastPosition = *anim->tmpVect;
351              break;
352            case COS:
353             
354              break;
355            case QUADRATIC:
356              *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
357              *anim->tmpVect = *anim->tmpVect * 1/3 * ldexpf(this->localTime, 3);
358              break;
359            default:
360              break;
361            }
362        }
363      anim = iterator->nextElement();
364    }
365  delete anim;
366}
367
368
369
370Animation* SimpleAnimation::getAnimationFromWorldEntity(WorldEntity* entity)
371{
372  tIterator<Animation>* iterator = this->animators->getIterator();
373  Animation* anim = iterator->nextElement();
374  while( anim != NULL)
375    {
376      if( anim->object == entity)
377        return anim;
378      anim = iterator->nextElement();
379    }
380  delete iterator;
381  return NULL;
382}
Note: See TracBrowser for help on using the repository browser.