Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6402 was 6402, checked in by patrick, 18 years ago

network: more modularity for the GameWorld: data and process are now totaly separated from each other, as it should be. Now I will do some magic on the MultiPlayerWorld, see if it works :D

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