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