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 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PNODE |
---|
17 | |
---|
18 | #include "p_node.h" |
---|
19 | #include "null_parent.h" |
---|
20 | |
---|
21 | #include "load_param.h" |
---|
22 | #include "class_list.h" |
---|
23 | |
---|
24 | #include "stdincl.h" |
---|
25 | #include "compiler.h" |
---|
26 | #include "error.h" |
---|
27 | #include "debug.h" |
---|
28 | #include "vector.h" |
---|
29 | |
---|
30 | #include "color.h" |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | |
---|
35 | /** |
---|
36 | * @brief standard constructor |
---|
37 | */ |
---|
38 | PNode::PNode () |
---|
39 | { |
---|
40 | init(NULL); |
---|
41 | |
---|
42 | NullParent::getInstance()->addChild(this); |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * @param root the load-Element for the PNode |
---|
47 | */ |
---|
48 | PNode::PNode(const TiXmlElement* root) |
---|
49 | { |
---|
50 | this->init(NULL); |
---|
51 | this->loadParams(root); |
---|
52 | |
---|
53 | if (this->parent == NULL) |
---|
54 | NullParent::getInstance()->addChild(this); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * @brief constructor with coodinates |
---|
59 | * @param absCoordinate the Absolute coordinate of the Object |
---|
60 | * @param parent The parent-node of this node. |
---|
61 | */ |
---|
62 | PNode::PNode (const Vector& absCoor, PNode* parent ) |
---|
63 | { |
---|
64 | this->init(parent); |
---|
65 | |
---|
66 | if (likely(parent != NULL)) |
---|
67 | parent->addChild (this); |
---|
68 | |
---|
69 | this->setAbsCoor(absCoor); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * @brief standard deconstructor |
---|
74 | * |
---|
75 | * There are two general ways to delete a PNode |
---|
76 | * 1. delete instance; |
---|
77 | * -> result |
---|
78 | * delete this Node and all its children and children's children... |
---|
79 | * (danger if you still need the children's instance somewhere else!!) |
---|
80 | * |
---|
81 | * 2. instance->remove2D(); delete instance; |
---|
82 | * -> result: |
---|
83 | * moves its children to the NullParent |
---|
84 | * then deletes the Element. |
---|
85 | */ |
---|
86 | PNode::~PNode () |
---|
87 | { |
---|
88 | // remove the Node, delete it's children. |
---|
89 | PNode* tmp; |
---|
90 | while (this->children.size() > 0) |
---|
91 | { |
---|
92 | tmp = this->children.front(); |
---|
93 | this->children.pop_front(); |
---|
94 | delete tmp; |
---|
95 | } |
---|
96 | if (this->parent != NULL) |
---|
97 | { |
---|
98 | this->parent->children.remove(this); |
---|
99 | this->parent = NULL; |
---|
100 | } |
---|
101 | |
---|
102 | // remove all other allocated memory. |
---|
103 | if (this->toCoordinate != NULL) |
---|
104 | delete this->toCoordinate; |
---|
105 | if (this->toDirection != NULL) |
---|
106 | delete this->toDirection; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | /** |
---|
111 | * @brief initializes a PNode |
---|
112 | * @param parent the parent for this PNode |
---|
113 | */ |
---|
114 | void PNode::init(PNode* parent) |
---|
115 | { |
---|
116 | this->setClassID(CL_PARENT_NODE, "PNode"); |
---|
117 | |
---|
118 | this->bRelCoorChanged = true; |
---|
119 | this->bRelDirChanged = true; |
---|
120 | this->parent = parent; |
---|
121 | this->parentMode = PNODE_PARENT_MODE_DEFAULT; |
---|
122 | |
---|
123 | // iterators |
---|
124 | this->toCoordinate = NULL; |
---|
125 | this->toDirection = NULL; |
---|
126 | this->bias = 1.0; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * @brief loads parameters of the PNode |
---|
131 | * @param root the XML-element to load the properties of |
---|
132 | */ |
---|
133 | void PNode::loadParams(const TiXmlElement* root) |
---|
134 | { |
---|
135 | static_cast<BaseObject*>(this)->loadParams(root); |
---|
136 | |
---|
137 | LoadParam(root, "rel-coor", this, PNode, setRelCoor) |
---|
138 | .describe("Sets The relative position of the Node to its parent."); |
---|
139 | |
---|
140 | LoadParam(root, "abs-coor", this, PNode, setAbsCoor) |
---|
141 | .describe("Sets The absolute Position of the Node."); |
---|
142 | |
---|
143 | LoadParam(root, "rel-dir", this, PNode, setRelDir) |
---|
144 | .describe("Sets The relative rotation of the Node to its parent."); |
---|
145 | |
---|
146 | LoadParam(root, "abs-dir", this, PNode, setAbsDir) |
---|
147 | .describe("Sets The absolute rotation of the Node."); |
---|
148 | |
---|
149 | LoadParam(root, "parent", this, PNode, setParent) |
---|
150 | .describe("the Name of the Parent of this PNode"); |
---|
151 | |
---|
152 | LoadParam(root, "parent-mode", this, PNode, setParentMode) |
---|
153 | .describe("the mode to connect this node to its parent ()"); |
---|
154 | |
---|
155 | // cycling properties |
---|
156 | if (root != NULL) |
---|
157 | { |
---|
158 | LOAD_PARAM_START_CYCLE(root, element); |
---|
159 | { |
---|
160 | LoadParam_CYCLE(element, "parent", this, PNode, addChild) |
---|
161 | .describe("adds a new Child to the current Node."); |
---|
162 | |
---|
163 | } |
---|
164 | LOAD_PARAM_END_CYCLE(element); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | /** |
---|
169 | * @brief set relative coordinates |
---|
170 | * @param relCoord relative coordinates to its parent |
---|
171 | * |
---|
172 | * |
---|
173 | * it is very importand, that you use this function, if you want to update the |
---|
174 | * relCoordinates. If you don't use this, the PNode won't recognize, that something |
---|
175 | * has changed and won't update the children Nodes. |
---|
176 | */ |
---|
177 | void PNode::setRelCoor (const Vector& relCoord) |
---|
178 | { |
---|
179 | if (this->toCoordinate!= NULL) |
---|
180 | { |
---|
181 | delete this->toCoordinate; |
---|
182 | this->toCoordinate = NULL; |
---|
183 | } |
---|
184 | |
---|
185 | this->relCoordinate = relCoord; |
---|
186 | this->bRelCoorChanged = true; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * @brief set relative coordinates |
---|
191 | * @param x x-relative coordinates to its parent |
---|
192 | * @param y y-relative coordinates to its parent |
---|
193 | * @param z z-relative coordinates to its parent |
---|
194 | * @see void PNode::setRelCoor (const Vector& relCoord) |
---|
195 | */ |
---|
196 | void PNode::setRelCoor (float x, float y, float z) |
---|
197 | { |
---|
198 | this->setRelCoor(Vector(x, y, z)); |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * @brief sets a new relative position smoothely |
---|
203 | * @param relCoordSoft the new Position to iterate to |
---|
204 | * @param bias how fast to iterate to this position |
---|
205 | */ |
---|
206 | void PNode::setRelCoorSoft(const Vector& relCoordSoft, float bias) |
---|
207 | { |
---|
208 | if (likely(this->toCoordinate == NULL)) |
---|
209 | this->toCoordinate = new Vector(); |
---|
210 | |
---|
211 | *this->toCoordinate = relCoordSoft; |
---|
212 | this->bias = bias; |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | /** |
---|
217 | * @brief set relative coordinates smoothely |
---|
218 | * @param x x-relative coordinates to its parent |
---|
219 | * @param y y-relative coordinates to its parent |
---|
220 | * @param z z-relative coordinates to its parent |
---|
221 | * @see void PNode::setRelCoorSoft (const Vector&, float) |
---|
222 | */ |
---|
223 | void PNode::setRelCoorSoft (float x, float y, float z, float bias) |
---|
224 | { |
---|
225 | this->setRelCoorSoft(Vector(x, y, z), bias); |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | /** |
---|
230 | * @param absCoord set absolute coordinate |
---|
231 | */ |
---|
232 | void PNode::setAbsCoor (const Vector& absCoord) |
---|
233 | { |
---|
234 | if (this->toCoordinate!= NULL) |
---|
235 | { |
---|
236 | delete this->toCoordinate; |
---|
237 | this->toCoordinate = NULL; |
---|
238 | } |
---|
239 | |
---|
240 | if( likely(this->parentMode & PNODE_MOVEMENT)) |
---|
241 | { |
---|
242 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
243 | if (likely(this->parent != NULL)) |
---|
244 | this->relCoordinate = absCoord - parent->getAbsCoor (); |
---|
245 | else |
---|
246 | this->relCoordinate = absCoord; |
---|
247 | } |
---|
248 | if( this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
249 | { |
---|
250 | if (likely(this->parent != NULL)) |
---|
251 | this->relCoordinate = absCoord - parent->getAbsCoor (); |
---|
252 | else |
---|
253 | this->relCoordinate = absCoord; |
---|
254 | } |
---|
255 | |
---|
256 | this->bRelCoorChanged = true; |
---|
257 | // this->absCoordinate = absCoord; |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | /** |
---|
262 | * @param x x-coordinate. |
---|
263 | * @param y y-coordinate. |
---|
264 | * @param z z-coordinate. |
---|
265 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
---|
266 | */ |
---|
267 | void PNode::setAbsCoor(float x, float y, float z) |
---|
268 | { |
---|
269 | this->setAbsCoor(Vector(x, y, z)); |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | * @param absCoord set absolute coordinate |
---|
274 | * @todo check off |
---|
275 | */ |
---|
276 | void PNode::setAbsCoorSoft (const Vector& absCoordSoft, float bias) |
---|
277 | { |
---|
278 | if (this->toCoordinate == NULL) |
---|
279 | this->toCoordinate = new Vector; |
---|
280 | |
---|
281 | if( likely(this->parentMode & PNODE_MOVEMENT)) |
---|
282 | { |
---|
283 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
284 | if (likely(this->parent != NULL)) |
---|
285 | *this->toCoordinate = absCoordSoft - parent->getAbsCoor (); |
---|
286 | else |
---|
287 | *this->toCoordinate = absCoordSoft; |
---|
288 | } |
---|
289 | if( this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
290 | { |
---|
291 | if (likely(this->parent != NULL)) |
---|
292 | *this->toCoordinate = absCoordSoft - parent->getAbsCoor (); |
---|
293 | else |
---|
294 | *this->toCoordinate = absCoordSoft; |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | |
---|
299 | /** |
---|
300 | * @brief shift coordinate relative |
---|
301 | * @param shift shift vector |
---|
302 | * |
---|
303 | * this function shifts the current coordinates about the vector shift. this is |
---|
304 | * usefull because from some place else you can: |
---|
305 | * PNode* someNode = ...; |
---|
306 | * Vector objectMovement = calculateShift(); |
---|
307 | * someNode->shiftCoor(objectMovement); |
---|
308 | * |
---|
309 | * this is the internal method of: |
---|
310 | * PNode* someNode = ...; |
---|
311 | * Vector objectMovement = calculateShift(); |
---|
312 | * Vector currentCoor = someNode->getRelCoor(); |
---|
313 | * Vector newCoor = currentCoor + objectMovement; |
---|
314 | * someNode->setRelCoor(newCoor); |
---|
315 | * |
---|
316 | */ |
---|
317 | void PNode::shiftCoor (const Vector& shift) |
---|
318 | { |
---|
319 | this->relCoordinate += shift; |
---|
320 | this->bRelCoorChanged = true; |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | * @brief set relative direction |
---|
325 | * @param relDir to its parent |
---|
326 | */ |
---|
327 | void PNode::setRelDir (const Quaternion& relDir) |
---|
328 | { |
---|
329 | if (this->toDirection!= NULL) |
---|
330 | { |
---|
331 | delete this->toDirection; |
---|
332 | this->toDirection = NULL; |
---|
333 | } |
---|
334 | this->relDirection = relDir; |
---|
335 | |
---|
336 | this->bRelCoorChanged = true; |
---|
337 | } |
---|
338 | |
---|
339 | /** |
---|
340 | * @see void PNode::setRelDir (const Quaternion& relDir) |
---|
341 | * @param x the x direction |
---|
342 | * @param y the y direction |
---|
343 | * @param z the z direction |
---|
344 | * |
---|
345 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
346 | */ |
---|
347 | void PNode::setRelDir (float x, float y, float z) |
---|
348 | { |
---|
349 | this->setRelDir(Quaternion(Vector(x,y,z), Vector(0,1,0))); |
---|
350 | } |
---|
351 | |
---|
352 | |
---|
353 | /** |
---|
354 | * @brief sets the Relative Direction of this node to its parent in a Smoothed way |
---|
355 | * @param relDirSoft the direction to iterate to smoothely. |
---|
356 | * @param bias how fast to iterate to the new Direction |
---|
357 | */ |
---|
358 | void PNode::setRelDirSoft(const Quaternion& relDirSoft, float bias) |
---|
359 | { |
---|
360 | if (likely(this->toDirection == NULL)) |
---|
361 | this->toDirection = new Quaternion(); |
---|
362 | |
---|
363 | *this->toDirection = relDirSoft; |
---|
364 | this->bias = bias; |
---|
365 | this->bRelDirChanged = true; |
---|
366 | } |
---|
367 | |
---|
368 | /** |
---|
369 | * @see void PNode::setRelDirSoft (const Quaternion& relDir) |
---|
370 | * @param x the x direction |
---|
371 | * @param y the y direction |
---|
372 | * @param z the z direction |
---|
373 | * |
---|
374 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
375 | */ |
---|
376 | void PNode::setRelDirSoft(float x, float y, float z, float bias) |
---|
377 | { |
---|
378 | this->setRelDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias); |
---|
379 | } |
---|
380 | |
---|
381 | /** |
---|
382 | * @brief sets the absolute direction |
---|
383 | * @param absDir absolute coordinates |
---|
384 | */ |
---|
385 | void PNode::setAbsDir (const Quaternion& absDir) |
---|
386 | { |
---|
387 | if (this->toDirection!= NULL) |
---|
388 | { |
---|
389 | delete this->toDirection; |
---|
390 | this->toDirection = NULL; |
---|
391 | } |
---|
392 | |
---|
393 | if (likely(this->parent != NULL)) |
---|
394 | this->relDirection = absDir / this->parent->getAbsDir(); |
---|
395 | else |
---|
396 | this->relDirection = absDir; |
---|
397 | |
---|
398 | this->bRelDirChanged = true; |
---|
399 | } |
---|
400 | |
---|
401 | /** |
---|
402 | * @see void PNode::setAbsDir (const Quaternion& relDir) |
---|
403 | * @param x the x direction |
---|
404 | * @param y the y direction |
---|
405 | * @param z the z direction |
---|
406 | * |
---|
407 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
408 | */ |
---|
409 | void PNode::setAbsDir (float x, float y, float z) |
---|
410 | { |
---|
411 | this->setAbsDir(Quaternion(Vector(x,y,z), Vector(0,1,0))); |
---|
412 | } |
---|
413 | |
---|
414 | /** |
---|
415 | * @brief sets the absolute direction |
---|
416 | * @param absDir absolute coordinates |
---|
417 | * @param bias how fast to iterator to the new Position |
---|
418 | */ |
---|
419 | void PNode::setAbsDirSoft (const Quaternion& absDirSoft, float bias) |
---|
420 | { |
---|
421 | if (this->toDirection == NULL) |
---|
422 | this->toDirection = new Quaternion(); |
---|
423 | |
---|
424 | if (likely(this->parent != NULL)) |
---|
425 | *this->toDirection = absDirSoft / this->parent->getAbsDir(); |
---|
426 | else |
---|
427 | *this->toDirection = absDirSoft; |
---|
428 | |
---|
429 | this->bias = bias; |
---|
430 | this->bRelDirChanged = true; |
---|
431 | } |
---|
432 | |
---|
433 | /** |
---|
434 | * @see void PNode::setAbsDir (const Quaternion& relDir) |
---|
435 | * @param x the x direction |
---|
436 | * @param y the y direction |
---|
437 | * @param z the z direction |
---|
438 | * |
---|
439 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
440 | */ |
---|
441 | void PNode::setAbsDirSoft (float x, float y, float z, float bias) |
---|
442 | { |
---|
443 | this->setAbsDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias); |
---|
444 | } |
---|
445 | |
---|
446 | |
---|
447 | /** |
---|
448 | * @brief shift Direction |
---|
449 | * @param shift the direction around which to shift. |
---|
450 | */ |
---|
451 | void PNode::shiftDir (const Quaternion& shift) |
---|
452 | { |
---|
453 | this->relDirection = this->relDirection * shift; |
---|
454 | this->bRelDirChanged = true; |
---|
455 | } |
---|
456 | |
---|
457 | |
---|
458 | /** |
---|
459 | * @brief adds a child and makes this node to a parent |
---|
460 | * @param child child reference |
---|
461 | * use this to add a child to this node. |
---|
462 | */ |
---|
463 | void PNode::addChild (PNode* child) |
---|
464 | { |
---|
465 | if( likely(child->parent != NULL)) |
---|
466 | { |
---|
467 | PRINTF(5)("PNode::addChild() - reparenting node: removing it and adding it again\n"); |
---|
468 | child->parent->children.remove(child); |
---|
469 | } |
---|
470 | child->parent = this; |
---|
471 | if (unlikely(this != NULL)) |
---|
472 | this->children.push_back(child); |
---|
473 | child->parentCoorChanged(); |
---|
474 | } |
---|
475 | |
---|
476 | |
---|
477 | /** |
---|
478 | * @see PNode::addChild(PNode* child); |
---|
479 | * @param childName the name of the child to add to this PNode |
---|
480 | */ |
---|
481 | void PNode::addChild (const char* childName) |
---|
482 | { |
---|
483 | PNode* childNode = dynamic_cast<PNode*>(ClassList::getObject(childName, CL_PARENT_NODE)); |
---|
484 | if (childNode != NULL) |
---|
485 | this->addChild(childNode); |
---|
486 | } |
---|
487 | |
---|
488 | |
---|
489 | /** |
---|
490 | * @brief removes a child from the node |
---|
491 | * @param child the child to remove from this pNode. |
---|
492 | * |
---|
493 | * Children from pNode will not be lost, they are referenced to NullPointer |
---|
494 | */ |
---|
495 | void PNode::removeChild (PNode* child) |
---|
496 | { |
---|
497 | if (child != NULL) |
---|
498 | { |
---|
499 | child->removeNode(); |
---|
500 | // this->children->remove(child); |
---|
501 | // child->parent = NULL; |
---|
502 | } |
---|
503 | } |
---|
504 | |
---|
505 | /** |
---|
506 | * @brief remove this pnode from the tree and adds all following to NullParent |
---|
507 | * |
---|
508 | * this can be the case, if an entity in the world is being destroyed. |
---|
509 | */ |
---|
510 | void PNode::removeNode() |
---|
511 | { |
---|
512 | list<PNode*>::iterator child; |
---|
513 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
514 | NullParent::getInstance()->addChild(*child); |
---|
515 | |
---|
516 | if (this->parent != NULL) |
---|
517 | this->parent->children.remove(this); |
---|
518 | } |
---|
519 | |
---|
520 | |
---|
521 | /** |
---|
522 | * @see PNode::setParent(PNode* parent); |
---|
523 | * @param parentName the name of the Parent to set to this PNode |
---|
524 | */ |
---|
525 | void PNode::setParent (const char* parentName) |
---|
526 | { |
---|
527 | PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE)); |
---|
528 | if (parentNode != NULL) |
---|
529 | parentNode->addChild(this); |
---|
530 | } |
---|
531 | |
---|
532 | /** |
---|
533 | * @brief does the reparenting in a very smooth way |
---|
534 | * @param parentNode the new Node to connect this node to. |
---|
535 | * @param bias the speed to iterate to this new Positions |
---|
536 | */ |
---|
537 | void PNode::setParentSoft(PNode* parentNode, float bias) |
---|
538 | { |
---|
539 | // return if the new parent and the old one match |
---|
540 | if (this->parent == parentNode) |
---|
541 | return; |
---|
542 | |
---|
543 | // store the Valures to iterate to. |
---|
544 | if (likely(this->toCoordinate == NULL)) |
---|
545 | { |
---|
546 | this->toCoordinate = new Vector(); |
---|
547 | *this->toCoordinate = this->getRelCoor(); |
---|
548 | } |
---|
549 | if (likely(this->toDirection == NULL)) |
---|
550 | { |
---|
551 | this->toDirection = new Quaternion(); |
---|
552 | *this->toDirection = this->getRelDir(); |
---|
553 | } |
---|
554 | this->bias = bias; |
---|
555 | |
---|
556 | |
---|
557 | Vector tmpV = this->getAbsCoor(); |
---|
558 | Quaternion tmpQ = this->getAbsDir(); |
---|
559 | |
---|
560 | parentNode->addChild(this); |
---|
561 | |
---|
562 | if (this->parentMode & PNODE_ROTATE_MOVEMENT && this->parent != NULL) |
---|
563 | this->relCoordinate = this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor()); |
---|
564 | else |
---|
565 | this->relCoordinate = tmpV - parentNode->getAbsCoor(); |
---|
566 | |
---|
567 | this->relDirection = tmpQ / parentNode->getAbsDir(); |
---|
568 | } |
---|
569 | |
---|
570 | /** |
---|
571 | * @brief does the reparenting in a very smooth way |
---|
572 | * @param parentName the name of the Parent to reconnect to |
---|
573 | * @param bias the speed to iterate to this new Positions |
---|
574 | */ |
---|
575 | void PNode::setParentSoft(const char* parentName, float bias) |
---|
576 | { |
---|
577 | PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE)); |
---|
578 | if (parentNode != NULL) |
---|
579 | this->setParentSoft(parentNode, bias); |
---|
580 | } |
---|
581 | |
---|
582 | |
---|
583 | /** |
---|
584 | * @brief sets the mode of this parent manually |
---|
585 | * @param parentMode a String representing this parentingMode |
---|
586 | */ |
---|
587 | void PNode::setParentMode (const char* parentingMode) |
---|
588 | { |
---|
589 | this->setParentMode(PNode::charToParentingMode(parentingMode)); |
---|
590 | } |
---|
591 | |
---|
592 | /** |
---|
593 | * @brief updates the absCoordinate/absDirection |
---|
594 | * @param dt The time passed since the last update |
---|
595 | * |
---|
596 | * this is used to go through the parent-tree to update all the absolute coordinates |
---|
597 | * and directions. this update should be done by the engine, so you don't have to |
---|
598 | * worry, normaly... |
---|
599 | */ |
---|
600 | void PNode::updateNode (float dt) |
---|
601 | { |
---|
602 | if( likely(this->parent != NULL)) |
---|
603 | { |
---|
604 | // movement for nodes with smoothMove enabled |
---|
605 | if (unlikely(this->toCoordinate != NULL)) |
---|
606 | { |
---|
607 | Vector moveVect = (*this->toCoordinate - this->relCoordinate) *fabsf(dt)*bias; |
---|
608 | if (likely(moveVect.len() >= PNODE_ITERATION_DELTA)) |
---|
609 | { |
---|
610 | this->shiftCoor(moveVect); |
---|
611 | } |
---|
612 | else |
---|
613 | { |
---|
614 | delete this->toCoordinate; |
---|
615 | this->toCoordinate = NULL; |
---|
616 | PRINTF(5)("SmoothMove of %s finished\n", this->getName()); |
---|
617 | } |
---|
618 | } |
---|
619 | if (unlikely(this->toDirection != NULL)) |
---|
620 | { |
---|
621 | Quaternion rotQuat = Quaternion::quatSlerp(this->relDirection,*this->toDirection, fabsf(dt)*this->bias); |
---|
622 | if (this->relDirection.distance(rotQuat) >PNODE_ITERATION_DELTA) |
---|
623 | { |
---|
624 | this->relDirection = rotQuat; |
---|
625 | this->bRelDirChanged; |
---|
626 | } |
---|
627 | else |
---|
628 | { |
---|
629 | delete this->toDirection; |
---|
630 | this->toDirection = NULL; |
---|
631 | PRINTF(5)("SmoothRotate of %s finished\n", this->getName()); |
---|
632 | } |
---|
633 | } |
---|
634 | |
---|
635 | // MAIN UPDATE ///////////////////////////////////// |
---|
636 | this->lastAbsCoordinate = this->absCoordinate; |
---|
637 | |
---|
638 | PRINTF(5)("PNode::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
639 | |
---|
640 | |
---|
641 | if( this->parentMode & PNODE_LOCAL_ROTATE && this->bRelDirChanged) |
---|
642 | { |
---|
643 | /* update the current absDirection - remember * means rotation around sth.*/ |
---|
644 | this->prevRelCoordinate = this->relCoordinate; |
---|
645 | this->absDirection = this->relDirection * parent->getAbsDir();; |
---|
646 | } |
---|
647 | |
---|
648 | if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged)) |
---|
649 | { |
---|
650 | /* update the current absCoordinate */ |
---|
651 | this->prevRelCoordinate = this->relCoordinate; |
---|
652 | this->absCoordinate = this->parent->getAbsCoor() + this->relCoordinate; |
---|
653 | } |
---|
654 | else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged) |
---|
655 | { |
---|
656 | /* update the current absCoordinate */ |
---|
657 | this->prevRelCoordinate = this->relCoordinate; |
---|
658 | this->absCoordinate = this->parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate); |
---|
659 | } |
---|
660 | ///////////////////////////////////////////////// |
---|
661 | } |
---|
662 | else |
---|
663 | { |
---|
664 | PRINTF(4)("NullParent::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
665 | if (this->bRelCoorChanged) |
---|
666 | { |
---|
667 | this->prevRelCoordinate = this->relCoordinate; |
---|
668 | this->absCoordinate = this->relCoordinate; |
---|
669 | } |
---|
670 | if (this->bRelDirChanged) |
---|
671 | { |
---|
672 | this->prevRelDirection = this->relDirection; |
---|
673 | this->absDirection = this->getAbsDir () * this->relDirection; |
---|
674 | } |
---|
675 | } |
---|
676 | |
---|
677 | if(!this->children.empty()) |
---|
678 | { |
---|
679 | list<PNode*>::iterator child; |
---|
680 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
681 | { |
---|
682 | /* if this node has changed, make sure, that all children are updated also */ |
---|
683 | if( likely(this->bRelCoorChanged)) |
---|
684 | (*child)->parentCoorChanged (); |
---|
685 | if( likely(this->bRelDirChanged)) |
---|
686 | (*child)->parentDirChanged (); |
---|
687 | |
---|
688 | (*child)->updateNode(dt); |
---|
689 | } |
---|
690 | } |
---|
691 | this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt; |
---|
692 | this->bRelCoorChanged = false; |
---|
693 | this->bRelDirChanged = false; |
---|
694 | } |
---|
695 | |
---|
696 | |
---|
697 | /** |
---|
698 | * @brief counts total amount the children walking through the entire tree. |
---|
699 | * @param nodes the counter |
---|
700 | */ |
---|
701 | void PNode::countChildNodes(int& nodes) const |
---|
702 | { |
---|
703 | nodes++; |
---|
704 | list<PNode*>::const_iterator child; |
---|
705 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
706 | (*child)->countChildNodes(nodes); |
---|
707 | } |
---|
708 | |
---|
709 | |
---|
710 | /** |
---|
711 | * @brief displays some information about this pNode |
---|
712 | * @param depth The deph into which to debug the children of this PNode to. |
---|
713 | * (0: all children will be debugged, 1: only this PNode, 2: this and direct children, ...) |
---|
714 | * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output). |
---|
715 | */ |
---|
716 | void PNode::debugNode(unsigned int depth, unsigned int level) const |
---|
717 | { |
---|
718 | for (unsigned int i = 0; i < level; i++) |
---|
719 | PRINT(0)(" |"); |
---|
720 | if (this->children.size() > 0) |
---|
721 | PRINT(0)(" +"); |
---|
722 | else |
---|
723 | PRINT(0)(" -"); |
---|
724 | |
---|
725 | int childNodeCount = 0; |
---|
726 | this->countChildNodes(childNodeCount); |
---|
727 | |
---|
728 | PRINT(0)("PNode(%s::%s) - absCoord: (%0.2f, %0.2f, %0.2f), relCoord(%0.2f, %0.2f, %0.2f), direction(%0.2f, %0.2f, %0.2f) - %s - %d childs\n", |
---|
729 | this->getClassName(), |
---|
730 | this->getName(), |
---|
731 | this->absCoordinate.x, |
---|
732 | this->absCoordinate.y, |
---|
733 | this->absCoordinate.z, |
---|
734 | this->relCoordinate.x, |
---|
735 | this->relCoordinate.y, |
---|
736 | this->relCoordinate.z, |
---|
737 | this->getAbsDirV().x, |
---|
738 | this->getAbsDirV().y, |
---|
739 | this->getAbsDirV().z, |
---|
740 | this->parentingModeToChar(parentMode), |
---|
741 | childNodeCount); |
---|
742 | if (depth >= 2 || depth == 0) |
---|
743 | { |
---|
744 | list<PNode*>::const_iterator child; |
---|
745 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
746 | { |
---|
747 | if (depth == 0) |
---|
748 | (*child)->debugNode(0, level + 1); |
---|
749 | else |
---|
750 | (*child)->debugNode(depth - 1, level +1); |
---|
751 | } |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | /** |
---|
756 | * @brief displays the PNode at its position with its rotation as a cube. |
---|
757 | * @param depth The deph into which to debug the children of this PNode to. |
---|
758 | * (0: all children will be displayed, 1: only this PNode, 2: this and direct children, ...) |
---|
759 | * @param size the Size of the Box to draw. |
---|
760 | * @param color the color of the Box to display. |
---|
761 | * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output). |
---|
762 | */ |
---|
763 | void PNode::debugDraw(unsigned int depth, float size, const Vector& color, unsigned int level) const |
---|
764 | { |
---|
765 | // if this is the first Element we draw |
---|
766 | if (level == 0) |
---|
767 | { |
---|
768 | glPushAttrib(GL_ENABLE_BIT); // save the Enable-attributes |
---|
769 | glMatrixMode(GL_MODELVIEW); // goto the ModelView Matrix |
---|
770 | |
---|
771 | glDisable(GL_LIGHTING); // disable lighting (we do not need them for just lighting) |
---|
772 | glDisable(GL_BLEND); // '' |
---|
773 | glDisable(GL_TEXTURE_2D); // '' |
---|
774 | glDisable(GL_DEPTH_TEST); // '' |
---|
775 | } |
---|
776 | |
---|
777 | glPushMatrix(); // repush the Matrix-stack |
---|
778 | /* translate */ |
---|
779 | glTranslatef (this->getAbsCoor ().x, |
---|
780 | this->getAbsCoor ().y, |
---|
781 | this->getAbsCoor ().z); |
---|
782 | // this->getAbsDir ().matrix (matrix); |
---|
783 | |
---|
784 | /* rotate */ |
---|
785 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
786 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
787 | /* set the new Color */ |
---|
788 | glColor3f(color.x, color.y, color.z); |
---|
789 | { /* draw a cube of size size */ |
---|
790 | glBegin(GL_LINE_STRIP); |
---|
791 | glVertex3f(-.5*size, -.5*size, -.5*size); |
---|
792 | glVertex3f(+.5*size, -.5*size, -.5*size); |
---|
793 | glVertex3f(+.5*size, -.5*size, +.5*size); |
---|
794 | glVertex3f(-.5*size, -.5*size, +.5*size); |
---|
795 | glVertex3f(-.5*size, -.5*size, -.5*size); |
---|
796 | glEnd(); |
---|
797 | glBegin(GL_LINE_STRIP); |
---|
798 | glVertex3f(-.5*size, +.5*size, -.5*size); |
---|
799 | glVertex3f(+.5*size, +.5*size, -.5*size); |
---|
800 | glVertex3f(+.5*size, +.5*size, +.5*size); |
---|
801 | glVertex3f(-.5*size, +.5*size, +.5*size); |
---|
802 | glVertex3f(-.5*size, +.5*size, -.5*size); |
---|
803 | glEnd(); |
---|
804 | |
---|
805 | glBegin(GL_LINES); |
---|
806 | glVertex3f(-.5*size, -.5*size, -.5*size); |
---|
807 | glVertex3f(-.5*size, +.5*size, -.5*size); |
---|
808 | glVertex3f(+.5*size, -.5*size, -.5*size); |
---|
809 | glVertex3f(+.5*size, +.5*size, -.5*size); |
---|
810 | glVertex3f(+.5*size, -.5*size, +.5*size); |
---|
811 | glVertex3f(+.5*size, +.5*size, +.5*size); |
---|
812 | glVertex3f(-.5*size, -.5*size, +.5*size); |
---|
813 | glVertex3f(-.5*size, +.5*size, +.5*size); |
---|
814 | glEnd(); |
---|
815 | } |
---|
816 | |
---|
817 | glPopMatrix(); |
---|
818 | if (depth >= 2 || depth == 0) |
---|
819 | { |
---|
820 | /* rotate the current color in HSV space around 20 degree */ |
---|
821 | Vector childColor = Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0)); |
---|
822 | list<PNode*>::const_iterator child; |
---|
823 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
824 | { |
---|
825 | // drawing the Dependency graph |
---|
826 | if (this != NullParent::getInstance()) |
---|
827 | { |
---|
828 | glBegin(GL_LINES); |
---|
829 | glColor3f(color.x, color.y, color.z); |
---|
830 | glVertex3f(this->getAbsCoor ().x, |
---|
831 | this->getAbsCoor ().y, |
---|
832 | this->getAbsCoor ().z); |
---|
833 | glColor3f(childColor.x, childColor.y, childColor.z); |
---|
834 | glVertex3f((*child)->getAbsCoor ().x, |
---|
835 | (*child)->getAbsCoor ().y, |
---|
836 | (*child)->getAbsCoor ().z); |
---|
837 | glEnd(); |
---|
838 | } |
---|
839 | |
---|
840 | /* if we want to draw the children too */ |
---|
841 | if (depth == 0) /* -> all of them */ |
---|
842 | (*child)->debugDraw(0, size, childColor, level+1); |
---|
843 | else /* -> only the Next one */ |
---|
844 | (*child)->debugDraw(depth - 1, size, childColor, level +1); |
---|
845 | } |
---|
846 | } |
---|
847 | if (level == 0) |
---|
848 | glPopAttrib(); /* pop the saved attributes back out */ |
---|
849 | } |
---|
850 | |
---|
851 | |
---|
852 | |
---|
853 | ///////////////////// |
---|
854 | // HELPER_FUCTIONS // |
---|
855 | ///////////////////// |
---|
856 | |
---|
857 | /** |
---|
858 | * @brief converts a parentingMode into a string that is the name of it |
---|
859 | * @param parentingMode the ParentingMode to convert |
---|
860 | * @return the converted string |
---|
861 | */ |
---|
862 | const char* PNode::parentingModeToChar(int parentingMode) |
---|
863 | { |
---|
864 | if (parentingMode == PNODE_LOCAL_ROTATE) |
---|
865 | return "local-rotate"; |
---|
866 | else if (parentingMode == PNODE_ROTATE_MOVEMENT) |
---|
867 | return "rotate-movement"; |
---|
868 | else if (parentingMode == PNODE_MOVEMENT) |
---|
869 | return "movement"; |
---|
870 | else if (parentingMode == PNODE_ALL) |
---|
871 | return "all"; |
---|
872 | else if (parentingMode == PNODE_ROTATE_AND_MOVE) |
---|
873 | return "rotate-and-move"; |
---|
874 | } |
---|
875 | |
---|
876 | /** |
---|
877 | * @brief converts a parenting-mode-string into a int |
---|
878 | * @param parentingMode the string naming the parentingMode |
---|
879 | * @return the int corresponding to the named parentingMode |
---|
880 | */ |
---|
881 | PARENT_MODE PNode::charToParentingMode(const char* parentingMode) |
---|
882 | { |
---|
883 | if (!strcmp(parentingMode, "local-rotate")) |
---|
884 | return (PNODE_LOCAL_ROTATE); |
---|
885 | else if (!strcmp(parentingMode, "rotate-movement")) |
---|
886 | return (PNODE_ROTATE_MOVEMENT); |
---|
887 | else if (!strcmp(parentingMode, "movement")) |
---|
888 | return (PNODE_MOVEMENT); |
---|
889 | else if (!strcmp(parentingMode, "all")) |
---|
890 | return (PNODE_ALL); |
---|
891 | else if (!strcmp(parentingMode, "rotate-and-move")) |
---|
892 | return (PNODE_ROTATE_AND_MOVE); |
---|
893 | } |
---|