Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: doxy-todo's

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