Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/coord/p_node.cc @ 6629

Last change on this file since 6629 was 6624, checked in by bensch, 19 years ago

orxonox/trunk: Some missing stuff

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