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