Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/t_animation.h @ 3854

Last change on this file since 3854 was 3854, checked in by bensch, 19 years ago

orxonox/trunk: doxy-tags

File size: 9.3 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16/*!
17    \file t_animation.h
18*/
19
20#ifndef _T_ANIMATION_H
21#define _T_ANIMATION_H
22
23#include "animation.h"
24
25//! A Class to handle some animation for single floated values.
26template<class T> class tAnimation : public Animation
27{
28 public:
29  tAnimation(T* object = NULL, void (T::*funcToAnim)(float) = NULL);
30  virtual ~tAnimation();
31
32  virtual void rewind();
33
34  void setFuncToAnim(T* object, void (T::*funcToAnim)(float));
35  void addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc = ANIM_LINEAR);
36
37  virtual void tick(float dt);
38
39  // animation functions
40  void setAnimFunc(ANIM_FUNCTION animFunc);
41
42 private:
43
44  float constant(float timePassed) const;
45  float linear(float timePassed) const;
46  float sine(float timePassed) const;
47  float cosine(float timePassed) const;
48  float exp(float timePassed) const;
49  float negExp(float timePassed) const;
50  float quadratic(float timePassed) const;
51  float random(float timePassed) const;
52
53
54  //  ANIM_FUNCTION animFunc;
55  float (tAnimation<T>::*animFunc)(float) const;  //!< A Function for the AnimationType
56  KeyFrameF* currentKeyFrame;                     //!< The current KeyFrame
57  KeyFrameF* nextKeyFrame;                        //!< The KeyFrame we iterate to
58  tList<KeyFrameF>* keyFrameList;                 //!< The KeyFrameList
59
60  T* object;                                      //!< The Object from which to Animate something
61  void (T::*funcToAnim)(float);                   //!< The function to Animate
62
63  float expFactor;                                //!< some factors
64
65};
66
67
68
69/**
70   \brief standard constructor
71*/
72template<class T>
73tAnimation<T>::tAnimation (T* object, void (T::*funcToAnim)(float)) 
74{
75  // create a new List
76  this->keyFrameList = new tList<KeyFrameF>();
77  KeyFrameF* tmpKeyFrame = new KeyFrameF;
78  tmpKeyFrame->value = 0.0;
79  tmpKeyFrame->duration = 1.0;
80  keyFrameList->add(tmpKeyFrame);
81
82  this->currentKeyFrame = tmpKeyFrame;
83  this->nextKeyFrame = tmpKeyFrame;
84
85  this->animFunc = &tAnimation<T>::linear;
86
87  this->setFuncToAnim(object, funcToAnim);
88}
89
90
91/**
92   \brief standard deconstructor
93   
94   deletes all the Keyframes
95*/
96template<class T>
97tAnimation<T>::~tAnimation () 
98{
99  // delete all the KeyFrames
100  tIterator<KeyFrameF>* itKF = keyFrameList->getIterator();
101  KeyFrameF*  enumKF = itKF->nextElement();
102  while (enumKF)
103    {
104      delete enumKF;
105      enumKF = itKF->nextElement();
106    }
107  delete itKF;
108  delete this->keyFrameList;
109}
110
111/**
112   \brief rewinds the Animation to the beginning (first KeyFrame and time == 0)
113*/
114template<class T>
115void tAnimation<T>::rewind(void)
116{
117  this->currentKeyFrame = keyFrameList->firstElement();
118  this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement());
119  this->localTime = 0.0;
120}
121
122/**
123   \brief sets the Function we want to animate
124   \param object from what object do we want to animate
125   \param funcToAnim which function
126*/
127template<class T>
128void tAnimation<T>::setFuncToAnim(T* object, void (T::*funcToAnim)(float))
129{
130  this->baseObject = this->object = object;
131  this->funcToAnim = funcToAnim;
132}
133
134/**
135   \brief Appends a new Keyframe
136   \param value the value of the new KeyFrame
137   \param duration The duration from the new KeyFrame to the next one
138   \param animFunc The function to animate between this keyFrame and the next one
139*/
140template<class T>
141void tAnimation<T>::addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc)
142{
143  // some small check
144  if (duration <= 0.0)
145    duration = 1.0;
146
147  KeyFrameF* tmpKeyFrame;
148   
149  if (bHasKeys)
150    {
151      tmpKeyFrame = new KeyFrameF;
152      if (this->currentKeyFrame == this->nextKeyFrame)
153        this->nextKeyFrame = tmpKeyFrame;
154      this->keyFrameList->add(tmpKeyFrame);
155
156    }
157  else
158    {
159      tmpKeyFrame = this->keyFrameList->firstElement();
160      bHasKeys = true;
161      this->setAnimFunc(animFunc);
162    }
163  tmpKeyFrame->value = value;
164  tmpKeyFrame->duration = duration;
165  tmpKeyFrame->animFunc = animFunc;
166}
167
168/**
169   \brief ticks the Animation
170   \param dt how much time to tick
171*/
172template<class T>
173void tAnimation<T>::tick(float dt)
174{
175  if (this->bRunning)
176    {
177      this->localTime += dt;
178      if (localTime >= this->currentKeyFrame->duration)
179        {
180          // switching to the next Key-Frame
181          this->localTime -= this->currentKeyFrame->duration;
182
183          this->currentKeyFrame = this->nextKeyFrame;
184          // checking, if we should still Play the animation
185          if (this->currentKeyFrame == this->keyFrameList->lastElement())
186            {
187              switch (this->postInfinity)
188                {
189                case ANIM_INF_CONSTANT:
190                  this->localTime = 0.0;
191                  this->bRunning = false;
192                  break;
193                case ANIM_INF_REWIND:
194                  break;
195                }
196            }
197          this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame);
198          printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value);
199          this->setAnimFunc(this->currentKeyFrame->animFunc);     
200        }
201     
202      (this->object->*(funcToAnim))((this->*animFunc)(this->localTime));
203    }
204}
205
206/**
207   \brief Sets The kind of Animation between this keyframe and the next one
208   \param animFunc The Type of Animation to set
209*/
210template<class T>
211void tAnimation<T>::setAnimFunc(ANIM_FUNCTION animFunc)
212{
213  switch (animFunc)
214    {
215    default:
216    case ANIM_CONSTANT:
217      this->animFunc = &tAnimation<T>::constant;
218      break;
219    case ANIM_LINEAR:
220      this->animFunc = &tAnimation<T>::linear;
221      break;
222    case ANIM_SINE:
223      this->animFunc = &tAnimation<T>::sine;
224      break;
225    case ANIM_COSINE:
226      this->animFunc = &tAnimation<T>::cosine;
227      break;
228    case ANIM_EXP:
229      this->animFunc = &tAnimation<T>::exp;
230      break;
231    case ANIM_NEG_EXP:
232      {
233        this->animFunc = &tAnimation<T>::negExp;
234        float d = fabs(this->currentKeyFrame->value - this->nextKeyFrame->value);
235        expFactor =  - 1.0 / this->currentKeyFrame->duration * logf(DELTA_X);
236        break;
237      }
238    case ANIM_QUADRATIC:
239      this->animFunc = &tAnimation<T>::quadratic;
240      break;
241    case ANIM_RANDOM:
242      this->animFunc = &tAnimation<T>::random;
243      break;
244    }
245}
246
247
248// animation functions
249
250/**
251   \brief some random animation (fluctuating)
252   \param timePassed The time passed since this Keyframe began
253*/
254template<class T>
255float tAnimation<T>::random(float timePassed) const
256{
257  return this->currentKeyFrame->value * (float)rand()/(float)RAND_MAX;
258}
259
260/**
261   \brief stays at the value of the currentKeyFrame
262   \param timePassed The time passed since this Keyframe began
263*/
264template<class T>
265float tAnimation<T>::constant(float timePassed) const
266{
267  return this->currentKeyFrame->value;
268}
269
270/**
271   \brief linear interpolation between this keyframe and the next one
272   \param timePassed The time passed since this Keyframe began
273*/
274template<class T>
275float tAnimation<T>::linear(float timePassed) const 
276{
277  return this->currentKeyFrame->value + (this->nextKeyFrame->value - this->currentKeyFrame->value) 
278    * (timePassed / this->currentKeyFrame->duration);
279  //  PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame);
280  //  return val;
281}
282
283/**
284   \brief a Sinusodial Interpolation between this keyframe and the next one
285   \param timePassed The time passed since this Keyframe began
286*/
287template<class T>
288float tAnimation<T>::sine(float timePassed) const
289{
290  float d = this->currentKeyFrame->value - this->nextKeyFrame->value;
291  float e = 0.5 * d * (1 - cos(M_PI * timePassed / this->currentKeyFrame->duration));
292  return this->currentKeyFrame->value - e;
293  /*
294  return his->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value)
295    * sin(timePassed / this->currentKeyFrame->duration * M_PI);
296  */
297}
298
299/**
300   \brief a cosine interpolation between this keyframe and the next one
301   \param timePassed The time passed since this Keyframe began
302*/
303template<class T>
304float tAnimation<T>::cosine(float timePassed) const
305{
306  float d = this->currentKeyFrame->value - this->nextKeyFrame->value;
307  float e = 0.5 * d * (sin(M_PI * timePassed / this->currentKeyFrame->duration));
308  if( timePassed > 0.5*this->currentKeyFrame->duration) e = (d - e);
309  return this->currentKeyFrame->value - e;
310  /*
311  return this->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value)
312    * cos(timePassed / this->currentKeyFrame->duration * M_PI);
313  */
314}
315
316/**
317   \brief an exponential interpolation between this keyframe and the next one
318   \param timePassed The time passed since this Keyframe began
319*/
320template<class T>
321float tAnimation<T>::exp(float timePassed) const
322{
323}
324
325/**
326   \brief a negative exponential interpolation between this keyframe and the next one
327   \param timePassed The time passed since this Keyframe began
328*/
329template<class T>
330float tAnimation<T>::negExp(float timePassed) const
331{
332  float d = this->currentKeyFrame->value - this->nextKeyFrame->value;
333  float e = d * (1.0 - expf(- timePassed * expFactor));
334  return  this->currentKeyFrame->value - e;
335}
336
337/**
338   \brief a quadratic interpolation between this keyframe and the next one
339   \param timePassed The time passed since this Keyframe began
340*/
341template<class T>
342float tAnimation<T>::quadratic(float timePassed) const
343{
344
345}
346
347
348#endif /* _T_ANIMATION_H */
Note: See TracBrowser for help on using the repository browser.