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 "stdincl.h" |
---|
23 | #include "vector.h" |
---|
24 | #include "p_node.h" |
---|
25 | |
---|
26 | using namespace std; |
---|
27 | |
---|
28 | |
---|
29 | Animation3D::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 | Animation3D::~Animation3D(void) |
---|
47 | { |
---|
48 | // delete all the KeyFrames |
---|
49 | tIterator<KeyFrame3D>* itKF = keyFrameList->getIterator(); |
---|
50 | KeyFrame3D* enumKF = itKF->nextElement(); |
---|
51 | while (enumKF) |
---|
52 | { |
---|
53 | delete enumKF; |
---|
54 | enumKF = itKF->nextElement(); |
---|
55 | } |
---|
56 | delete itKF; |
---|
57 | delete this->keyFrameList; |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | void Animation3D::rewind(void) |
---|
62 | { |
---|
63 | this->currentKeyFrame = keyFrameList->firstElement(); |
---|
64 | this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement()); |
---|
65 | this->localTime = 0.0; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | void Animation3D::addKeyFrame(Vector position, Quaternion direction, float duration = 1.0, ANIM_FUNCTION animFunc) |
---|
70 | { |
---|
71 | // some small check |
---|
72 | if (duration <= 0.0) |
---|
73 | duration = 1.0; |
---|
74 | |
---|
75 | KeyFrame3D* tmpKeyFrame; |
---|
76 | |
---|
77 | if (bHasKeys) |
---|
78 | { |
---|
79 | tmpKeyFrame = new KeyFrame3D; |
---|
80 | if (this->currentKeyFrame == this->nextKeyFrame) |
---|
81 | this->nextKeyFrame = tmpKeyFrame; |
---|
82 | this->keyFrameList->add(tmpKeyFrame); |
---|
83 | |
---|
84 | } |
---|
85 | else |
---|
86 | { |
---|
87 | tmpKeyFrame = this->keyFrameList->firstElement(); |
---|
88 | bHasKeys = true; |
---|
89 | this->setAnimFunc(animFunc); |
---|
90 | } |
---|
91 | |
---|
92 | tmpKeyFrame->position = position; |
---|
93 | tmpKeyFrame->direction = direction; |
---|
94 | tmpKeyFrame->duration = duration; |
---|
95 | tmpKeyFrame->animFunc = animFunc; |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | void Animation3D::tick(float dt) |
---|
100 | { |
---|
101 | if (this->bRunning) |
---|
102 | { |
---|
103 | this->localTime += dt; |
---|
104 | if (localTime >= this->currentKeyFrame->duration) |
---|
105 | { |
---|
106 | // switching to the next Key-Frame |
---|
107 | this->localTime -= this->currentKeyFrame->duration; |
---|
108 | this->currentKeyFrame = this->nextKeyFrame; |
---|
109 | // checking, if we should still Play the animation |
---|
110 | if (this->currentKeyFrame == this->keyFrameList->lastElement()) |
---|
111 | { |
---|
112 | switch (this->postInfinity) |
---|
113 | { |
---|
114 | case ANIM_INF_CONSTANT: |
---|
115 | this->localTime = 0.0; |
---|
116 | this->bRunning = false; |
---|
117 | break; |
---|
118 | case ANIM_INF_REWIND: |
---|
119 | break; |
---|
120 | } |
---|
121 | } |
---|
122 | this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame); |
---|
123 | this->setAnimFunc(this->currentKeyFrame->animFunc); |
---|
124 | |
---|
125 | if( this->currentKeyFrame->animFunc == ANIM_NEG_EXP) |
---|
126 | { |
---|
127 | this->tmpVect = this->nextKeyFrame->position - this->currentKeyFrame->position; |
---|
128 | this->deltaT = 1/this->currentKeyFrame->duration * logf(1.0 + 600.0/this->tmpVect.len()); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | /* now animate it */ |
---|
133 | (this->*animFunc)(this->localTime); |
---|
134 | /* |
---|
135 | switch( this->movMode) |
---|
136 | { |
---|
137 | case LINEAR: |
---|
138 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
139 | *this->tmpVect = *this->tmpVect * this->localTime / this->currentFrame->time; |
---|
140 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
---|
141 | *this->lastPosition = *this->tmpVect; |
---|
142 | break; |
---|
143 | case EXP: |
---|
144 | |
---|
145 | break; |
---|
146 | case NEG_EXP: |
---|
147 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
148 | *this->tmpVect = *this->tmpVect * (1 - expf(- this->localTime * this->deltaT)); |
---|
149 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
---|
150 | *this->lastPosition = *this->tmpVect; |
---|
151 | break; |
---|
152 | case SIN: |
---|
153 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
154 | *this->tmpVect = *this->tmpVect * 0.5*(1 - cos(M_PI * this->localTime / this->currentFrame->time)); |
---|
155 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
---|
156 | *this->lastPosition = *this->tmpVect; |
---|
157 | break; |
---|
158 | case COS: |
---|
159 | |
---|
160 | break; |
---|
161 | case QUADRATIC: |
---|
162 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
163 | *this->tmpVect = *this->tmpVect * 1/3 * ldexpf(this->localTime, 3); |
---|
164 | break; |
---|
165 | default: |
---|
166 | break; |
---|
167 | } |
---|
168 | */ |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | |
---|
174 | void Animation3D::setAnimFunc(ANIM_FUNCTION animFunc) |
---|
175 | { |
---|
176 | switch (animFunc) |
---|
177 | { |
---|
178 | default: |
---|
179 | case ANIM_CONSTANT: |
---|
180 | this->animFunc = &Animation3D::constant; |
---|
181 | break; |
---|
182 | case ANIM_LINEAR: |
---|
183 | this->animFunc = &Animation3D::linear; |
---|
184 | break; |
---|
185 | case ANIM_SINE: |
---|
186 | this->animFunc = &Animation3D::sine; |
---|
187 | break; |
---|
188 | case ANIM_COSINE: |
---|
189 | this->animFunc = &Animation3D::cosine; |
---|
190 | break; |
---|
191 | case ANIM_EXP: |
---|
192 | this->animFunc = &Animation3D::exp; |
---|
193 | break; |
---|
194 | case ANIM_NEG_EXP: |
---|
195 | this->animFunc = &Animation3D::negExp; |
---|
196 | break; |
---|
197 | case ANIM_QUADRATIC: |
---|
198 | this->animFunc = &Animation3D::quadratic; |
---|
199 | break; |
---|
200 | case ANIM_RANDOM: |
---|
201 | this->animFunc = &Animation3D::random; |
---|
202 | break; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | void Animation3D::constant(float timePassed) const |
---|
207 | { |
---|
208 | this->object->setRelCoor(this->currentKeyFrame->position); |
---|
209 | |
---|
210 | /* |
---|
211 | this->tmpVect = this->nextKeyFrame->position - this->currentKeyFrame->position; |
---|
212 | this->tmpVect = this->tmpVect * this->localTime / this->currentKeyFrame->duration; |
---|
213 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
---|
214 | this->lastPosition = this->tmpVect; |
---|
215 | */ |
---|
216 | } |
---|
217 | |
---|
218 | void Animation3D::linear(float timePassed) const |
---|
219 | { |
---|
220 | this->object->setRelCoor(this->currentKeyFrame->position + |
---|
221 | (this->nextKeyFrame->position - this->currentKeyFrame->position) * |
---|
222 | (timePassed/this->currentKeyFrame->duration)); |
---|
223 | } |
---|
224 | |
---|
225 | void Animation3D::sine(float timePassed) const |
---|
226 | { |
---|
227 | |
---|
228 | } |
---|
229 | |
---|
230 | void Animation3D::cosine(float timePassed) const |
---|
231 | { |
---|
232 | |
---|
233 | } |
---|
234 | |
---|
235 | void Animation3D::exp(float timePassed) const |
---|
236 | { |
---|
237 | |
---|
238 | } |
---|
239 | |
---|
240 | void Animation3D::negExp(float timePassed) const |
---|
241 | { |
---|
242 | |
---|
243 | } |
---|
244 | |
---|
245 | void Animation3D::quadratic(float timePassed) const |
---|
246 | { |
---|
247 | |
---|
248 | } |
---|
249 | |
---|
250 | void Animation3D::random(float timePassed) const |
---|
251 | { |
---|
252 | |
---|
253 | } |
---|