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 | \todo Null-Parent => center of the coord system - singleton |
---|
18 | \todo Smooth-Parent: delay, speed |
---|
19 | \todo destroy the stuff again, delete... |
---|
20 | */ |
---|
21 | |
---|
22 | |
---|
23 | #include "p_node.h" |
---|
24 | |
---|
25 | |
---|
26 | using namespace std; |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | \brief standard constructor |
---|
31 | |
---|
32 | \todo this constructor is not jet implemented - do it |
---|
33 | */ |
---|
34 | PNode::PNode () |
---|
35 | { |
---|
36 | this->children = new tList<PNode>(); |
---|
37 | this->bRelCoorChanged = true; |
---|
38 | this->bAbsCoorChanged = false; |
---|
39 | this->bRelDirChanged = true; |
---|
40 | this->bAbsDirChanged = false; |
---|
41 | this->parent = NULL; |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | /** |
---|
46 | \brief constructor with coodinates |
---|
47 | */ |
---|
48 | PNode::PNode (Vector* absCoordinate, PNode* parent ) |
---|
49 | { |
---|
50 | this->absCoordinate = *absCoordinate; |
---|
51 | this->relCoordinate = this->absCoordinate - parent->getAbsCoor (); |
---|
52 | |
---|
53 | this->children = new tList<PNode>(); |
---|
54 | this->bRelCoorChanged = true; |
---|
55 | this->bAbsCoorChanged = false; |
---|
56 | this->bRelDirChanged = true; |
---|
57 | this->bAbsDirChanged = false; |
---|
58 | this->parent = parent; |
---|
59 | |
---|
60 | parent->addChild (this); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | /** |
---|
65 | \brief standard deconstructor |
---|
66 | |
---|
67 | \todo this deconstructor is not jet implemented - do it |
---|
68 | */ |
---|
69 | PNode::~PNode () |
---|
70 | { |
---|
71 | /* |
---|
72 | delete &this->children; |
---|
73 | delete &this->relCoordinate; |
---|
74 | delete &this->absCoordinate; |
---|
75 | delete &this->relDirection; |
---|
76 | delete &this->absDirection; |
---|
77 | */ |
---|
78 | this->parent = NULL; |
---|
79 | /* there is currently a problem with cleaning up - fix*/ |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | /** |
---|
84 | \brief deletes the hole pnode tree |
---|
85 | |
---|
86 | cleans up all pnodes |
---|
87 | */ |
---|
88 | void PNode::destroy () |
---|
89 | { |
---|
90 | PNode* pn = this->children->enumerate(); |
---|
91 | while( pn != NULL) |
---|
92 | { |
---|
93 | pn->destroy (); |
---|
94 | pn = this->children->nextElement(); |
---|
95 | } |
---|
96 | this->children->destroy (); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | /** |
---|
101 | \brief get relative coordinates |
---|
102 | \returns relative coordinates to its parent |
---|
103 | */ |
---|
104 | Vector PNode::getRelCoor () |
---|
105 | { |
---|
106 | Vector r = this->relCoordinate; /* return a copy, so it can't be modified */ |
---|
107 | return r; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | /** |
---|
112 | \brief set relative coordinates |
---|
113 | \param relative coordinates to its parent |
---|
114 | |
---|
115 | it is very importand, that you use this function, if you want to update the |
---|
116 | relCoordinates. If you don't use this, the PNode won't recognize, that something |
---|
117 | has changed and won't update the children Nodes. |
---|
118 | */ |
---|
119 | void PNode::setRelCoor (Vector* relCoord) |
---|
120 | { |
---|
121 | this->bRelCoorChanged = true; |
---|
122 | this->relCoordinate = *relCoord; |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | /** |
---|
127 | \brief get absolute coordinates |
---|
128 | \returns absolute coordinates from (0,0,0) |
---|
129 | */ |
---|
130 | Vector PNode::getAbsCoor () |
---|
131 | { |
---|
132 | return this->absCoordinate; |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | /** |
---|
137 | \brief get relative coordinates |
---|
138 | \returns relative coordinates to its parent |
---|
139 | |
---|
140 | it is very importand, that you use this function, if you want to update the |
---|
141 | absCoordinates. If you don't use this, the PNode won't recognize, that something |
---|
142 | has changed and won't update the children Nodes. |
---|
143 | */ |
---|
144 | void PNode::setAbsCoor (Vector* absCoord) |
---|
145 | { |
---|
146 | this->bAbsCoorChanged = true; |
---|
147 | this->absCoordinate = *absCoord; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | /** |
---|
152 | \brief shift coordinate (abs and rel) |
---|
153 | \param shift vector |
---|
154 | |
---|
155 | this function shifts the current coordinates about the vector shift. this is |
---|
156 | usefull because from some place else you can: |
---|
157 | PNode* someNode = ...; |
---|
158 | Vector objectMovement = calculateShift(); |
---|
159 | someNode->shiftCoor(objectMovement); |
---|
160 | |
---|
161 | elsewhere you would have to: |
---|
162 | PNode* someNode = ...; |
---|
163 | Vector objectMovement = calculateShift(); |
---|
164 | Vector currentCoor = someNode->getRelCoor(); |
---|
165 | Vector newCoor = currentCoor + objectMovement; |
---|
166 | someNode->setRelCoor(newCoor); |
---|
167 | |
---|
168 | yea right... shorter... |
---|
169 | |
---|
170 | */ |
---|
171 | void PNode::shiftCoor (Vector* shift) |
---|
172 | { |
---|
173 | if( this->bAbsCoorChanged) |
---|
174 | { |
---|
175 | this->absCoordinate = this->absCoordinate + *shift; |
---|
176 | } |
---|
177 | else |
---|
178 | { |
---|
179 | this->relCoordinate = this->relCoordinate + *shift; |
---|
180 | this->bRelCoorChanged = true; |
---|
181 | } |
---|
182 | |
---|
183 | printf("PNode::shiftCoor() - relCoord: (%f, %f, %f)\n", |
---|
184 | this->relCoordinate.x, |
---|
185 | this->relCoordinate.y, |
---|
186 | this->relCoordinate.z); |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | /** |
---|
192 | \brief get relative direction |
---|
193 | \returns relative direction to its parent |
---|
194 | */ |
---|
195 | Quaternion PNode::getRelDir () |
---|
196 | { |
---|
197 | return this->relDirection; |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | /** |
---|
202 | \brief set relative direction |
---|
203 | \param relative direction to its parent |
---|
204 | |
---|
205 | it is very importand, that you use this function, if you want to update the |
---|
206 | relDirection. If you don't use this, the PNode won't recognize, that something |
---|
207 | has changed and won't update the children Nodes. |
---|
208 | */ |
---|
209 | void PNode::setRelDir (Quaternion* relDir) |
---|
210 | { |
---|
211 | this->bRelCoorChanged = true; |
---|
212 | this->relDirection = *relDir; |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | /** |
---|
217 | \brief gets the absolute direction (0,0,1) |
---|
218 | \returns absolute coordinates |
---|
219 | */ |
---|
220 | Quaternion PNode::getAbsDir () |
---|
221 | {} |
---|
222 | |
---|
223 | |
---|
224 | /** |
---|
225 | \brief sets the absolute direction (0,0,1) |
---|
226 | \param absolute coordinates |
---|
227 | |
---|
228 | it is very importand, that you use this function, if you want to update the |
---|
229 | absDirection. If you don't use this, the PNode won't recognize, that something |
---|
230 | has changed and won't update the children Nodes. |
---|
231 | */ |
---|
232 | void PNode::setAbsDir (Quaternion* absDir) |
---|
233 | {} |
---|
234 | |
---|
235 | |
---|
236 | /** |
---|
237 | \brief shift coordinate (abs and rel) |
---|
238 | \param shift vector |
---|
239 | |
---|
240 | this function shifts the current coordinates about the vector shift. this is |
---|
241 | usefull because from some place else you can: |
---|
242 | PNode* someNode = ...; |
---|
243 | Quaternion objectMovement = calculateShift(); |
---|
244 | someNode->shiftCoor(objectMovement); |
---|
245 | |
---|
246 | elsewhere you would have to: |
---|
247 | PNode* someNode = ...; |
---|
248 | Quaternion objectMovement = calculateShift(); |
---|
249 | Quaternion currentCoor = someNode->getRelCoor(); |
---|
250 | Quaternion newCoor = currentCoor + objectMovement; |
---|
251 | someNode->setRelCoor(newCoor); |
---|
252 | |
---|
253 | yea right... shorter... |
---|
254 | |
---|
255 | */ |
---|
256 | void PNode::shiftDir (Quaternion* shift) |
---|
257 | {} |
---|
258 | |
---|
259 | |
---|
260 | |
---|
261 | /** |
---|
262 | \brief adds a child and makes this node to a parent |
---|
263 | \param child reference |
---|
264 | |
---|
265 | use this to add a child to this node. |
---|
266 | */ |
---|
267 | void PNode::addChild (PNode* pNode) |
---|
268 | { |
---|
269 | this->addChild(pNode, DEFAULT_MODE); |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | /** |
---|
274 | \brief adds a child and makes this node to a parent |
---|
275 | \param child reference |
---|
276 | \param on which changes the child should also change ist state |
---|
277 | |
---|
278 | use this to add a child to this node. |
---|
279 | */ |
---|
280 | void PNode::addChild (PNode* pNode, parentingMode mode) |
---|
281 | { |
---|
282 | pNode->mode = mode; |
---|
283 | pNode->parent = this; |
---|
284 | this->children->add (pNode); |
---|
285 | } |
---|
286 | |
---|
287 | |
---|
288 | /** |
---|
289 | /brief removes a child from the node |
---|
290 | */ |
---|
291 | void PNode::removeChild (PNode* pNode) |
---|
292 | { |
---|
293 | this->children->remove (pNode); |
---|
294 | } |
---|
295 | |
---|
296 | |
---|
297 | /** |
---|
298 | \brief sets the parent of this PNode |
---|
299 | */ |
---|
300 | void PNode::setParent (PNode* parent) |
---|
301 | { |
---|
302 | this->parent = parent; |
---|
303 | } |
---|
304 | |
---|
305 | /** |
---|
306 | \brief set the mode of this parent manualy |
---|
307 | */ |
---|
308 | void PNode::setMode (parentingMode mode) |
---|
309 | { |
---|
310 | this->mode = mode; |
---|
311 | } |
---|
312 | |
---|
313 | /** |
---|
314 | \brief has to be called, if the parent coordinate has changed |
---|
315 | |
---|
316 | normaly this will be done by the parent itself automaticaly. If you call this, you |
---|
317 | will force an update of the coordinated of the node. |
---|
318 | */ |
---|
319 | void PNode::parentCoorChanged () |
---|
320 | { |
---|
321 | this->bRelCoorChanged = true; |
---|
322 | } |
---|
323 | |
---|
324 | |
---|
325 | /** |
---|
326 | \brief has to be called, if the parent direction has changed |
---|
327 | |
---|
328 | normaly this will be done by the parent itself automaticaly. If you call this, you |
---|
329 | will force an update of the direction of the node. |
---|
330 | */ |
---|
331 | void PNode::parentDirChanged () |
---|
332 | { |
---|
333 | this->bRelDirChanged = true; |
---|
334 | } |
---|
335 | |
---|
336 | |
---|
337 | /** |
---|
338 | \brief updates the absCoordinate/absDirection |
---|
339 | |
---|
340 | this is used to go through the parent-tree to update all the absolute coordinates |
---|
341 | and directions. this update should be done by the engine, so you don't have to |
---|
342 | worry, normaly... |
---|
343 | */ |
---|
344 | void PNode::update (long timeStamp) |
---|
345 | { |
---|
346 | |
---|
347 | if( this->mode == MOVEMENT || this->mode == ALL) |
---|
348 | { |
---|
349 | if( this->bAbsCoorChanged /*&& this->timeStamp != DataTank::timeStamp*/) |
---|
350 | { |
---|
351 | printf("PNode::update () - this->bAbsCoorChanged = true\n"); |
---|
352 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
353 | this->relCoordinate = this->absCoordinate - parent->getAbsCoor (); |
---|
354 | } |
---|
355 | else if( this->bRelCoorChanged /*&& this->timeStamp != DataTank::timeStamp*/) |
---|
356 | { |
---|
357 | /*this is bad style... must be deleted later - just for testing*/ |
---|
358 | if( this->parent == NULL) |
---|
359 | { |
---|
360 | this->absCoordinate = this->relCoordinate; |
---|
361 | } |
---|
362 | else |
---|
363 | this->absCoordinate = parent->getAbsCoor () + this->relCoordinate; /* update the current absCoordinate */ |
---|
364 | } |
---|
365 | } |
---|
366 | |
---|
367 | if( this->mode == ROTATION && this->mode == ALL) |
---|
368 | { |
---|
369 | if( this->bAbsDirChanged /*&& this->timeStamp != DataTank::timeStamp*/) |
---|
370 | { |
---|
371 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
372 | this->relDirection = this->absDirection - parent->getAbsDir (); |
---|
373 | } |
---|
374 | else if( this->bRelDirChanged /*&& this->timeStamp != DataTank::timeStamp*/) |
---|
375 | { |
---|
376 | /* update the current absDirection - remember * means rotation around sth.*/ |
---|
377 | this->absDirection = parent->getAbsDir () * this->relDirection; |
---|
378 | } |
---|
379 | } |
---|
380 | // } |
---|
381 | PNode* pn = this->children->enumerate(); |
---|
382 | while( pn != NULL) |
---|
383 | { |
---|
384 | /* if this node has changed, make sure, that all children are updated also */ |
---|
385 | if( this->bRelCoorChanged || this->bAbsCoorChanged) |
---|
386 | pn->parentCoorChanged (); |
---|
387 | if( this->bRelDirChanged || this->bAbsDirChanged) |
---|
388 | pn->parentDirChanged (); |
---|
389 | pn->update(timeStamp); |
---|
390 | pn = this->children->nextElement(); |
---|
391 | } |
---|
392 | |
---|
393 | this->timeStamp = timeStamp; |
---|
394 | this->bRelCoorChanged = false; |
---|
395 | this->bAbsCoorChanged = false; |
---|
396 | this->bRelDirChanged = false; |
---|
397 | this->bAbsDirChanged = false; |
---|
398 | } |
---|
399 | |
---|
400 | |
---|
401 | void PNode::debug() |
---|
402 | { |
---|
403 | printf("PNode::debug() - absCoord: (%f, %f, %f)\n", |
---|
404 | this->absCoordinate.x, |
---|
405 | this->absCoordinate.y, |
---|
406 | this->absCoordinate.z); |
---|
407 | } |
---|