1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2006 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 | |
---|
13 | This is anohter installment of the infamous track system. It serves for |
---|
14 | temporary use only and is mainly a slimmed version of the old track. |
---|
15 | |
---|
16 | The track is used to steer the spaceship. In this case the track will have |
---|
17 | to control a PNode. The spaceship will be able to fly around this node. |
---|
18 | The camera will always be focused on that point. |
---|
19 | |
---|
20 | As we do this we have exactly the verticalscroller feeling we want. |
---|
21 | |
---|
22 | main-programmer: Benjamin Knecht |
---|
23 | */ |
---|
24 | |
---|
25 | #include "util/loading/load_param.h" |
---|
26 | #include "track/track.h" |
---|
27 | |
---|
28 | #include "p_node.h" |
---|
29 | |
---|
30 | #include "stdincl.h" |
---|
31 | |
---|
32 | #include "debug.h" |
---|
33 | |
---|
34 | ObjectListDefinition(Track); |
---|
35 | // CREATE_FACTORY(Track); |
---|
36 | |
---|
37 | |
---|
38 | /** |
---|
39 | * standard constructor |
---|
40 | */ |
---|
41 | Track::Track() |
---|
42 | { |
---|
43 | this->init(); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | /** |
---|
48 | * this is a constructor for use with the xml loading file |
---|
49 | * @param root xml root element for this object |
---|
50 | */ |
---|
51 | Track::Track(const TiXmlElement* root) |
---|
52 | { |
---|
53 | this->init(); |
---|
54 | |
---|
55 | if (root != NULL) |
---|
56 | this->loadParams(root); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | /** |
---|
61 | * initializes this class |
---|
62 | */ |
---|
63 | void Track::init() |
---|
64 | { |
---|
65 | this->curveType = CURVE_BEZIER; |
---|
66 | // this->startingTime = 0; |
---|
67 | this->duration = 20; |
---|
68 | this->endTime = 20; |
---|
69 | this->width = 10; |
---|
70 | this->curve = new BezierCurve(); |
---|
71 | this->trackNode = new PNode(PNode::getNullParent(), PNODE_ALL); |
---|
72 | this->nodeCount = 0; |
---|
73 | this->localTime = 0; |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * standard destructor |
---|
78 | */ |
---|
79 | Track::~Track() |
---|
80 | { |
---|
81 | |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | void Track::loadParams(const TiXmlElement* root) |
---|
86 | { |
---|
87 | LOAD_PARAM_START_CYCLE(root, element); |
---|
88 | { |
---|
89 | LoadParam_CYCLE(element, "addPoint", this, Track, addPoint) |
---|
90 | .describe("Adds a new Point to the currently selected TrackElement"); |
---|
91 | LoadParam_CYCLE(element, "speed", this, Track, setSpeed) |
---|
92 | .describe("Sets speed of traveling"); |
---|
93 | LoadParam_CYCLE(element, "mode", this, Track, setMode) |
---|
94 | .describe("Sets mode of track behavior"); |
---|
95 | |
---|
96 | } |
---|
97 | LOAD_PARAM_END_CYCLE(element); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | /** |
---|
103 | * This function adds a point with its coordinates to the track |
---|
104 | * @param x |
---|
105 | * @param y |
---|
106 | * @param z |
---|
107 | */ |
---|
108 | void Track::addPoint(float x, float y, float z) |
---|
109 | { |
---|
110 | this->addPointV(Vector (x,y,z)); |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | /** |
---|
115 | * This function adds a point to the track as a vector |
---|
116 | * @param newPoint |
---|
117 | */ |
---|
118 | void Track::addPointV(Vector newPoint) |
---|
119 | { |
---|
120 | this->curve->addNode(newPoint); |
---|
121 | if( this->nodeCount == 0) this->trackNode->setAbsCoor(newPoint); |
---|
122 | this->nodeCount++; |
---|
123 | // PRINTF(0)("Point added to curve\n"); |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * This function sets the speed of the trackNode by altering the duration |
---|
128 | * of the time the trackNode travels on the whole track. This is bad because |
---|
129 | * the speed depends on the length of the curve. (by getting the curve's length |
---|
130 | * this function will make a lot more sense) |
---|
131 | */ |
---|
132 | void Track::setSpeed(float speed) |
---|
133 | { |
---|
134 | this->duration = this->duration/speed; |
---|
135 | |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Sets the mode of the track. 0 means wait at the end. 1 means rerun track |
---|
140 | */ |
---|
141 | void Track::setMode(int newMode) |
---|
142 | { |
---|
143 | this->mode = newMode; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * We probably doesn't even need this |
---|
148 | */ |
---|
149 | //void Track::finalize() |
---|
150 | //{ |
---|
151 | // for (int i = 1; i<= trackElemCount ;i++) |
---|
152 | // { |
---|
153 | // TrackElement* tmpElem = this->firstTrackElem->findByID(i); |
---|
154 | // if( tmpElem->childCount > 0) |
---|
155 | // { |
---|
156 | // tIterator<TrackElement>* iterator = tmpElem->children->getIterator(); |
---|
157 | // TrackElement* enumElem = iterator->firstElement(); |
---|
158 | // //TrackElement* enumElem = tmpElem->children->enumerate(); |
---|
159 | // while (enumElem) |
---|
160 | // { |
---|
161 | // |
---|
162 | // // c1-continuity |
---|
163 | // enumElem->curve->addNode(enumElem->curve->getNode(0) + |
---|
164 | // ((enumElem->curve->getNode(0) - |
---|
165 | // tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) |
---|
166 | // ),2); |
---|
167 | // enumElem->nodeCount++; |
---|
168 | // // c2-continuity |
---|
169 | // enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())- |
---|
170 | // tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 + |
---|
171 | // tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3); |
---|
172 | // enumElem->nodeCount++; |
---|
173 | // PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n %d-out: count: %d %f, %f, %f\n", |
---|
174 | // tmpElem->ID, tmpElem->nodeCount, |
---|
175 | // tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z, |
---|
176 | // enumElem->ID, enumElem->nodeCount, |
---|
177 | // enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z); |
---|
178 | // |
---|
179 | // enumElem = iterator->nextElement(); |
---|
180 | // } |
---|
181 | // delete iterator; |
---|
182 | // } |
---|
183 | // } |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | /*for (int i = 1; i <= trackElemCount;i++) |
---|
188 | if (this->firstTrackElem->findByID(i)->endTime > this->maxTime) |
---|
189 | this->maxTime = this->firstTrackElem->findByID(i)->endTime; // very bad implemented :/ |
---|
190 | */ |
---|
191 | //} |
---|
192 | |
---|
193 | Vector Track::calcPos() const |
---|
194 | { |
---|
195 | return this->curve->calcPos(this->localTime/this->duration); |
---|
196 | } |
---|
197 | |
---|
198 | Vector Track::calcDir() const |
---|
199 | { |
---|
200 | return this->curve->calcDir(this->localTime/this->duration); |
---|
201 | } |
---|
202 | |
---|
203 | void Track::tick(float dt) |
---|
204 | { |
---|
205 | // PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt); |
---|
206 | // if (this->localTime <= this->firstTrackElem->duration) |
---|
207 | // this->jumpTo(this->localTime); |
---|
208 | // if (this->localTime <= this->maxTime) |
---|
209 | if(this->mode == 0) |
---|
210 | { |
---|
211 | if(this->localTime < this->duration) |
---|
212 | this->localTime += dt; |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | this->localTime += dt; |
---|
217 | if(this->localTime >= this->duration) |
---|
218 | this->localTime = 0; |
---|
219 | } |
---|
220 | |
---|
221 | // if (this->localTime > this->currentTrackElem->endTime |
---|
222 | // && this->currentTrackElem->children) |
---|
223 | // { |
---|
224 | // if (this->currentTrackElem->jumpTime != 0.0) |
---|
225 | // this->jumpTo(this->localTime + this->currentTrackElem->jumpTime); |
---|
226 | // // jump to the next TrackElement and also set the history of the new Element to the old one. |
---|
227 | // TrackElement* tmpHistoryElem = this->currentTrackElem; |
---|
228 | // this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem)); |
---|
229 | // this->currentTrackElem->history = tmpHistoryElem; |
---|
230 | // if (this->currentTrackElem->getName()) |
---|
231 | // { |
---|
232 | // this->trackText->setText(this->currentTrackElem->getName()); |
---|
233 | // this->textAnimation->replay(); |
---|
234 | // } |
---|
235 | // } |
---|
236 | if (this->trackNode) |
---|
237 | { |
---|
238 | Vector tmp = this->calcPos(); |
---|
239 | //Quaternion quat = Quaternion(this->calcDir(), Vector(this->curve->calcAcc(this->localTime/this->duration).x,1,this->curve->calcAcc(this->localTime/this->duration).z)); |
---|
240 | |
---|
241 | |
---|
242 | Vector v(0.0, 1.0, 0.0); |
---|
243 | Quaternion quat = Quaternion(this->calcDir(), v); |
---|
244 | Quaternion q(-PI/2, v); |
---|
245 | quat = quat * q; |
---|
246 | |
---|
247 | // move trackNode of the track |
---|
248 | this->trackNode->shiftCoor(tmp - this->trackNode->getAbsCoor()); |
---|
249 | // set direction and roll angle of trackNode |
---|
250 | this->trackNode->setAbsDir(quat); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * @returns the main TrackNode |
---|
256 | */ |
---|
257 | PNode* Track::getTrackNode() |
---|
258 | { |
---|
259 | return this->trackNode; |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | * Imports a model of the Graph into the OpenGL-environment. |
---|
264 | * @param dt The Iterator used in seconds for Painting the Graph. |
---|
265 | |
---|
266 | This is for testing facility only. Do this if you want to see the Path inside the Level. |
---|
267 | eventually this will all be packed into a gl-list. |
---|
268 | */ |
---|
269 | void Track::drawGraph(float dt) const |
---|
270 | { |
---|
271 | glMatrixMode(GL_MODELVIEW); |
---|
272 | glPushMatrix(); |
---|
273 | |
---|
274 | glPushAttrib(GL_ENABLE_BIT); |
---|
275 | |
---|
276 | glDisable(GL_LIGHTING); |
---|
277 | glDisable(GL_TEXTURE_2D); |
---|
278 | glDisable(GL_BLEND); |
---|
279 | glLineWidth(2.0); |
---|
280 | |
---|
281 | |
---|
282 | |
---|
283 | PNode* node = PNode::getNullParent(); |
---|
284 | glTranslatef (node->getAbsCoor ().x, |
---|
285 | node->getAbsCoor ().y, |
---|
286 | node->getAbsCoor ().z); |
---|
287 | Vector tmpRot = node->getAbsDir().getSpacialAxis(); |
---|
288 | glRotatef (node->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
289 | |
---|
290 | |
---|
291 | glBegin(GL_LINE_STRIP); |
---|
292 | glColor3f(1.0, 1.0, 0.6); |
---|
293 | |
---|
294 | Vector tmpVector; |
---|
295 | for(float f = 0.0; f < 1.0; f+=dt) |
---|
296 | { |
---|
297 | //PRINTF(0)("drawing",this->calcPos().x, this->calcPos().y, this->calcPos().z); |
---|
298 | tmpVector = this->curve->calcPos(f); |
---|
299 | glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z); |
---|
300 | } |
---|
301 | glEnd(); |
---|
302 | |
---|
303 | glPopAttrib(); |
---|
304 | |
---|
305 | glPopMatrix(); |
---|
306 | } |
---|