Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3754 was 3752, checked in by patrick, 20 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
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  singletonRef = NULL;
76}
77
78
79/**
80   \brief this determines the start of an Animator Describtion
81
82   this can then be followed by different commands like addKeyFrame(..) etc. and
83   will be closed with AnimatiorEnd()
84*/
85void SimpleAnimation::animatorBegin()
86{
87  this->bDescriptive = true;
88}
89
90
91/**
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*/
97void SimpleAnimation::animatorEnd()
98{
99  this->workingObject = NULL;
100  this->workingAnimator = NULL;
101  this->bDescriptive = false;
102}
103
104
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
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{
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();
128      anim->frames = new tList<KeyFrame>();
129      anim->animMode = LOOP;
130      bRunning = false;
131      deltaT = 0.0;
132      this->animators->add(anim);
133    }
134  this->workingAnimator = anim;
135}
136
137
138
139/**
140   \brief adds a keyframe with properties
141   \param the point of the object
142   \param and the direction of it
143   \param at this time
144*/
145void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time)
146{
147  if( !this->bDescriptive || this->workingAnimator == NULL)
148    {
149      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
150      return;
151    }
152  KeyFrame* frame = new KeyFrame;
153  frame->position = point;
154  frame->direction = direction;
155  frame->time = time;
156  frame->mode = DEFAULT_ANIMATION_MODE;
157  frame->object = this->workingAnimator->object;
158  this->workingAnimator->frames->add(frame);
159}
160
161
162/**
163   \brief adds a keyframe with properties
164   \param the point of the object
165   \param and the direction of it
166   \param at this time
167   \param function of the velocity of the movement
168*/
169void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time, movementMode mode)
170{
171  if( !this->bDescriptive || this->workingAnimator == NULL)
172    {
173      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
174      return;
175    }
176  KeyFrame* frame = new KeyFrame;
177  frame->position = point;
178  frame->direction = direction;
179  frame->time = time;
180  frame->mode = mode;
181  frame->object = this->workingAnimator->object;
182  this->workingAnimator->frames->add(frame);
183}
184
185/**
186   \brief adds a already defined keyframe
187   \param the keyframe to add
188*/
189void SimpleAnimation::addKeyFrame(KeyFrame* frame)
190{
191  printf("SimpleAnimation::addKeyFrame() - adding frame\n");
192  if( !this->bDescriptive || this->workingAnimator == NULL)
193    {
194      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
195      return;
196    }
197  frame->object = this->workingAnimator->object;
198  this->workingAnimator->frames->add(frame);
199  printf("SimpleAnimation::addKeyFrame() - addition completed\n");
200}
201
202
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
213/**
214   \brief clear the list of keyframes, deleting all keyframes included
215*/
216void SimpleAnimation::reset()
217{
218  /*
219  tIterator<KeyFrame>* iterator = this->frames->getIterator();
220  KeyFrame* frame = iterator->nextElement();
221  while( frame != NULL)
222    {
223      delete frame;
224      frame = iterator->nextElement();
225    }
226  delete iterator;
227  delete this->frames;
228
229  this->frames = new tList<KeyFrame>();
230  this->localTime = 0;
231  this->bRunning = false;
232
233  this->currentFrame = NULL;
234  this->lastFrame = NULL;
235  */
236}
237
238/**
239   \brief starts the animation, therefore listens to tick signals
240*/
241void SimpleAnimation::start()
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 
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);
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  */
270}
271
272
273/**
274   \brief stops the animation, immune to tick signals
275*/
276void SimpleAnimation::stop()
277{
278  this->bRunning = false;
279}
280
281/**
282   \brief stops and then starts the animation from begining
283*/
284void SimpleAnimation::restart()
285{
286  this->localTime = 0;
287  //this->lastFrame = this->frames->firstElement();
288  //this->currentFrame = this->frames->nextElement(this->currentFrame);
289  this->bRunning = true;
290}
291
292/**
293   \brief pauses the animation until resumed
294*/
295void SimpleAnimation::pause()
296{
297  this->bRunning = false;
298}
299
300/**
301   \brief resumes a pause, if not paused, no effect
302*/
303void SimpleAnimation::resume()
304{
305  this->bRunning = true;
306}
307
308
309/**
310   \brief heart beat, next animation step
311*/
312void SimpleAnimation::tick(float time)
313{
314  tIterator<Animation>* iterator = this->animators->getIterator();
315  Animation* anim = iterator->nextElement();
316  while( anim != NULL)
317    {
318      if( anim->bRunning)
319        { 
320          anim->localTime += time;
321          /* first get the current frame via time-stamps */ 
322          while( anim->localTime > anim->currentFrame->time)
323            {
324              printf("SimpleAnimation::tick(...) - changing Frame\n");
325             
326              anim->localTime -= anim->currentFrame->time;
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);
332              if( anim->currentFrame == anim->frames->firstElement() && anim->animMode == SINGLE)
333                {
334                  anim->bRunning = false;
335                  return;
336                }
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;
350              *anim->tmpVect = *anim->tmpVect * anim->localTime / anim->currentFrame->time;
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;
359              *anim->tmpVect = *anim->tmpVect * (1 - exp(- anim->localTime * anim->deltaT));     
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;
365              *anim->tmpVect = *anim->tmpVect * 0.5*(1 - cos(M_PI * anim->localTime / anim->currentFrame->time));     
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;
374              *anim->tmpVect = *anim->tmpVect * 1/3 * ldexpf(anim->localTime, 3);
375              break;
376            default:
377              break;
378            }
379        }
380      anim = iterator->nextElement();
381    }
382  delete anim;
383}
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.