Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3849 was 3848, checked in by bensch, 20 years ago

orxonox/trunk: names of keyframes:
KeyFrame → KeyFrame3D
AnimKeyFrame → KeyFrameF (F for float)

also changed some stuff in the Animation3D-class.

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