Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/height_map_merge/src/lib/coord/p_node.cc @ 6496

Last change on this file since 6496 was 6424, checked in by bensch, 19 years ago

orxonox/trunk: merged the branche network back to the trunk
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r 6351:HEAD
no conflicts

File size: 32.1 KB
Line 
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 <algorithm>
24#include "compiler.h"
25#include "debug.h"
26
27#include "glincl.h"
28#include "color.h"
29
30#include "synchronizeable.h"
31
32#include "shell_command.h"
33SHELL_COMMAND(debugNode, PNode, debugNodeSC);
34
35using namespace std;
36
37
38/**
39 * @brief standard constructor
40 * @param parent the Parent of this Node. __NULL__ if __No Parent__ requested, PNode::getNullParent(), if connected to NullParent directly (default)
41 * @param nodeFlags all flags to set. THIS_WILL_OVERWRITE Default_Values.
42 */
43PNode::PNode (PNode* parent, long nodeFlags)
44{
45  this->setClassID(CL_PARENT_NODE, "PNode");
46
47  this->bRelCoorChanged = true;
48  this->bRelDirChanged = true;
49  this->parent = NULL;
50  this->parentMode = nodeFlags;
51  this->bActive = true;
52
53  // smooth-movers
54  this->toCoordinate = NULL;
55  this->toDirection = NULL;
56  this->bias = 1.0;
57
58  if (parent != NULL)
59    parent->addChild(this);
60}
61
62// NullParent Reference
63PNode* 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 */
79PNode::~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_NULL)
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->eraseChild(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 */
123void 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/**
160 *  init the pnode to a well definied state
161 *
162 * this function actualy only updates the PNode tree
163 */
164void PNode::init()
165{
166  /* just update all aboslute positions via timestep 0.001ms */
167  this->updateNode(0.001f);
168  this->updateNode(0.001f);
169}
170
171
172/**
173 * @brief set relative coordinates
174 * @param relCoord relative coordinates to its parent
175 *
176 *
177 * it is very importand, that you use this function, if you want to update the
178 * relCoordinates. If you don't use this, the PNode won't recognize, that something
179 * has changed and won't update the children Nodes.
180 */
181void PNode::setRelCoor (const Vector& relCoord)
182{
183  if (this->toCoordinate!= NULL)
184  {
185    delete this->toCoordinate;
186    this->toCoordinate = NULL;
187  }
188
189  this->relCoordinate = relCoord;
190  this->bRelCoorChanged = true;
191}
192
193/**
194 * @brief set relative coordinates
195 * @param x x-relative coordinates to its parent
196 * @param y y-relative coordinates to its parent
197 * @param z z-relative coordinates to its parent
198 * @see  void PNode::setRelCoor (const Vector& relCoord)
199 */
200void PNode::setRelCoor (float x, float y, float z)
201{
202  this->setRelCoor(Vector(x, y, z));
203}
204
205/**
206 * @brief sets a new relative position smoothely
207 * @param relCoordSoft the new Position to iterate to
208 * @param bias how fast to iterate to this position
209 */
210void PNode::setRelCoorSoft(const Vector& relCoordSoft, float bias)
211{
212  if (likely(this->toCoordinate == NULL))
213    this->toCoordinate = new Vector();
214
215  *this->toCoordinate = relCoordSoft;
216  this->bias = bias;
217}
218
219
220/**
221 * @brief set relative coordinates smoothely
222 * @param x x-relative coordinates to its parent
223 * @param y y-relative coordinates to its parent
224 * @param z z-relative coordinates to its parent
225 * @see  void PNode::setRelCoorSoft (const Vector&, float)
226 */
227void PNode::setRelCoorSoft (float x, float y, float z, float bias)
228{
229  this->setRelCoorSoft(Vector(x, y, z), bias);
230}
231
232
233/**
234 * @param absCoord set absolute coordinate
235 */
236void PNode::setAbsCoor (const Vector& absCoord)
237{
238  if (this->toCoordinate!= NULL)
239  {
240    delete this->toCoordinate;
241    this->toCoordinate = NULL;
242  }
243
244  if( likely(this->parentMode & PNODE_MOVEMENT))
245  {
246      /* if you have set the absolute coordinates this overrides all other changes */
247    if (likely(this->parent != NULL))
248      this->relCoordinate = absCoord - parent->getAbsCoor ();
249    else
250      this->relCoordinate = absCoord;
251  }
252  if( this->parentMode & PNODE_ROTATE_MOVEMENT)
253  {
254    if (likely(this->parent != NULL))
255      this->relCoordinate = absCoord - parent->getAbsCoor ();
256    else
257      this->relCoordinate = absCoord;
258  }
259
260  this->bRelCoorChanged = true;
261//  this->absCoordinate = absCoord;
262}
263
264
265/**
266 * @param x x-coordinate.
267 * @param y y-coordinate.
268 * @param z z-coordinate.
269 * @see void PNode::setAbsCoor (const Vector& absCoord)
270 */
271void PNode::setAbsCoor(float x, float y, float z)
272{
273  this->setAbsCoor(Vector(x, y, z));
274}
275
276
277/**
278 * @param absCoord set absolute coordinate
279 * @todo check off
280 */
281void PNode::setAbsCoorSoft (const Vector& absCoordSoft, float bias)
282{
283  if (this->toCoordinate == NULL)
284    this->toCoordinate = new Vector;
285
286  if( likely(this->parentMode & PNODE_MOVEMENT))
287  {
288      /* if you have set the absolute coordinates this overrides all other changes */
289    if (likely(this->parent != NULL))
290      *this->toCoordinate = absCoordSoft - parent->getAbsCoor ();
291    else
292      *this->toCoordinate = absCoordSoft;
293  }
294  if( this->parentMode & PNODE_ROTATE_MOVEMENT)
295  {
296    if (likely(this->parent != NULL))
297      *this->toCoordinate = absCoordSoft - parent->getAbsCoor ();
298    else
299      *this->toCoordinate = absCoordSoft;
300  }
301}
302
303
304/**
305 * @brief shift coordinate relative
306 * @param shift shift vector
307 *
308 * this function shifts the current coordinates about the vector shift. this is
309 * usefull because from some place else you can:
310 * PNode* someNode = ...;
311 * Vector objectMovement = calculateShift();
312 * someNode->shiftCoor(objectMovement);
313 *
314 * this is the internal method of:
315 * PNode* someNode = ...;
316 * Vector objectMovement = calculateShift();
317 * Vector currentCoor = someNode->getRelCoor();
318 * Vector newCoor = currentCoor + objectMovement;
319 * someNode->setRelCoor(newCoor);
320 *
321 */
322void PNode::shiftCoor (const Vector& shift)
323{
324  this->relCoordinate += shift;
325  this->bRelCoorChanged = true;
326}
327
328
329/**
330 * @brief set relative direction
331 * @param relDir to its parent
332 */
333void PNode::setRelDir (const Quaternion& relDir)
334{
335  if (this->toDirection!= NULL)
336  {
337    delete this->toDirection;
338    this->toDirection = NULL;
339  }
340  this->relDirection = relDir;
341
342  this->bRelCoorChanged = true;
343}
344
345
346/**
347 * @see void PNode::setRelDir (const Quaternion& relDir)
348 * @param x the x direction
349 * @param y the y direction
350 * @param z the z direction
351 *
352 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
353 */
354void PNode::setRelDir (float x, float y, float z)
355{
356  this->setRelDir(Quaternion(Vector(x,y,z), Vector(0,1,0)));
357}
358
359
360/**
361 * @brief sets the Relative Direction of this node to its parent in a Smoothed way
362 * @param relDirSoft the direction to iterate to smoothely.
363 * @param bias how fast to iterate to the new Direction
364 */
365void PNode::setRelDirSoft(const Quaternion& relDirSoft, float bias)
366{
367  if (likely(this->toDirection == NULL))
368    this->toDirection = new Quaternion();
369
370  *this->toDirection = relDirSoft;
371  this->bias = bias;
372  this->bRelDirChanged = true;
373}
374
375
376/**
377 * @see void PNode::setRelDirSoft (const Quaternion& relDir)
378 * @param x the x direction
379 * @param y the y direction
380 * @param z the z direction
381 *
382 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
383 */
384void PNode::setRelDirSoft(float x, float y, float z, float bias)
385{
386  this->setRelDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias);
387}
388
389
390/**
391 * @brief sets the absolute direction
392 * @param absDir absolute coordinates
393 */
394void PNode::setAbsDir (const Quaternion& absDir)
395{
396  if (this->toDirection!= NULL)
397  {
398    delete this->toDirection;
399    this->toDirection = NULL;
400  }
401
402  if (likely(this->parent != NULL))
403    this->relDirection = absDir / this->parent->getAbsDir();
404  else
405   this->relDirection = absDir;
406
407  this->bRelDirChanged = true;
408}
409
410
411/**
412 * @see void PNode::setAbsDir (const Quaternion& relDir)
413 * @param x the x direction
414 * @param y the y direction
415 * @param z the z direction
416 *
417 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
418 */
419void PNode::setAbsDir (float x, float y, float z)
420{
421  this->setAbsDir(Quaternion(Vector(x,y,z), Vector(0,1,0)));
422}
423
424
425/**
426 * @brief sets the absolute direction
427 * @param absDir absolute coordinates
428 * @param bias how fast to iterator to the new Position
429 */
430void PNode::setAbsDirSoft (const Quaternion& absDirSoft, float bias)
431{
432  if (this->toDirection == NULL)
433    this->toDirection = new Quaternion();
434
435  if (likely(this->parent != NULL))
436    *this->toDirection = absDirSoft / this->parent->getAbsDir();
437  else
438   *this->toDirection = absDirSoft;
439
440  this->bias = bias;
441  this->bRelDirChanged = true;
442}
443
444
445/**
446 * @see void PNode::setAbsDir (const Quaternion& relDir)
447 * @param x the x direction
448 * @param y the y direction
449 * @param z the z direction
450 *
451 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
452 */
453void PNode::setAbsDirSoft (float x, float y, float z, float bias)
454{
455  this->setAbsDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias);
456}
457
458
459/**
460 * @brief shift Direction
461 * @param shift the direction around which to shift.
462 */
463void PNode::shiftDir (const Quaternion& shift)
464{
465  this->relDirection = this->relDirection * shift;
466  this->bRelDirChanged = true;
467}
468
469
470/**
471 * @brief adds a child and makes this node to a parent
472 * @param child child reference
473 * use this to add a child to this node.
474 */
475void PNode::addChild (PNode* child)
476{
477  if( likely(child->parent != NULL))
478      child->parent->eraseChild(child);
479  if (this->checkIntegrity(child))
480  {
481    child->parent = this;
482    if (unlikely(this != NULL))
483      this->children.push_back(child);
484    child->parentCoorChanged();
485  }
486  else
487  {
488    PRINTF(1)("Tried to reparent to own child '%s::%s' to '%s::%s'.\n",
489              this->getClassName(), this->getName(), child->getClassName(), child->getName());
490    child->parent = NULL;
491    child->parentCoorChanged();
492  }
493}
494
495
496/**
497 * @see PNode::addChild(PNode* child);
498 * @param childName the name of the child to add to this PNode
499 */
500void PNode::addChild (const char* childName)
501{
502  PNode* childNode = dynamic_cast<PNode*>(ClassList::getObject(childName, CL_PARENT_NODE));
503  if (childNode != NULL)
504    this->addChild(childNode);
505}
506
507
508/**
509 * @brief removes a child from the node
510 * @param child the child to remove from this pNode.
511 *
512 * Children from pNode will not be lost, they are Reparented by the rules of the ParentMode
513 */
514void PNode::removeChild (PNode* child)
515{
516  if (child != NULL)
517   child->removeNode();
518}
519
520
521/**
522 * !! PRIVATE FUNCTION
523 * @brief reparents a node (happens on Parents Node delete or remove and Flags are set.)
524 */
525void PNode::reparent()
526{
527  if (this->parentMode & PNODE_REPARENT_TO_NULL)
528    this->setParent((PNode*)NULL);
529  else if (this->parentMode & PNODE_REPARENT_TO_PARENTS_PARENT && this->parent != NULL)
530    this->setParent(this->parent->getParent());
531  else
532    this->setParent(PNode::getNullParent());
533}
534
535/**
536 * ereases child from the nodes children
537 * @param chuld the child to remove
538 */
539void PNode::eraseChild(PNode* child)
540{
541  std::list<PNode*>::iterator childRemover = std::find(this->children.begin(), this->children.end(), child);
542  if(childRemover != this->children.end())
543    this->children.erase(childRemover);
544}
545
546
547/**
548 * @brief remove this pnode from the tree and adds all following to NullParent
549 *
550 * this can be the case, if an entity in the world is being destroyed.
551 */
552void PNode::removeNode()
553{
554  list<PNode*>::iterator child = this->children.begin();
555  list<PNode*>::iterator reparenter;
556  while (child != this->children.end())
557  {
558    reparenter = child;
559    child++;
560    if (this->parentMode & PNODE_REPARENT_CHILDREN_ON_REMOVE ||
561        (*reparenter)->parentMode & PNODE_REPARENT_ON_PARENTS_REMOVE)
562    {
563      printf("TEST----------------%s ---- %s\n", this->getClassName(), (*reparenter)->getClassName());
564      (*reparenter)->reparent();
565      printf("REPARENTED TO: %s::%s\n",(*reparenter)->getParent()->getClassName(),(*reparenter)->getParent()->getName());
566    }
567  }
568  if (this->parent != NULL)
569  {
570    this->parent->eraseChild(this);
571    this->parent = NULL;
572  }
573}
574
575
576/**
577 * @see PNode::setParent(PNode* parent);
578 * @param parentName the name of the Parent to set to this PNode
579 */
580void PNode::setParent (const char* parentName)
581{
582  PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE));
583  if (parentNode != NULL)
584    parentNode->addChild(this);
585  else
586    PRINTF(2)("Not Found PNode's (%s::%s) new Parent by Name: %s\n",
587              this->getClassName(), this->getName(), parentName);
588}
589
590
591/**
592 * @brief does the reparenting in a very smooth way
593 * @param parentNode the new Node to connect this node to.
594 * @param bias the speed to iterate to this new Positions
595 */
596void PNode::setParentSoft(PNode* parentNode, float bias)
597{
598  // return if the new parent and the old one match
599  if (this->parent == parentNode )
600    return;
601  if (parentNode == NULL)
602    parentNode = PNode::getNullParent();
603
604  // store the Valures to iterate to.
605  if (likely(this->toCoordinate == NULL))
606  {
607    this->toCoordinate = new Vector();
608    *this->toCoordinate = this->getRelCoor();
609  }
610  if (likely(this->toDirection == NULL))
611  {
612    this->toDirection = new Quaternion();
613    *this->toDirection = this->getRelDir();
614  }
615  this->bias = bias;
616
617
618  Vector tmpV = this->getAbsCoor();
619  Quaternion tmpQ = this->getAbsDir();
620
621  parentNode->addChild(this);
622
623 if (this->parentMode & PNODE_ROTATE_MOVEMENT && this->parent != NULL)
624   this->relCoordinate = this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor());
625 else
626   this->relCoordinate = tmpV - parentNode->getAbsCoor();
627
628 this->relDirection = tmpQ / parentNode->getAbsDir();
629}
630
631
632/**
633 * @brief does the reparenting in a very smooth way
634 * @param parentName the name of the Parent to reconnect to
635 * @param bias the speed to iterate to this new Positions
636 */
637void PNode::setParentSoft(const char* parentName, float bias)
638{
639  PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE));
640  if (parentNode != NULL)
641    this->setParentSoft(parentNode, bias);
642}
643
644/**
645 * @param parentMode sets the parentingMode of this Node
646 */
647void PNode::setParentMode(PARENT_MODE parentMode)
648{
649  this->parentMode = ((this->parentMode & 0xfff0) | parentMode);
650}
651
652/**
653 * @brief sets the mode of this parent manually
654 * @param parentMode a String representing this parentingMode
655 */
656void PNode::setParentMode (const char* parentingMode)
657{
658  this->setParentMode(PNode::charToParentingMode(parentingMode));
659}
660
661/**
662 * @brief adds special mode Flags to this PNode
663 * @see PARENT_MODE
664 * @param nodeFlags a compsition of PARENT_MODE-flags, split by the '|' (or) operator.
665 */
666void PNode::addNodeFlags(unsigned short nodeFlags)
667{
668  this->parentMode |= nodeFlags;
669}
670
671/**
672 * @brief removes special mode Flags to this PNode
673 * @see PARENT_MODE
674 * @param nodeFlags a compsition of PARENT_MODE-flags, split by the '|' (or) operator.
675 */
676void PNode::removeNodeFlags(unsigned short nodeFlags)
677{
678  this->parentMode &= !nodeFlags;
679}
680
681/**
682 * @returns the NullParent (and if needed creates it)
683 */
684PNode* PNode::createNullParent()
685{
686  if (likely(PNode::nullParent == NULL))
687  {
688    PNode::nullParent = new PNode(NULL, PNODE_PARENT_MODE_DEFAULT | PNODE_REPARENT_TO_NULL);
689    PNode::nullParent->setName("NullParent");
690  }
691  return PNode::nullParent;
692}
693
694
695/**
696 * !! PRIVATE FUNCTION
697 * @brief checks the upward integrity (e.g if PNode is somewhere up the Node tree.)
698 * @param checkParent the Parent to check.
699 * @returns true if the integrity-check succeeds, false otherwise.
700 *
701 * If there is a second occurence of checkParent before NULL, then a loop could get
702 * into the Tree, and we do not want this.
703 */
704bool PNode::checkIntegrity(const PNode* checkParent) const
705{
706  const PNode* parent = this;
707  while ( (parent = parent->getParent()) != NULL)
708    if (unlikely(parent == checkParent))
709      return false;
710  return true;
711}
712
713
714/**
715 * @brief updates the absCoordinate/absDirection
716 * @param dt The time passed since the last update
717 *
718 * this is used to go through the parent-tree to update all the absolute coordinates
719 * and directions. this update should be done by the engine, so you don't have to
720 * worry, normaly...
721 */
722void PNode::updateNode (float dt)
723{
724  if (!(this->parentMode & PNODE_STATIC_NODE))
725  {
726    if( likely(this->parent != NULL))
727    {
728        // movement for nodes with smoothMove enabled
729        if (unlikely(this->toCoordinate != NULL))
730        {
731          Vector moveVect = (*this->toCoordinate - this->relCoordinate) *fabsf(dt)*bias;
732          if (likely(moveVect.len() >= PNODE_ITERATION_DELTA))
733          {
734            this->shiftCoor(moveVect);
735          }
736          else
737          {
738            delete this->toCoordinate;
739            this->toCoordinate = NULL;
740            PRINTF(5)("SmoothMove of %s finished\n", this->getName());
741          }
742        }
743        if (unlikely(this->toDirection != NULL))
744        {
745          Quaternion rotQuat = Quaternion::quatSlerp(this->relDirection,*this->toDirection, fabsf(dt)*this->bias);
746          if (this->relDirection.distance(rotQuat) >PNODE_ITERATION_DELTA)
747          {
748            this->relDirection = rotQuat;
749            this->bRelDirChanged;
750          }
751          else
752          {
753            delete this->toDirection;
754            this->toDirection = NULL;
755            PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
756          }
757        }
758
759        // MAIN UPDATE /////////////////////////////////////
760        this->lastAbsCoordinate = this->absCoordinate;
761
762        PRINTF(5)("PNode::update - '%s::%s' - (%f, %f, %f)\n", this->getClassName(), this->getName(),
763                      this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
764
765
766        if(this->bRelDirChanged && this->parentMode & PNODE_LOCAL_ROTATE )
767        {
768          /* update the current absDirection - remember * means rotation around sth.*/
769          this->prevRelCoordinate = this->relCoordinate;
770          this->absDirection = parent->getAbsDir() * this->relDirection;
771        }
772
773        if(likely(this->bRelCoorChanged && this->parentMode & PNODE_MOVEMENT))
774        {
775        /* update the current absCoordinate */
776          this->prevRelCoordinate = this->relCoordinate;
777          this->absCoordinate = this->parent->getAbsCoor() + this->relCoordinate;
778        }
779        else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged)
780        {
781          /* update the current absCoordinate */
782          this->prevRelCoordinate = this->relCoordinate;
783          this->absCoordinate = this->parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate);
784        }
785        /////////////////////////////////////////////////
786      }
787
788  else // Nodes without a Parent are handled faster :: MOST LIKELY THE NULLPARENT
789    {
790      PRINTF(4)("update ParentLess Node (%s::%s) - (%f, %f, %f)\n", this->getClassName(), this->getName(),
791                this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
792        if (this->bRelCoorChanged)
793        {
794          this->prevRelCoordinate = this->relCoordinate;
795          this->absCoordinate = this->relCoordinate;
796        }
797        if (this->bRelDirChanged)
798        {
799          this->prevRelDirection = this->relDirection;
800          this->absDirection = this->getAbsDir () * this->relDirection;
801        }
802      }
803    }
804
805    if(!this->children.empty() && (this->bActive || this->parentMode & PNODE_UPDATE_CHILDREN_IF_INACTIVE ))
806    {
807      list<PNode*>::iterator child;
808      for (child = this->children.begin(); child != this->children.end(); child ++)
809      {
810        /* if this node has changed, make sure, that all children are updated also */
811        if( likely(this->bRelCoorChanged))
812          (*child)->parentCoorChanged ();
813        if( likely(this->bRelDirChanged))
814          (*child)->parentDirChanged ();
815
816        (*child)->updateNode(dt);
817      }
818    }
819    this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
820    this->bRelCoorChanged = false;
821    this->bRelDirChanged = false;
822}
823
824
825
826
827
828/*************
829 * DEBUGGING *
830 *************/
831/**
832 * @brief counts total amount the children walking through the entire tree.
833 * @param nodes the counter
834 */
835void PNode::countChildNodes(int& nodes) const
836{
837  nodes++;
838  list<PNode*>::const_iterator child;
839  for (child = this->children.begin(); child != this->children.end(); child ++)
840    (*child)->countChildNodes(nodes);
841}
842
843
844/**
845 * @brief displays some information about this pNode
846 * @param depth The deph into which to debug the children of this PNode to.
847 * (0: all children will be debugged, 1: only this PNode, 2: this and direct children, ...)
848 * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output).
849 */
850void PNode::debugNode(unsigned int depth, unsigned int level) const
851{
852  for (unsigned int i = 0; i < level; i++)
853    PRINT(0)(" |");
854  if (this->children.size() > 0)
855    PRINT(0)(" +");
856  else
857    PRINT(0)(" -");
858
859  int childNodeCount = 0;
860  this->countChildNodes(childNodeCount);
861
862  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",
863           this->getClassName(),
864           this->getName(),
865           this->absCoordinate.x,
866           this->absCoordinate.y,
867           this->absCoordinate.z,
868           this->relCoordinate.x,
869           this->relCoordinate.y,
870           this->relCoordinate.z,
871           this->getAbsDirV().x,
872           this->getAbsDirV().y,
873           this->getAbsDirV().z,
874           this->parentingModeToChar(parentMode),
875           childNodeCount);
876  if (depth >= 2 || depth == 0)
877  {
878    list<PNode*>::const_iterator child;
879    for (child = this->children.begin(); child != this->children.end(); child ++)
880    {
881      if (depth == 0)
882        (*child)->debugNode(0, level + 1);
883      else
884        (*child)->debugNode(depth - 1, level +1);
885    }
886  }
887}
888
889/**
890 * @brief displays the PNode at its position with its rotation as a cube.
891 * @param  depth The deph into which to debug the children of this PNode to.
892 * (0: all children will be displayed, 1: only this PNode, 2: this and direct children, ...)
893 * @param size the Size of the Box to draw.
894 * @param color the color of the Box to display.
895 * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output).
896 */
897void PNode::debugDraw(unsigned int depth, float size, const Vector& color, unsigned int level) const
898{
899  // if this is the first Element we draw
900  if (level == 0)
901  {
902    glPushAttrib(GL_ENABLE_BIT); // save the Enable-attributes
903    glMatrixMode(GL_MODELVIEW);  // goto the ModelView Matrix
904
905    glDisable(GL_LIGHTING);      // disable lighting (we do not need them for just lighting)
906    glDisable(GL_BLEND);         // ''
907    glDisable(GL_TEXTURE_2D);    // ''
908    glDisable(GL_DEPTH_TEST);    // ''
909  }
910
911  glPushMatrix();                // repush the Matrix-stack
912  /* translate */
913  glTranslatef (this->getAbsCoor ().x,
914                this->getAbsCoor ().y,
915                this->getAbsCoor ().z);
916//  this->getAbsDir ().matrix (matrix);
917
918  /* rotate */
919  Vector tmpRot = this->getAbsDir().getSpacialAxis();
920  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
921  /* set the new Color */
922  glColor3f(color.x, color.y, color.z);
923  { /* draw a cube of size size */
924    glBegin(GL_LINE_STRIP);
925    glVertex3f(-.5*size, -.5*size,  -.5*size);
926    glVertex3f(+.5*size, -.5*size,  -.5*size);
927    glVertex3f(+.5*size, -.5*size,  +.5*size);
928    glVertex3f(-.5*size, -.5*size,  +.5*size);
929    glVertex3f(-.5*size, -.5*size,  -.5*size);
930    glEnd();
931    glBegin(GL_LINE_STRIP);
932    glVertex3f(-.5*size, +.5*size,  -.5*size);
933    glVertex3f(+.5*size, +.5*size,  -.5*size);
934    glVertex3f(+.5*size, +.5*size,  +.5*size);
935    glVertex3f(-.5*size, +.5*size,  +.5*size);
936    glVertex3f(-.5*size, +.5*size,  -.5*size);
937    glEnd();
938
939    glBegin(GL_LINES);
940    glVertex3f(-.5*size, -.5*size,  -.5*size);
941    glVertex3f(-.5*size, +.5*size,  -.5*size);
942    glVertex3f(+.5*size, -.5*size,  -.5*size);
943    glVertex3f(+.5*size, +.5*size,  -.5*size);
944    glVertex3f(+.5*size, -.5*size,  +.5*size);
945    glVertex3f(+.5*size, +.5*size,  +.5*size);
946    glVertex3f(-.5*size, -.5*size,  +.5*size);
947    glVertex3f(-.5*size, +.5*size,  +.5*size);
948    glEnd();
949  }
950
951  glPopMatrix();
952  if (depth >= 2 || depth == 0)
953  {
954    /* rotate the current color in HSV space around 20 degree */
955    Vector childColor =  Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0));
956    list<PNode*>::const_iterator child;
957    for (child = this->children.begin(); child != this->children.end(); child ++)
958    {
959      // drawing the Dependency graph
960     if (this != PNode::getNullParent())
961      {
962       glBegin(GL_LINES);
963       glColor3f(color.x, color.y, color.z);
964       glVertex3f(this->getAbsCoor ().x,
965                  this->getAbsCoor ().y,
966                  this->getAbsCoor ().z);
967        glColor3f(childColor.x, childColor.y, childColor.z);
968        glVertex3f((*child)->getAbsCoor ().x,
969                   (*child)->getAbsCoor ().y,
970                   (*child)->getAbsCoor ().z);
971        glEnd();
972      }
973
974      /* if we want to draw the children too */
975      if (depth == 0) /* -> all of them */
976        (*child)->debugDraw(0, size, childColor, level+1);
977      else            /* -> only the Next one */
978        (*child)->debugDraw(depth - 1, size, childColor, level +1);
979    }
980  }
981  if (level == 0)
982    glPopAttrib(); /* pop the saved attributes back out */
983}
984
985
986
987/////////////////////
988// HELPER_FUCTIONS //
989/////////////////////
990
991/**
992 * @brief converts a parentingMode into a string that is the name of it
993 * @param parentingMode the ParentingMode to convert
994 * @return the converted string
995 */
996const char* PNode::parentingModeToChar(int parentingMode)
997{
998  if (parentingMode == PNODE_LOCAL_ROTATE)
999    return "local-rotate";
1000  else if (parentingMode == PNODE_ROTATE_MOVEMENT)
1001    return "rotate-movement";
1002  else if (parentingMode == PNODE_MOVEMENT)
1003    return "movement";
1004  else if (parentingMode == PNODE_ALL)
1005    return "all";
1006  else if (parentingMode == PNODE_ROTATE_AND_MOVE)
1007    return "rotate-and-move";
1008}
1009
1010/**
1011 * @brief converts a parenting-mode-string into a int
1012 * @param parentingMode the string naming the parentingMode
1013 * @return the int corresponding to the named parentingMode
1014 */
1015PARENT_MODE PNode::charToParentingMode(const char* parentingMode)
1016{
1017  if (!strcmp(parentingMode, "local-rotate"))
1018    return (PNODE_LOCAL_ROTATE);
1019  else  if (!strcmp(parentingMode, "rotate-movement"))
1020    return (PNODE_ROTATE_MOVEMENT);
1021  else  if (!strcmp(parentingMode, "movement"))
1022    return (PNODE_MOVEMENT);
1023  else  if (!strcmp(parentingMode, "all"))
1024    return (PNODE_ALL);
1025  else  if (!strcmp(parentingMode, "rotate-and-move"))
1026    return (PNODE_ROTATE_AND_MOVE);
1027}
1028
1029/**
1030 * Writes data from network containing information about the state
1031 * @param data pointer to data
1032 * @param length length of data
1033 * @param sender hostID of sender
1034 */
1035int PNode::writeState( const byte * data, int length, int sender )
1036{
1037  SYNCHELP_READ_BEGIN();
1038
1039  SYNCHELP_READ_FKT( BaseObject::writeState );
1040
1041  PRINTF(0)("name = %s\n", this->getName());
1042
1043  char * parentName = NULL;
1044  SYNCHELP_READ_STRINGM( parentName );
1045
1046  if ( strcmp(parentName, "")==0 )
1047  {
1048    setParent( (char*)NULL );
1049  }
1050  else
1051  {
1052    setParent( parentName );
1053  }
1054
1055  delete[] parentName;
1056
1057  int parentMode;
1058  SYNCHELP_READ_INT( parentMode );
1059  this->setParentMode((PARENT_MODE)parentMode);
1060
1061  float f1, f2, f3, f4;
1062
1063  SYNCHELP_READ_FLOAT( f1 );
1064  SYNCHELP_READ_FLOAT( f2 );
1065  SYNCHELP_READ_FLOAT( f3 );
1066  this->setRelCoor( f1, f2, f3 );
1067  //this->setRelCoor( 10, 0, 0 );
1068
1069  SYNCHELP_READ_FLOAT( f1 );
1070  SYNCHELP_READ_FLOAT( f2 );
1071  SYNCHELP_READ_FLOAT( f3 );
1072  //this->setAbsCoor( f1, f2, f3 );
1073
1074  SYNCHELP_READ_FLOAT( f1 );
1075  SYNCHELP_READ_FLOAT( f2 );
1076  SYNCHELP_READ_FLOAT( f3 );
1077  SYNCHELP_READ_FLOAT( f4 );
1078  this->setRelDir( Quaternion( Vector(f2, f3, f4), f1 ) );
1079
1080  SYNCHELP_READ_FLOAT( f1 );
1081  SYNCHELP_READ_FLOAT( f2 );
1082  SYNCHELP_READ_FLOAT( f3 );
1083  SYNCHELP_READ_FLOAT( f4 );
1084  //this->setAbsDir( Quaternion( Vector(f2, f3, f4), f1 ) );
1085
1086  int n;
1087  char * childName;
1088
1089  SYNCHELP_READ_INT( n );
1090
1091  for (int i = 0; i<n; i++)
1092  {
1093    SYNCHELP_READ_STRINGM( childName );
1094    PRINTF(0)("childname = %s\n", childName);
1095    addChild( childName );
1096    delete childName;
1097    childName = NULL;
1098  }
1099
1100  return SYNCHELP_READ_N;
1101}
1102
1103/**
1104 * data copied in data will bee sent to another host
1105 * @param data pointer to data
1106 * @param maxLength max length of data
1107 * @return the number of bytes writen
1108 */
1109int PNode::readState( byte * data, int maxLength )
1110{
1111  SYNCHELP_WRITE_BEGIN();
1112
1113  SYNCHELP_WRITE_FKT( BaseObject::readState );
1114
1115  PRINTF(0)("name = %s\n", this->getName());
1116
1117  if ( this->parent )
1118  {
1119    SYNCHELP_WRITE_STRING( parent->getName() );
1120  }
1121  else
1122  {
1123    SYNCHELP_WRITE_STRING( "" );
1124  }
1125
1126  SYNCHELP_WRITE_INT( this->parentMode );
1127
1128  SYNCHELP_WRITE_FLOAT( this->relCoordinate.x );
1129  SYNCHELP_WRITE_FLOAT( this->relCoordinate.y );
1130  SYNCHELP_WRITE_FLOAT( this->relCoordinate.z );
1131
1132  PRINTF(0)("%s, %f, %f, %f\n", getClassName(), relCoordinate.x, relCoordinate.y, relCoordinate.z);
1133
1134  SYNCHELP_WRITE_FLOAT( this->absCoordinate.x );
1135  SYNCHELP_WRITE_FLOAT( this->absCoordinate.y );
1136  SYNCHELP_WRITE_FLOAT( this->absCoordinate.z );
1137
1138  PRINTF(0)("%s, %f, %f, %f\n", getClassName(), absCoordinate.x, absCoordinate.y, absCoordinate.z);
1139
1140  SYNCHELP_WRITE_FLOAT( this->relDirection.w );
1141  SYNCHELP_WRITE_FLOAT( this->relDirection.v.x );
1142  SYNCHELP_WRITE_FLOAT( this->relDirection.v.y );
1143  SYNCHELP_WRITE_FLOAT( this->relDirection.v.z );
1144
1145  SYNCHELP_WRITE_FLOAT( this->absDirection.w );
1146  SYNCHELP_WRITE_FLOAT( this->absDirection.v.x );
1147  SYNCHELP_WRITE_FLOAT( this->absDirection.v.y );
1148  SYNCHELP_WRITE_FLOAT( this->absDirection.v.z );
1149
1150  int n = children.size();
1151  SYNCHELP_WRITE_INT( n );
1152
1153  for (std::list<PNode*>::const_iterator it = children.begin(); it!=children.end(); it++)
1154  {
1155    PRINTF(0)("childname = %s\n", (*it)->getName() );
1156    SYNCHELP_WRITE_STRING( (*it)->getName() );
1157  }
1158  return SYNCHELP_WRITE_N;
1159}
Note: See TracBrowser for help on using the repository browser.