Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/coord/p_node.cc @ 6336

Last change on this file since 6336 was 6336, checked in by rennerc, 18 years ago

ground turret at right place

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