Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/levelloader/src/simple_animation.cc @ 3825

Last change on this file since 3825 was 3746, checked in by chris, 20 years ago

orxonox/branches/levelloader: Merged trunk into branch… still not working though…

File size: 8.7 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->bDescriptive = false;
100}
101
102
103/*
104  Vector* lastPosition;
105  Vector* tmpVect;
106  tList<KeyFrame>* frames;
107  animationMode animMode;
108  movementMode movMode;
109  bool bRunning;
110  float deltaT;
111*/
112
113/**
114   \brief select an object to work on by using this function
115   \param object wo work on
116*/
117void SimpleAnimation::selectObject(WorldEntity* entity)
118{
119  Animation* anim = getAnimationFromWorldEntity(entity);
120  if( anim == NULL)
121    {
122      printf("SimpleAnimation::selectObject() - object not found, creating one\n");
123      anim = new Animation;
124      anim->object = entity;
125      anim->lastPosition = new Vector();
126      anim->tmpVect = new Vector();
127      frames = new tList<KeyFrame>();
128      bRunning = false;
129      deltaT = 0.0;
130      this->animators->add(anim);
131    }
132  this->workingAnimator = anim;
133}
134
135
136
137/**
138   \brief adds a keyframe with properties
139   \param the point of the object
140   \param and the direction of it
141   \param at this time
142*/
143void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time)
144{
145  if( !this->bDescriptive)
146    {
147      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
148      return;
149    }
150  KeyFrame* frame = new KeyFrame;
151  frame->position = point;
152  frame->direction = direction;
153  frame->time = time;
154  frame->mode = DEFAULT_ANIMATION_MODE;
155  frame->object = this->workingObject;
156  this->frames->add(frame);
157}
158
159
160/**
161   \brief adds a keyframe with properties
162   \param the point of the object
163   \param and the direction of it
164   \param at this time
165   \param function of the velocity of the movement
166*/
167void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time, movementMode mode)
168{
169  if( !this->bDescriptive)
170    {
171      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
172      return;
173    }
174  KeyFrame* frame = new KeyFrame;
175  frame->position = point;
176  frame->direction = direction;
177  frame->time = time;
178  frame->mode = mode;
179  frame->object = this->workingObject;
180  this->frames->add(frame);
181}
182
183/**
184   \brief adds a already defined keyframe
185   \param the keyframe to add
186*/
187void SimpleAnimation::addKeyFrame(KeyFrame* frame)
188{
189  if( !this->bDescriptive)
190    {
191      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
192      return;
193    }
194  frame->object = this->workingObject;
195  this->frames->add(frame);
196}
197
198
199/**
200   \brief clear the list of keyframes, deleting all keyframes included
201*/
202void SimpleAnimation::reset()
203{
204  tIterator<KeyFrame>* iterator = this->frames->getIterator();
205  KeyFrame* frame = iterator->nextElement(); 
206  while( frame != NULL) 
207    { 
208      delete frame;
209      frame = iterator->nextElement();
210    }
211  delete iterator;
212  delete this->frames;
213
214  this->frames = new tList<KeyFrame>();
215  this->localTime = 0;
216  this->bRunning = false;
217
218  this->currentFrame = NULL;
219  this->lastFrame = NULL;
220}
221
222/**
223   \brief starts the animation, therefore listens to tick signals
224*/
225void SimpleAnimation::start()
226{
227  if( this->bRunning)
228    {
229      PRINTF(2)("SimpleAnimatin is already running. You are trying to start it again.\n");
230    return;
231    }
232 
233  this->localTime = 0;
234  this->lastFrame = this->frames->firstElement();
235  this->currentFrame = this->frames->nextElement(this->currentFrame);
236  this->bRunning = true;
237}
238
239
240/**
241   \brief stops the animation, immune to tick signals
242*/
243void SimpleAnimation::stop()
244{
245  this->bRunning = false;
246}
247
248/**
249   \brief stops and then starts the animation from begining
250*/
251void SimpleAnimation::restart()
252{
253  this->localTime = 0;
254  this->lastFrame = this->frames->firstElement();
255  this->currentFrame = this->frames->nextElement(this->currentFrame);
256  this->bRunning = true;
257}
258
259/**
260   \brief pauses the animation until resumed
261*/
262void SimpleAnimation::pause()
263{
264  this->bRunning = false;
265}
266
267/**
268   \brief resumes a pause, if not paused, no effect
269*/
270void SimpleAnimation::resume()
271{
272  this->bRunning = true;
273}
274
275
276/**
277   \brief heart beat, next animation step
278*/
279void SimpleAnimation::tick(float time)
280{
281  this->localTime += time;
282  tIterator<Animation>* iterator = this->animators->getIterator();
283  Animation* anim = iterator->nextElement();
284  while( anim != NULL)
285    {
286      if( anim->bRunning)
287        {
288         
289         
290          /* first get the current frame via time-stamps */ 
291          while( this->localTime > anim->currentFrame->time)
292            {
293              printf("SimpleAnimation::tick(...) - changing Frame\n");
294             
295              //this->currentFrame->object->setRelCoor(*this->currentFrame->position);
296              *anim->lastPosition = *anim->currentFrame->position;
297             
298              anim->lastFrame = anim->currentFrame;
299              anim->currentFrame = anim->frames->nextElement(anim->currentFrame);
300              anim->movMode = anim->currentFrame->mode;
301              if( anim->movMode == NEG_EXP)
302                {
303                  *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
304                  anim->deltaT = 1/anim->currentFrame->time * logf(1.0 + 600.0/anim->tmpVect->len());
305                }
306            }
307         
308          /* now animate it */
309          switch( anim->movMode)
310            {
311            case LINEAR:
312             
313              *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
314              *anim->tmpVect = *anim->tmpVect * this->localTime / anim->currentFrame->time;
315              anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect);
316              *anim->lastPosition = *anim->tmpVect;
317              break;
318            case EXP:
319             
320              break;
321            case NEG_EXP:
322              *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
323              *anim->tmpVect = *anim->tmpVect * (1 - exp(- this->localTime * anim->deltaT));     
324              anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect);
325              *anim->lastPosition = *anim->tmpVect;
326              break;
327            case SIN:
328              *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
329              *anim->tmpVect = *anim->tmpVect * 0.5*(1 - cos(M_PI * this->localTime / anim->currentFrame->time));     
330              anim->currentFrame->object->setRelCoor(*anim->lastFrame->position + *anim->tmpVect);
331              *anim->lastPosition = *anim->tmpVect;
332              break;
333            case COS:
334             
335              break;
336            case QUADRATIC:
337              *anim->tmpVect = *anim->currentFrame->position - *anim->lastFrame->position;
338              *anim->tmpVect = *anim->tmpVect * 1/3 * ldexpf(this->localTime, 3);
339              break;
340            default:
341              break;
342            }
343        }
344      anim = iterator->nextElement();
345    }
346  delete anim;
347}
348
349
350
351Animation* SimpleAnimation::getAnimationFromWorldEntity(WorldEntity* entity)
352{
353  tIterator<Animation>* iterator = this->animators->getIterator();
354  Animation* anim = iterator->nextElement();
355  while( anim != NULL)
356    {
357      if( anim->object == entity)
358        return anim;
359      anim = iterator->nextElement();
360    }
361  delete iterator;
362  return NULL;
363}
Note: See TracBrowser for help on using the repository browser.