Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/animation3d.cc @ 3855

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

orxonox/trunk: animation of weapon reimplemented (but only with Linear interpolation.
doxy-tags

File size: 7.9 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: Patrick Boenzli
13   co-programmer: Benjamin Grauer
14
15   2005-04-17: Benjamin Grauer
16          Rewritte all functions, so it will fit into the Animation-class
17*/
18
19
20#include "animation3d.h"
21
22#include "p_node.h"
23
24using namespace std;
25
26/**
27   \brief standard constructor
28*/
29Animation3D::Animation3D(PNode* object)
30{
31  this->object = object;
32
33  // create a new List
34  this->keyFrameList = new tList<KeyFrame3D>();
35  KeyFrame3D* tmpKeyFrame = new KeyFrame3D;
36  tmpKeyFrame->position = Vector();
37  tmpKeyFrame->direction = Quaternion();
38  keyFrameList->add(tmpKeyFrame);
39
40  this->currentKeyFrame = tmpKeyFrame;
41  this->nextKeyFrame = tmpKeyFrame;
42
43  this->animFunc = &Animation3D::linear;
44}
45
46/**
47   \brief standard deconstructor
48   
49   deletes all the Keyframes
50*/
51Animation3D::~Animation3D(void)
52{
53  // delete all the KeyFrames
54  tIterator<KeyFrame3D>* itKF = keyFrameList->getIterator();
55  KeyFrame3D*  enumKF = itKF->nextElement();
56  while (enumKF)
57    {
58      delete enumKF;
59      enumKF = itKF->nextElement();
60    }
61  delete itKF;
62  delete this->keyFrameList;
63}
64
65/**
66   \brief rewinds the Animation to the beginning (first KeyFrame and time == 0)
67*/
68void Animation3D::rewind(void)
69{
70  this->currentKeyFrame = keyFrameList->firstElement();
71  this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement());
72  this->localTime = 0.0;
73}
74
75/**
76   \brief Appends a new Keyframe
77   \param position The position of the new Keyframe
78   \param direction The direction of the new Keyframe.
79   \param duration The duration from the new KeyFrame to the next one
80   \param animFunc The function to animate between this keyFrame and the next one
81*/
82void Animation3D::addKeyFrame(Vector position, Quaternion direction, float duration, ANIM_FUNCTION animFunc)
83{
84  // some small check
85  if (duration <= 0.0)
86    duration = 1.0;
87
88  KeyFrame3D* tmpKeyFrame;
89   
90  if (bHasKeys)
91    {
92      tmpKeyFrame = new KeyFrame3D;
93      if (this->currentKeyFrame == this->nextKeyFrame)
94        this->nextKeyFrame = tmpKeyFrame;
95      this->keyFrameList->add(tmpKeyFrame);
96
97    }
98  else
99    {
100      tmpKeyFrame = this->keyFrameList->firstElement();
101      bHasKeys = true;
102      this->setAnimFunc(animFunc);
103    }
104
105  tmpKeyFrame->position = position;
106  tmpKeyFrame->direction = direction;
107  tmpKeyFrame->duration = duration;
108  tmpKeyFrame->animFunc = animFunc;
109
110}
111
112/**
113   \brief ticks the Animation
114   \param dt how much time to tick
115*/
116void Animation3D::tick(float dt)
117{
118  if (this->bRunning)
119    { 
120      this->localTime += dt;
121      if (localTime >= this->currentKeyFrame->duration)
122        {
123          // switching to the next Key-Frame
124          this->localTime -= this->currentKeyFrame->duration;
125          this->currentKeyFrame = this->nextKeyFrame;
126          // checking, if we should still Play the animation
127          if (this->currentKeyFrame == this->keyFrameList->lastElement())
128            {
129              switch (this->postInfinity)
130                {
131                case ANIM_INF_CONSTANT:
132                  this->localTime = 0.0;
133                  this->bRunning = false;
134                  break;
135                case ANIM_INF_REWIND:
136                  break;
137                }
138            }
139          this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame);
140          this->setAnimFunc(this->currentKeyFrame->animFunc);     
141         
142          if( this->currentKeyFrame->animFunc == ANIM_NEG_EXP)
143            {
144              this->tmpVect = this->nextKeyFrame->position - this->currentKeyFrame->position;
145              this->deltaT = 1/this->currentKeyFrame->duration * logf(1.0 + 600.0/this->tmpVect.len());
146            }
147        }
148
149      /* now animate it */
150      (this->*animFunc)(this->localTime);
151      /*
152      switch( this->movMode)
153        {
154        case LINEAR:
155          *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position;
156          *this->tmpVect = *this->tmpVect * this->localTime / this->currentFrame->time;
157          this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect);
158          *this->lastPosition = *this->tmpVect;
159          break;
160        case EXP:
161             
162          break;
163        case NEG_EXP:
164          *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position;
165          *this->tmpVect = *this->tmpVect * (1 - expf(- this->localTime * this->deltaT));     
166          this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect);
167          *this->lastPosition = *this->tmpVect;
168          break;
169        case SIN:
170          *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position;
171          *this->tmpVect = *this->tmpVect * 0.5*(1 - cos(M_PI * this->localTime / this->currentFrame->time));     
172          this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect);
173          *this->lastPosition = *this->tmpVect;
174          break;
175        case COS:
176             
177          break;
178        case QUADRATIC:
179          *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position;
180          *this->tmpVect = *this->tmpVect * 1/3 * ldexpf(this->localTime, 3);
181          break;
182        default:
183          break;
184        }
185      */
186    }
187}
188
189
190/**
191   \brief Sets The kind of Animation between this keyframe and the next one
192   \param animFunc The Type of Animation to set
193*/
194void Animation3D::setAnimFunc(ANIM_FUNCTION animFunc)
195{
196  switch (animFunc)
197    {
198    default:
199    case ANIM_CONSTANT:
200      this->animFunc = &Animation3D::constant;
201      break;
202    case ANIM_LINEAR:
203      this->animFunc = &Animation3D::linear;
204      break;
205    case ANIM_SINE:
206      this->animFunc = &Animation3D::sine;
207      break;
208    case ANIM_COSINE:
209      this->animFunc = &Animation3D::cosine;
210      break;
211    case ANIM_EXP:
212      this->animFunc = &Animation3D::exp;
213      break;
214    case ANIM_NEG_EXP:
215      this->animFunc = &Animation3D::negExp;
216      break;
217    case ANIM_QUADRATIC:
218      this->animFunc = &Animation3D::quadratic;
219      break;
220    case ANIM_RANDOM:
221      this->animFunc = &Animation3D::random;
222      break;
223    }
224}
225
226/**
227   \brief stays at the value of the currentKeyFrame
228   \param timePassed The time passed since this Keyframe began
229*/
230void Animation3D::constant(float timePassed) const
231{
232  this->object->setRelCoor(this->currentKeyFrame->position);
233
234  /*
235    this->tmpVect = this->nextKeyFrame->position - this->currentKeyFrame->position;
236    this->tmpVect = this->tmpVect * this->localTime / this->currentKeyFrame->duration;
237    this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect);
238    this->lastPosition = this->tmpVect;
239  */
240}
241
242/**
243   \brief linear interpolation between this keyframe and the next one
244   \param timePassed The time passed since this Keyframe began
245*/
246void Animation3D::linear(float timePassed) const
247{
248  this->object->setRelCoor(this->currentKeyFrame->position +
249                          (this->nextKeyFrame->position - this->currentKeyFrame->position) * 
250                          (timePassed/this->currentKeyFrame->duration));
251}
252
253/**
254   \brief a Sinusodial Interpolation between this keyframe and the next one
255   \param timePassed The time passed since this Keyframe began
256*/
257void Animation3D::sine(float timePassed) const
258{
259
260}
261
262/**
263   \brief a cosine interpolation between this keyframe and the next one
264   \param timePassed The time passed since this Keyframe began
265*/
266void Animation3D::cosine(float timePassed) const
267{
268
269}
270
271/**
272   \brief an exponential interpolation between this keyframe and the next one
273   \param timePassed The time passed since this Keyframe began
274*/
275void Animation3D::exp(float timePassed) const
276{
277
278}
279
280/**
281   \brief a negative exponential interpolation between this keyframe and the next one
282   \param timePassed The time passed since this Keyframe began
283*/
284void Animation3D::negExp(float timePassed) const
285{
286  this->linear(timePassed);
287}
288
289/**
290   \brief a quadratic interpolation between this keyframe and the next one
291   \param timePassed The time passed since this Keyframe began
292*/
293void Animation3D::quadratic(float timePassed) const
294{
295
296}
297
298/**
299   \brief some random animation (fluctuating)
300   \param timePassed The time passed since this Keyframe began
301*/
302void Animation3D::random(float timePassed) const
303{
304
305}
Note: See TracBrowser for help on using the repository browser.