Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3858 was 3851, checked in by bensch, 20 years ago

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

File size: 10.1 KB
Line 
1
2/*
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
14   co-programmer: Benjamin Grauer
15
16   2005-04-17: Benjamin Grauer
17          Rewritte all functions, so it will fit into the Animation-class
18*/
19
20
21#include "simple_animation.h"
22
23
24SimpleAnimation* SimpleAnimation::singletonRef = 0;
25/**
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/**
37   \brief standard constructor
38*/
39SimpleAnimation::SimpleAnimation () 
40{
41   this->setClassName ("SimpleAnimation");
42   this->frames = new tList<KeyFrame3D>();
43   this->animators = new tList<Animation3D>();
44   this->localTime = 0;
45   this->bRunning = false;
46   this->currentFrame = NULL;
47   this->lastFrame = NULL;
48
49   this->tmpVect = new Vector();
50   this->lastPosition = new Vector();
51   this->deltaT = 0.2;
52}
53
54
55/**
56   \brief standard deconstructor
57
58*/
59SimpleAnimation::~SimpleAnimation () 
60{
61  tIterator<KeyFrame3D>* iterator = this->frames->getIterator();
62  KeyFrame3D* frame = iterator->nextElement(); 
63  while( frame != NULL) 
64    { 
65      delete frame;
66      frame = iterator->nextElement();
67    }
68  delete iterator;
69  delete this->frames;
70  singletonRef = NULL;
71}
72
73
74/**
75   \brief this determines the start of an Animator Describtion
76
77   this can then be followed by different commands like addKeyFrame(..) etc. and
78   will be closed with AnimatiorEnd()
79*/
80void SimpleAnimation::animatorBegin()
81{
82  this->bDescriptive = true;
83}
84
85
86/**
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*/
92void SimpleAnimation::animatorEnd()
93{
94  this->workingObject = NULL;
95  this->workingAnimator = NULL;
96  this->bDescriptive = false;
97}
98
99
100/*
101  Vector* lastPosition;
102  Vector* tmpVect;
103  tList<KeyFrame3D>* frames;
104  animationMode animMode;
105  movementMode movMode;
106  bool bRunning;
107  float deltaT;
108*/
109
110/**
111   \brief select an object to work on by using this function
112   \param object wo work on
113*/
114void SimpleAnimation::selectObject(PNode* entity)
115{
116  Animation3D* anim = getAnimationFromPNode(entity);
117  if( anim == NULL)
118    {
119      anim = new Animation3D;
120      anim->object = entity;
121      anim->lastPosition = new Vector();
122      anim->tmpVect = new Vector();
123      anim->frames = new tList<KeyFrame3D>();
124      anim->animMode = LOOP;
125      anim->bRunning = false;
126      deltaT = 0.0;
127      this->animators->add(anim);
128    }
129  this->workingAnimator = anim;
130}
131
132
133
134/**
135   \brief adds a keyframe with properties
136   \param the point of the object
137   \param and the direction of it
138   \param at this time
139*/
140void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time)
141{
142  if( !this->bDescriptive || this->workingAnimator == NULL)
143    {
144      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
145      return;
146    }
147  KeyFrame3D* frame = new KeyFrame3D;
148  frame->position = point;
149  frame->direction = direction;
150  frame->time = time;
151  frame->mode = DEFAULT_ANIMATION_MODE;
152  frame->object = this->workingAnimator->object;
153  this->workingAnimator->frames->add(frame);
154}
155
156
157/**
158   \brief adds a keyframe with properties
159   \param the point of the object
160   \param and the direction of it
161   \param at this time
162   \param function of the velocity of the movement
163*/
164void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time, movementMode mode)
165{
166  if( !this->bDescriptive || this->workingAnimator == NULL)
167    {
168      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
169      return;
170    }
171  KeyFrame3D* frame = new KeyFrame3D;
172  frame->position = point;
173  frame->direction = direction;
174  frame->time = time;
175  frame->mode = mode;
176  frame->object = this->workingAnimator->object;
177  this->workingAnimator->frames->add(frame);
178}
179
180/**
181   \brief adds a already defined keyframe
182   \param the keyframe to add
183*/
184void SimpleAnimation::addKeyFrame(KeyFrame3D* frame)
185{
186  if( !this->bDescriptive || this->workingAnimator == NULL)
187    {
188      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
189      return;
190    }
191  frame->object = this->workingAnimator->object;
192  this->workingAnimator->frames->add(frame);
193}
194
195
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
206/**
207   \brief clear the list of keyframes, deleting all keyframes included
208*/
209void SimpleAnimation::reset()
210{
211  /*
212  tIterator<KeyFrame3D>* iterator = this->frames->getIterator();
213  KeyFrame3D* 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<KeyFrame3D>();
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  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);
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  */
263}
264
265
266/**
267   \brief stops the animation, immune to tick signals
268*/
269void SimpleAnimation::stop()
270{
271  this->bRunning = false;
272}
273
274/**
275   \brief stops and then starts the animation from begining
276*/
277void SimpleAnimation::restart()
278{
279  this->localTime = 0;
280  //this->lastFrame = this->frames->firstElement();
281  //this->currentFrame = this->frames->nextElement(this->currentFrame);
282  this->bRunning = true;
283}
284
285/**
286   \brief pauses the animation until resumed
287*/
288void SimpleAnimation::pause()
289{
290  this->bRunning = false;
291}
292
293/**
294   \brief resumes a pause, if not paused, no effect
295*/
296void SimpleAnimation::resume()
297{
298  this->bRunning = true;
299}
300
301
302/**
303   \brief heart beat, next animation step
304*/
305void SimpleAnimation::tick(float time)
306{
307  tIterator<Animation3D>* iterator = this->animators->getIterator();
308  Animation3D* anim = iterator->nextElement();
309  while( anim != NULL)
310    {
311      if( anim->bRunning)
312        { 
313          anim->localTime += time;
314          /* first get the current frame via time-stamps */ 
315          while( anim->localTime > anim->currentFrame->time)
316            {
317              PRINTF(4)("SimpleAnimation::tick(...) - changing Frame\n");
318             
319              anim->localTime -= anim->currentFrame->time;
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);
325              if( anim->currentFrame == anim->frames->firstElement() && anim->animMode == SINGLE)
326                {
327                  anim->bRunning = false;
328                  return;
329                }
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;
343              *anim->tmpVect = *anim->tmpVect * anim->localTime / anim->currentFrame->time;
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;
352              *anim->tmpVect = *anim->tmpVect * (1 - expf(- anim->localTime * anim->deltaT));     
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;
358              *anim->tmpVect = *anim->tmpVect * 0.5*(1 - cos(M_PI * anim->localTime / anim->currentFrame->time));     
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;
367              *anim->tmpVect = *anim->tmpVect * 1/3 * ldexpf(anim->localTime, 3);
368              break;
369            default:
370              break;
371            }
372        }
373      anim = iterator->nextElement();
374    }
375  delete anim;
376}
377
378
379
380Animation3D* SimpleAnimation::getAnimationFromPNode(PNode* entity)
381{
382  tIterator<Animation3D>* iterator = this->animators->getIterator();
383  Animation3D* anim = iterator->nextElement();
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.