Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: moved the infinity-handling into animation.cc

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  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            this->handleInfinity();
188          this->nextKeyFrame = this->keyFrameList->nextElement(this->currentKeyFrame);
189          printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value);
190          this->setAnimFunc(this->currentKeyFrame->animFunc);     
191        }
192     
193      (this->object->*(funcToAnim))((this->*animFunc)(this->localTime));
194    }
195}
196
197/**
198   \brief Sets The kind of Animation between this keyframe and the next one
199   \param animFunc The Type of Animation to set
200*/
201template<class T>
202void tAnimation<T>::setAnimFunc(ANIM_FUNCTION animFunc)
203{
204  switch (animFunc)
205    {
206    default:
207    case ANIM_CONSTANT:
208      this->animFunc = &tAnimation<T>::constant;
209      break;
210    case ANIM_LINEAR:
211      this->animFunc = &tAnimation<T>::linear;
212      break;
213    case ANIM_SINE:
214      this->animFunc = &tAnimation<T>::sine;
215      break;
216    case ANIM_COSINE:
217      this->animFunc = &tAnimation<T>::cosine;
218      break;
219    case ANIM_EXP:
220      this->animFunc = &tAnimation<T>::exp;
221      break;
222    case ANIM_NEG_EXP:
223      {
224        this->animFunc = &tAnimation<T>::negExp;
225        float d = fabs(this->currentKeyFrame->value - this->nextKeyFrame->value);
226        expFactor =  - 1.0 / this->currentKeyFrame->duration * logf(DELTA_X);
227        break;
228      }
229    case ANIM_QUADRATIC:
230      this->animFunc = &tAnimation<T>::quadratic;
231      break;
232    case ANIM_RANDOM:
233      this->animFunc = &tAnimation<T>::random;
234      break;
235    }
236}
237
238
239// animation functions
240/**
241   \brief stays at the value of the currentKeyFrame
242   \param timePassed The time passed since this Keyframe began
243*/
244template<class T>
245float tAnimation<T>::constant(float timePassed) const
246{
247  return this->currentKeyFrame->value;
248}
249
250/**
251   \brief linear interpolation between this keyframe and the next one
252   \param timePassed The time passed since this Keyframe began
253*/
254template<class T>
255float tAnimation<T>::linear(float timePassed) const 
256{
257  return this->currentKeyFrame->value + (this->nextKeyFrame->value - this->currentKeyFrame->value) 
258    * (timePassed / this->currentKeyFrame->duration);
259  //  PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame);
260  //  return val;
261}
262
263/**
264   \brief a Sinusodial Interpolation between this keyframe and the next one
265   \param timePassed The time passed since this Keyframe began
266*/
267template<class T>
268float tAnimation<T>::sine(float timePassed) const
269{
270  float d = this->currentKeyFrame->value - this->nextKeyFrame->value;
271  float e = 0.5 * d * (1 - cos(M_PI * timePassed / this->currentKeyFrame->duration));
272  return this->currentKeyFrame->value - e;
273  /*
274  return his->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value)
275    * sin(timePassed / this->currentKeyFrame->duration * M_PI);
276  */
277}
278
279/**
280   \brief a cosine interpolation between this keyframe and the next one
281   \param timePassed The time passed since this Keyframe began
282*/
283template<class T>
284float tAnimation<T>::cosine(float timePassed) const
285{
286  float d = this->currentKeyFrame->value - this->nextKeyFrame->value;
287  float e = 0.5 * d * (sin(M_PI * timePassed / this->currentKeyFrame->duration));
288  if( timePassed > 0.5*this->currentKeyFrame->duration) e = (d - e);
289  return this->currentKeyFrame->value - e;
290  /*
291  return this->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value)
292    * cos(timePassed / this->currentKeyFrame->duration * M_PI);
293  */
294}
295
296/**
297   \brief an exponential interpolation between this keyframe and the next one
298   \param timePassed The time passed since this Keyframe began
299*/
300template<class T>
301float tAnimation<T>::exp(float timePassed) const
302{
303}
304
305/**
306   \brief a negative exponential interpolation between this keyframe and the next one
307   \param timePassed The time passed since this Keyframe began
308*/
309template<class T>
310float tAnimation<T>::negExp(float timePassed) const
311{
312  float d = this->currentKeyFrame->value - this->nextKeyFrame->value;
313  float e = d * (1.0 - expf(- timePassed * expFactor));
314  return  this->currentKeyFrame->value - e;
315}
316
317/**
318   \brief a quadratic interpolation between this keyframe and the next one
319   \param timePassed The time passed since this Keyframe began
320*/
321template<class T>
322float tAnimation<T>::quadratic(float timePassed) const
323{
324  this->linear(timePassed);
325}
326
327/**
328   \brief some random animation (fluctuating)
329   \param timePassed The time passed since this Keyframe began
330*/
331template<class T>
332float tAnimation<T>::random(float timePassed) const
333{
334  return this->currentKeyFrame->value * (float)rand()/(float)RAND_MAX;
335}
336
337#endif /* _T_ANIMATION_H */
Note: See TracBrowser for help on using the repository browser.