Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: minor change

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