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 "p_node.h" |
---|
22 | #include "vector.h" |
---|
23 | |
---|
24 | using namespace std; |
---|
25 | |
---|
26 | /** |
---|
27 | \brief standard constructor |
---|
28 | \param the point of the object |
---|
29 | \param and the orientation of it |
---|
30 | \param at this time |
---|
31 | */ |
---|
32 | KeyFrame::KeyFrame(Vector* point, Quaternion* orientation, float time) |
---|
33 | { |
---|
34 | this->setRelCoor(point); |
---|
35 | this->setRelDir(orientation); |
---|
36 | this->time = time; |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | /** |
---|
41 | \brief standard constructor |
---|
42 | \param the point of the object |
---|
43 | \param and the orientation of it |
---|
44 | \param at this time |
---|
45 | \param function of the velocity of the movement |
---|
46 | */ |
---|
47 | KeyFrame::KeyFrame(Vector* point, Quaternion* orientation, float time, movementMode mode) |
---|
48 | { |
---|
49 | this->setRelCoor(point); |
---|
50 | this->setRelDir(orientation); |
---|
51 | this->time = time; |
---|
52 | this->mode = mode; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | /** |
---|
57 | \brief standard deconstructor |
---|
58 | */ |
---|
59 | KeyFrame::~KeyFrame() |
---|
60 | { |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | /** |
---|
65 | \brief sets the important properties of a Keyframe |
---|
66 | \param the point of the object |
---|
67 | \param and the orientation of it |
---|
68 | \param at this time |
---|
69 | */ |
---|
70 | void KeyFrame::set(Vector* point, Quaternion* orientation, float time) |
---|
71 | { |
---|
72 | this->setRelCoor(point); |
---|
73 | this->setRelDir(orientation); |
---|
74 | this->time = time; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | /** |
---|
79 | \brief sets the important properties of a Keyframe |
---|
80 | \param the point of the object |
---|
81 | \param and the orientation of it |
---|
82 | \param at this time |
---|
83 | \param function of the velocity of the movement |
---|
84 | */ |
---|
85 | void KeyFrame::set(Vector* point, Quaternion* orientation, float time, movementMode mode) |
---|
86 | { |
---|
87 | this->setRelCoor(point); |
---|
88 | this->setRelDir(orientation); |
---|
89 | this->time = time; |
---|
90 | this->mode = mode; |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | /** |
---|
96 | \brief standard constructor |
---|
97 | */ |
---|
98 | SimpleAnimation::SimpleAnimation (PNode* parent) |
---|
99 | { |
---|
100 | this->setClassName ("SimpleAnimation"); |
---|
101 | this->frames = new tList<KeyFrame>(); |
---|
102 | this->localTime = 0; |
---|
103 | this->bRunning = false; |
---|
104 | this->parent = parent; |
---|
105 | this->currentFrame = NULL; |
---|
106 | this->lastFrame = NULL; |
---|
107 | |
---|
108 | this->tmpVect = new Vector(); |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | /** |
---|
113 | \brief standard deconstructor |
---|
114 | |
---|
115 | */ |
---|
116 | SimpleAnimation::~SimpleAnimation () |
---|
117 | { |
---|
118 | tIterator<KeyFrame>* iterator = this->frames->getIterator(); |
---|
119 | KeyFrame* frame = iterator->nextElement(); |
---|
120 | while( frame != NULL) |
---|
121 | { |
---|
122 | delete frame; |
---|
123 | frame = iterator->nextElement(); |
---|
124 | } |
---|
125 | delete iterator; |
---|
126 | delete this->frames; |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | |
---|
131 | /** |
---|
132 | \brief adds a keyframe with properties |
---|
133 | \param the point of the object |
---|
134 | \param and the orientation of it |
---|
135 | \param at this time |
---|
136 | */ |
---|
137 | void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* orientation, float time) |
---|
138 | { |
---|
139 | KeyFrame* frame = new KeyFrame(point, orientation, time); |
---|
140 | this->frames->add(frame); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | /** |
---|
145 | \brief adds a keyframe with properties |
---|
146 | \param the point of the object |
---|
147 | \param and the orientation of it |
---|
148 | \param at this time |
---|
149 | \param function of the velocity of the movement |
---|
150 | */ |
---|
151 | void SimpleAnimation::addKeyFrame(Vector* point, Quaternion* orientation, float time, movementMode mode) |
---|
152 | { |
---|
153 | KeyFrame* frame = new KeyFrame(point, orientation, time, mode); |
---|
154 | this->frames->add(frame); |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | \brief adds a already defined keyframe |
---|
159 | \param the keyframe to add |
---|
160 | */ |
---|
161 | void SimpleAnimation::addKeyFrame(KeyFrame* frame) |
---|
162 | { |
---|
163 | if( frame != NULL) |
---|
164 | this->frames->add(frame); |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | /** |
---|
169 | \brief clear the list of keyframes, deleting all keyframes included |
---|
170 | */ |
---|
171 | void SimpleAnimation::reset() |
---|
172 | { |
---|
173 | tIterator<KeyFrame>* iterator = this->frames->getIterator(); |
---|
174 | KeyFrame* frame = iterator->nextElement(); |
---|
175 | while( frame != NULL) |
---|
176 | { |
---|
177 | delete frame; |
---|
178 | frame = iterator->nextElement(); |
---|
179 | } |
---|
180 | delete iterator; |
---|
181 | delete this->frames; |
---|
182 | |
---|
183 | this->frames = new tList<KeyFrame>(); |
---|
184 | this->localTime = 0; |
---|
185 | this->bRunning = false; |
---|
186 | |
---|
187 | this->currentFrame = NULL; |
---|
188 | this->lastFrame = NULL; |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | \brief starts the animation, therefore listens to tick signals |
---|
193 | */ |
---|
194 | void SimpleAnimation::start() |
---|
195 | { |
---|
196 | if( this->bRunning) |
---|
197 | { |
---|
198 | PRINTF(2)("SimpleAnimatin is already running. You are trying to start it again.\n"); |
---|
199 | return; |
---|
200 | } |
---|
201 | |
---|
202 | this->localTime = 0; |
---|
203 | this->lastFrame = this->frames->firstElement(); |
---|
204 | this->currentFrame = this->frames->nextElement(this->currentFrame); |
---|
205 | this->bRunning = true; |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | /** |
---|
210 | \brief stops the animation, immune to tick signals |
---|
211 | */ |
---|
212 | void SimpleAnimation::stop() |
---|
213 | { |
---|
214 | this->bRunning = false; |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | \brief stops and then starts the animation from begining |
---|
219 | */ |
---|
220 | void SimpleAnimation::restart() |
---|
221 | { |
---|
222 | this->localTime = 0; |
---|
223 | this->lastFrame = this->frames->firstElement(); |
---|
224 | this->currentFrame = this->frames->nextElement(this->currentFrame); |
---|
225 | this->bRunning = true; |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | \brief pauses the animation until resumed |
---|
230 | */ |
---|
231 | void SimpleAnimation::pause() |
---|
232 | { |
---|
233 | this->bRunning = false; |
---|
234 | } |
---|
235 | |
---|
236 | /** |
---|
237 | \brief resumes a pause, if not paused, no effect |
---|
238 | */ |
---|
239 | void SimpleAnimation::resume() |
---|
240 | { |
---|
241 | this->bRunning = true; |
---|
242 | } |
---|
243 | |
---|
244 | |
---|
245 | /** |
---|
246 | \brief heart beat, next animation step |
---|
247 | */ |
---|
248 | void SimpleAnimation::tick(float time) |
---|
249 | { |
---|
250 | if( !this->bRunning) |
---|
251 | return; |
---|
252 | |
---|
253 | this->localTime += time; |
---|
254 | /* first get the current frame via time-stamps */ |
---|
255 | while( this->localTime > this->currentFrame->time) |
---|
256 | { |
---|
257 | printf("SimpleAnimation::tick(...) - changing Frame"); |
---|
258 | this->lastFrame = this->currentFrame; |
---|
259 | this->currentFrame = this->frames->nextElement(this->currentFrame); |
---|
260 | this->localTime -= this->currentFrame->time; |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | /* now animate it */ |
---|
265 | switch( this->mode) |
---|
266 | { |
---|
267 | case LINEAR: |
---|
268 | |
---|
269 | *this->tmpVect = this->currentFrame->getAbsCoor() - this->lastFrame->getAbsCoor(); |
---|
270 | *this->tmpVect = *this->tmpVect * this->localTime / this->currentFrame->time; |
---|
271 | //this->setAbsCoordinate(this->tmpVect); |
---|
272 | break; |
---|
273 | case EXP: |
---|
274 | |
---|
275 | break; |
---|
276 | case NEG_EXP: |
---|
277 | *this->tmpVect = this->currentFrame->getAbsCoor() - this->lastFrame->getAbsCoor(); |
---|
278 | *this->tmpVect = *this->tmpVect * (1 - exp(- this->localTime / this->currentFrame->time)); |
---|
279 | break; |
---|
280 | case SIN: |
---|
281 | *this->tmpVect = this->currentFrame->getAbsCoor() - this->lastFrame->getAbsCoor(); |
---|
282 | *this->tmpVect = *this->tmpVect * (1 - cos(- this->localTime / this->currentFrame->time)); |
---|
283 | break; |
---|
284 | case COS: |
---|
285 | |
---|
286 | break; |
---|
287 | case QUADRATIC: |
---|
288 | *this->tmpVect = this->currentFrame->getAbsCoor() - this->lastFrame->getAbsCoor(); |
---|
289 | *this->tmpVect = *this->tmpVect * 1/3 * ldexpf(this->localTime, 3); |
---|
290 | break; |
---|
291 | default: |
---|
292 | break; |
---|
293 | } |
---|
294 | } |
---|