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->bRunning = false; |
---|
116 | break; |
---|
117 | case ANIM_INF_REWIND: |
---|
118 | break; |
---|
119 | } |
---|
120 | } |
---|
121 | this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame); |
---|
122 | this->setAnimFunc(this->currentKeyFrame->animFunc); |
---|
123 | |
---|
124 | if( this->currentKeyFrame->animFunc == ANIM_NEG_EXP) |
---|
125 | { |
---|
126 | this->tmpVect = this->nextKeyFrame->position - this->currentKeyFrame->position; |
---|
127 | this->deltaT = 1/this->currentKeyFrame->duration * logf(1.0 + 600.0/this->tmpVect.len()); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | /* now animate it */ |
---|
132 | (this->*animFunc)(this->localTime); |
---|
133 | /* |
---|
134 | switch( this->movMode) |
---|
135 | { |
---|
136 | case LINEAR: |
---|
137 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
138 | *this->tmpVect = *this->tmpVect * this->localTime / this->currentFrame->time; |
---|
139 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
---|
140 | *this->lastPosition = *this->tmpVect; |
---|
141 | break; |
---|
142 | case EXP: |
---|
143 | |
---|
144 | break; |
---|
145 | case NEG_EXP: |
---|
146 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
147 | *this->tmpVect = *this->tmpVect * (1 - expf(- this->localTime * this->deltaT)); |
---|
148 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
---|
149 | *this->lastPosition = *this->tmpVect; |
---|
150 | break; |
---|
151 | case SIN: |
---|
152 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
153 | *this->tmpVect = *this->tmpVect * 0.5*(1 - cos(M_PI * this->localTime / this->currentFrame->time)); |
---|
154 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
---|
155 | *this->lastPosition = *this->tmpVect; |
---|
156 | break; |
---|
157 | case COS: |
---|
158 | |
---|
159 | break; |
---|
160 | case QUADRATIC: |
---|
161 | *this->tmpVect = *this->currentFrame->position - *this->lastFrame->position; |
---|
162 | *this->tmpVect = *this->tmpVect * 1/3 * ldexpf(this->localTime, 3); |
---|
163 | break; |
---|
164 | default: |
---|
165 | break; |
---|
166 | } |
---|
167 | */ |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | void Animation3D::setAnimFunc(ANIM_FUNCTION animFunc) |
---|
174 | { |
---|
175 | switch (animFunc) |
---|
176 | { |
---|
177 | default: |
---|
178 | case ANIM_CONSTANT: |
---|
179 | this->animFunc = &Animation3D::constant; |
---|
180 | break; |
---|
181 | case ANIM_LINEAR: |
---|
182 | this->animFunc = &Animation3D::linear; |
---|
183 | break; |
---|
184 | case ANIM_SINE: |
---|
185 | this->animFunc = &Animation3D::sine; |
---|
186 | break; |
---|
187 | case ANIM_COSINE: |
---|
188 | this->animFunc = &Animation3D::cosine; |
---|
189 | break; |
---|
190 | case ANIM_EXP: |
---|
191 | this->animFunc = &Animation3D::exp; |
---|
192 | break; |
---|
193 | case ANIM_NEG_EXP: |
---|
194 | this->animFunc = &Animation3D::negExp; |
---|
195 | break; |
---|
196 | case ANIM_QUADRATIC: |
---|
197 | this->animFunc = &Animation3D::quadratic; |
---|
198 | break; |
---|
199 | case ANIM_RANDOM: |
---|
200 | this->animFunc = &Animation3D::random; |
---|
201 | break; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | void Animation3D::constant(float timePassed) const |
---|
206 | { |
---|
207 | this->object->setRelCoor(this->currentKeyFrame->position); |
---|
208 | |
---|
209 | /* |
---|
210 | this->tmpVect = this->nextKeyFrame->position - this->currentKeyFrame->position; |
---|
211 | this->tmpVect = this->tmpVect * this->localTime / this->currentKeyFrame->duration; |
---|
212 | this->currentFrame->object->setRelCoor(*this->lastFrame->position + *this->tmpVect); |
---|
213 | this->lastPosition = this->tmpVect; |
---|
214 | */ |
---|
215 | } |
---|
216 | |
---|
217 | void Animation3D::linear(float timePassed) const |
---|
218 | { |
---|
219 | this->object->setRelCoor(this->currentKeyFrame->position + |
---|
220 | (this->nextKeyFrame->position - this->currentKeyFrame->position) * |
---|
221 | (timePassed/this->currentKeyFrame->duration)); |
---|
222 | } |
---|
223 | |
---|
224 | void Animation3D::sine(float timePassed) const |
---|
225 | { |
---|
226 | |
---|
227 | } |
---|
228 | |
---|
229 | void Animation3D::cosine(float timePassed) const |
---|
230 | { |
---|
231 | |
---|
232 | } |
---|
233 | |
---|
234 | void Animation3D::exp(float timePassed) const |
---|
235 | { |
---|
236 | |
---|
237 | } |
---|
238 | |
---|
239 | void Animation3D::negExp(float timePassed) const |
---|
240 | { |
---|
241 | |
---|
242 | } |
---|
243 | |
---|
244 | void Animation3D::quadratic(float timePassed) const |
---|
245 | { |
---|
246 | |
---|
247 | } |
---|
248 | |
---|
249 | void Animation3D::random(float timePassed) const |
---|
250 | { |
---|
251 | |
---|
252 | } |
---|