Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/simple_animation.cc @ 3732

Last change on this file since 3732 was 3732, checked in by patrick, 19 years ago

orxonox/trunk: SimpleAnimation - now sinusoidal velocity distribution works also.

File size: 7.3 KB
Line 
1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific:
14   main-programmer: Patrick Boenzli
15   co-programmer: ...
16*/
17
18
19#include "simple_animation.h"
20#include "stdincl.h"
21#include "vector.h"
22#include "world_entity.h"
23
24using namespace std;
25
26
27
28SimpleAnimation* SimpleAnimation::singletonRef = 0;
29/**
30   \brief gets the singleton instance
31   \returns singleton instance
32*/
33SimpleAnimation* SimpleAnimation::getInstance()
34{
35  if( singletonRef == NULL)
36    singletonRef = new SimpleAnimation();
37  return singletonRef;
38}
39
40/**
41   \brief standard constructor
42*/
43SimpleAnimation::SimpleAnimation () 
44{
45   this->setClassName ("SimpleAnimation");
46   this->frames = new tList<KeyFrame>();
47   this->localTime = 0;
48   this->bRunning = false;
49   this->currentFrame = NULL;
50   this->lastFrame = NULL;
51
52   this->tmpVect = new Vector();
53   this->lastPosition = new Vector();
54}
55
56
57/**
58   \brief standard deconstructor
59
60*/
61SimpleAnimation::~SimpleAnimation () 
62{
63  tIterator<KeyFrame>* iterator = this->frames->getIterator();
64  KeyFrame* frame = iterator->nextElement(); 
65  while( frame != NULL) 
66    { 
67      delete frame;
68      frame = iterator->nextElement();
69    }
70  delete iterator;
71  delete this->frames;
72}
73
74
75/**
76   \brief this determines the start of an Animator Describtion
77
78   this can then be followed by different commands like addKeyFrame(..) etc. and
79   will be closed with AnimatiorEnd()
80*/
81void SimpleAnimation::animatorBegin()
82{
83  this->bDescriptive = true;
84}
85
86
87/**
88   \brief this determines the end of an Animator Describtion
89
90   this can then be followed by different commands like addKeyFrame(..) etc. and
91   will be closed with AnimatiorEnd()
92*/
93void SimpleAnimation::animatorEnd()
94{
95  this->workingObject = NULL;
96  this->bDescriptive = false;
97}
98
99
100/**
101   \brief select an object to work on by using this function
102   \param object wo work on
103*/
104void SimpleAnimation::selectObject(WorldEntity* entity)
105{
106  this->workingObject = entity;
107}
108
109
110
111/**
112   \brief adds a keyframe with properties
113   \param the point of the object
114   \param and the direction of it
115   \param at this time
116*/
117void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time)
118{
119  if( !this->bDescriptive)
120    {
121      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
122      return;
123    }
124  KeyFrame* frame = new KeyFrame;
125  frame->position = point;
126  frame->direction = direction;
127  frame->time = time;
128  frame->mode = DEFAULT_ANIMATION_MODE;
129  frame->object = this->workingObject;
130  this->frames->add(frame);
131}
132
133
134/**
135   \brief adds a keyframe with properties
136   \param the point of the object
137   \param and the direction of it
138   \param at this time
139   \param function of the velocity of the movement
140*/
141void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* direction, float time, movementMode mode)
142{
143  if( !this->bDescriptive)
144    {
145      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
146      return;
147    }
148  KeyFrame* frame = new KeyFrame;
149  frame->position = point;
150  frame->direction = direction;
151  frame->time = time;
152  frame->mode = mode;
153  frame->object = this->workingObject;
154  this->frames->add(frame);
155}
156
157/**
158   \brief adds a already defined keyframe
159   \param the keyframe to add
160*/
161void SimpleAnimation::addKeyFrame(KeyFrame* frame)
162{
163  if( !this->bDescriptive)
164    {
165      PRINTF(1)("SimpleAnimation: executing animation code outside a AnimationBegin()/AnimationEnd() - ignoring\n");
166      return;
167    }
168  frame->object = this->workingObject;
169  this->frames->add(frame);
170}
171
172
173/**
174   \brief clear the list of keyframes, deleting all keyframes included
175*/
176void SimpleAnimation::reset()
177{
178  tIterator<KeyFrame>* iterator = this->frames->getIterator();
179  KeyFrame* frame = iterator->nextElement(); 
180  while( frame != NULL) 
181    { 
182      delete frame;
183      frame = iterator->nextElement();
184    }
185  delete iterator;
186  delete this->frames;
187
188  this->frames = new tList<KeyFrame>();
189  this->localTime = 0;
190  this->bRunning = false;
191
192  this->currentFrame = NULL;
193  this->lastFrame = NULL;
194}
195
196/**
197   \brief starts the animation, therefore listens to tick signals
198*/
199void SimpleAnimation::start()
200{
201  if( this->bRunning)
202    {
203      PRINTF(2)("SimpleAnimatin is already running. You are trying to start it again.\n");
204    return;
205    }
206 
207  this->localTime = 0;
208  this->lastFrame = this->frames->firstElement();
209  this->currentFrame = this->frames->nextElement(this->currentFrame);
210  this->bRunning = true;
211}
212
213
214/**
215   \brief stops the animation, immune to tick signals
216*/
217void SimpleAnimation::stop()
218{
219  this->bRunning = false;
220}
221
222/**
223   \brief stops and then starts the animation from begining
224*/
225void SimpleAnimation::restart()
226{
227  this->localTime = 0;
228  this->lastFrame = this->frames->firstElement();
229  this->currentFrame = this->frames->nextElement(this->currentFrame);
230  this->bRunning = true;
231}
232
233/**
234   \brief pauses the animation until resumed
235*/
236void SimpleAnimation::pause()
237{
238  this->bRunning = false;
239}
240
241/**
242   \brief resumes a pause, if not paused, no effect
243*/
244void SimpleAnimation::resume()
245{
246  this->bRunning = true;
247}
248
249
250/**
251   \brief heart beat, next animation step
252*/
253void SimpleAnimation::tick(float time)
254{
255  if( !this->bRunning)
256    return;
257
258  this->localTime += time;
259  /* first get the current frame via time-stamps */ 
260  while( this->localTime > this->currentFrame->time)
261    {
262      printf("SimpleAnimation::tick(...) - changing Frame\n");
263      this->localTime -= this->currentFrame->time;
264
265      this->currentFrame->object->setRelCoor(*this->currentFrame->position);
266      *this->lastPosition = *this->currentFrame->position;
267
268      this->lastFrame = this->currentFrame;
269      this->currentFrame = this->frames->nextElement(this->currentFrame);
270      this->mode = this->currentFrame->mode;
271    }
272
273  /* now animate it */
274  switch( this->mode)
275    {
276    case LINEAR:
277
278      *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position;
279      *this->tmpVect = *this->tmpVect * this->localTime / this->currentFrame->time;
280      this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect);
281      *this->lastPosition = *this->tmpVect;
282      break;
283    case EXP:
284     
285      break;
286    case NEG_EXP:
287      *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position;
288      *this->tmpVect = *this->tmpVect * (1 - exp(- this->localTime / this->currentFrame->time));     
289      this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect);
290      *this->lastPosition = *this->tmpVect;
291      break;
292    case SIN:
293      *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position;
294      *this->tmpVect = *this->tmpVect * 0.5*(1 - cos(M_PI * this->localTime / this->currentFrame->time));     
295      this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect);
296      *this->lastPosition = *this->tmpVect;
297      break;
298    case COS:
299     
300      break;
301    case QUADRATIC:
302      *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position;
303      *this->tmpVect = *this->tmpVect * 1/3 * ldexpf(this->localTime, 3);
304      break;
305    default:
306      break;
307    }
308}
Note: See TracBrowser for help on using the repository browser.