Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6527 was 6526, checked in by rennerc, 19 years ago

pnode can sync now

File size: 34.4 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Patrick Boenzli
13   co-programmer: Benjamin Grauer
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PNODE
17
18#include "p_node.h"
19
20#include "load_param.h"
21#include "class_list.h"
22
23#include <algorithm>
24#include "compiler.h"
25#include "debug.h"
26
27#include "glincl.h"
28#include "color.h"
29
30#include "synchronizeable.h"
31
32#include "shell_command.h"
33SHELL_COMMAND(debugNode, PNode, debugNodeSC);
34
35using namespace std;
36
37
38/**
39 * @brief standard constructor
40 * @param parent the Parent of this Node. __NULL__ if __No Parent__ requested, PNode::getNullParent(), if connected to NullParent directly (default)
41 * @param nodeFlags all flags to set. THIS_WILL_OVERWRITE Default_Values.
42 */
43PNode::PNode (PNode* parent, long nodeFlags)
44{
45  this->setClassID(CL_PARENT_NODE, "PNode");
46
47  this->bRelCoorChanged = true;
48  this->bRelDirChanged = true;
49  this->parent = NULL;
50  this->parentMode = nodeFlags;
51  this->bActive = true;
52
53  // smooth-movers
54  this->toCoordinate = NULL;
55  this->toDirection = NULL;
56  this->bias = 1.0;
57
58  if (parent != NULL)
59    parent->addChild(this);
60}
61
62// NullParent Reference
63PNode* PNode::nullParent = NULL;
64
65/**
66 * @brief standard deconstructor
67 *
68 * There are two general ways to delete a PNode
69 * 1. delete instance;
70 *   -> result
71 *    delete this Node and all its children and children's children...
72 *    (danger if you still need the children's instance somewhere else!!)
73 *
74 * 2. instance->removeNode(); delete instance;
75 *   -> result:
76 *    moves its children to the NullParent
77 *    then deletes the Element.
78 */
79PNode::~PNode ()
80{
81  // remove the Node, delete it's children (if required).
82  std::list<PNode*>::iterator tmp = this->children.begin();
83  std::list<PNode*>::iterator deleteNode;
84  while(!this->children.empty())
85    while (tmp != this->children.end())
86    {
87      deleteNode = tmp;
88      tmp++;
89//      printf("TEST::%s(%s) %s\n", (*deleteNode)->getName(), (*deleteNode)->getClassName(), this->getName());
90      if ((this->parentMode & PNODE_PROHIBIT_CHILD_DELETE) ||
91          ((*deleteNode)->parentMode & PNODE_PROHIBIT_DELETE_WITH_PARENT))
92      {
93        if (this == PNode::nullParent && (*deleteNode)->parentMode & PNODE_REPARENT_TO_NULL)
94          delete (*deleteNode);
95        else
96          (*deleteNode)->reparent();
97      }
98      else
99        delete (*deleteNode);
100    }
101
102  if (this->parent != NULL)
103   {
104     this->parent->eraseChild(this);
105     this->parent = NULL;
106   }
107
108  // remove all other allocated memory.
109  if (this->toCoordinate != NULL)
110    delete this->toCoordinate;
111  if (this->toDirection != NULL)
112    delete this->toDirection;
113
114  if (this == PNode::nullParent)
115    PNode::nullParent = NULL;
116}
117
118
119/**
120 * @brief loads parameters of the PNode
121 * @param root the XML-element to load the properties of
122 */
123void PNode::loadParams(const TiXmlElement* root)
124{
125  static_cast<BaseObject*>(this)->loadParams(root);
126
127  LoadParam(root, "rel-coor", this, PNode, setRelCoor)
128      .describe("Sets The relative position of the Node to its parent.");
129
130  LoadParam(root, "abs-coor", this, PNode, setAbsCoor)
131      .describe("Sets The absolute Position of the Node.");
132
133  LoadParam(root, "rel-dir", this, PNode, setRelDir)
134      .describe("Sets The relative rotation of the Node to its parent.");
135
136  LoadParam(root, "abs-dir", this, PNode, setAbsDir)
137      .describe("Sets The absolute rotation of the Node.");
138
139  LoadParam(root, "parent", this, PNode, setParent)
140      .describe("the Name of the Parent of this PNode");
141
142  LoadParam(root, "parent-mode", this, PNode, setParentMode)
143      .describe("the mode to connect this node to its parent ()");
144
145  // cycling properties
146  if (root != NULL)
147  {
148    LOAD_PARAM_START_CYCLE(root, element);
149    {
150      LoadParam_CYCLE(element, "child", this, PNode, addChild)
151          .describe("adds a new Child to the current Node.");
152
153    }
154    LOAD_PARAM_END_CYCLE(element);
155  }
156}
157
158
159/**
160 *  init the pnode to a well definied state
161 *
162 * this function actualy only updates the PNode tree
163 */
164void PNode::init()
165{
166  /* just update all aboslute positions via timestep 0.001ms */
167  this->updateNode(0.001f);
168  this->updateNode(0.001f);
169}
170
171
172/**
173 * @brief set relative coordinates
174 * @param relCoord relative coordinates to its parent
175 *
176 *
177 * it is very importand, that you use this function, if you want to update the
178 * relCoordinates. If you don't use this, the PNode won't recognize, that something
179 * has changed and won't update the children Nodes.
180 */
181void PNode::setRelCoor (const Vector& relCoord)
182{
183  if (this->toCoordinate!= NULL)
184  {
185    delete this->toCoordinate;
186    this->toCoordinate = NULL;
187  }
188
189  this->relCoordinate = relCoord;
190  this->bRelCoorChanged = true;
191}
192
193/**
194 * @brief set relative coordinates
195 * @param x x-relative coordinates to its parent
196 * @param y y-relative coordinates to its parent
197 * @param z z-relative coordinates to its parent
198 * @see  void PNode::setRelCoor (const Vector& relCoord)
199 */
200void PNode::setRelCoor (float x, float y, float z)
201{
202  this->setRelCoor(Vector(x, y, z));
203}
204
205/**
206 * @brief sets a new relative position smoothely
207 * @param relCoordSoft the new Position to iterate to
208 * @param bias how fast to iterate to this position
209 */
210void PNode::setRelCoorSoft(const Vector& relCoordSoft, float bias)
211{
212  if (likely(this->toCoordinate == NULL))
213    this->toCoordinate = new Vector();
214
215  *this->toCoordinate = relCoordSoft;
216  this->bias = bias;
217}
218
219
220/**
221 * @brief set relative coordinates smoothely
222 * @param x x-relative coordinates to its parent
223 * @param y y-relative coordinates to its parent
224 * @param z z-relative coordinates to its parent
225 * @see  void PNode::setRelCoorSoft (const Vector&, float)
226 */
227void PNode::setRelCoorSoft (float x, float y, float z, float bias)
228{
229  this->setRelCoorSoft(Vector(x, y, z), bias);
230}
231
232
233/**
234 * @param absCoord set absolute coordinate
235 */
236void PNode::setAbsCoor (const Vector& absCoord)
237{
238  if (this->toCoordinate!= NULL)
239  {
240    delete this->toCoordinate;
241    this->toCoordinate = NULL;
242  }
243
244  if( likely(this->parentMode & PNODE_MOVEMENT))
245  {
246      /* if you have set the absolute coordinates this overrides all other changes */
247    if (likely(this->parent != NULL))
248      this->relCoordinate = absCoord - parent->getAbsCoor ();
249    else
250      this->relCoordinate = absCoord;
251  }
252  if( this->parentMode & PNODE_ROTATE_MOVEMENT)
253  {
254    if (likely(this->parent != NULL))
255      this->relCoordinate = absCoord - parent->getAbsCoor ();
256    else
257      this->relCoordinate = absCoord;
258  }
259
260  this->bRelCoorChanged = true;
261//  this->absCoordinate = absCoord;
262}
263
264
265/**
266 * @param x x-coordinate.
267 * @param y y-coordinate.
268 * @param z z-coordinate.
269 * @see void PNode::setAbsCoor (const Vector& absCoord)
270 */
271void PNode::setAbsCoor(float x, float y, float z)
272{
273  this->setAbsCoor(Vector(x, y, z));
274}
275
276
277/**
278 * @param absCoord set absolute coordinate
279 * @todo check off
280 */
281void PNode::setAbsCoorSoft (const Vector& absCoordSoft, float bias)
282{
283  if (this->toCoordinate == NULL)
284    this->toCoordinate = new Vector;
285
286  if( likely(this->parentMode & PNODE_MOVEMENT))
287  {
288      /* if you have set the absolute coordinates this overrides all other changes */
289    if (likely(this->parent != NULL))
290      *this->toCoordinate = absCoordSoft - parent->getAbsCoor ();
291    else
292      *this->toCoordinate = absCoordSoft;
293  }
294  if( this->parentMode & PNODE_ROTATE_MOVEMENT)
295  {
296    if (likely(this->parent != NULL))
297      *this->toCoordinate = absCoordSoft - parent->getAbsCoor ();
298    else
299      *this->toCoordinate = absCoordSoft;
300  }
301}
302
303
304/**
305 * @brief shift coordinate relative
306 * @param shift shift vector
307 *
308 * this function shifts the current coordinates about the vector shift. this is
309 * usefull because from some place else you can:
310 * PNode* someNode = ...;
311 * Vector objectMovement = calculateShift();
312 * someNode->shiftCoor(objectMovement);
313 *
314 * this is the internal method of:
315 * PNode* someNode = ...;
316 * Vector objectMovement = calculateShift();
317 * Vector currentCoor = someNode->getRelCoor();
318 * Vector newCoor = currentCoor + objectMovement;
319 * someNode->setRelCoor(newCoor);
320 *
321 */
322void PNode::shiftCoor (const Vector& shift)
323{
324  this->relCoordinate += shift;
325  this->bRelCoorChanged = true;
326}
327
328
329/**
330 * @brief set relative direction
331 * @param relDir to its parent
332 */
333void PNode::setRelDir (const Quaternion& relDir)
334{
335  if (this->toDirection!= NULL)
336  {
337    delete this->toDirection;
338    this->toDirection = NULL;
339  }
340  this->relDirection = relDir;
341
342  this->bRelCoorChanged = true;
343}
344
345
346/**
347 * @see void PNode::setRelDir (const Quaternion& relDir)
348 * @param x the x direction
349 * @param y the y direction
350 * @param z the z direction
351 *
352 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
353 */
354void PNode::setRelDir (float x, float y, float z)
355{
356  this->setRelDir(Quaternion(Vector(x,y,z), Vector(0,1,0)));
357}
358
359
360/**
361 * @brief sets the Relative Direction of this node to its parent in a Smoothed way
362 * @param relDirSoft the direction to iterate to smoothely.
363 * @param bias how fast to iterate to the new Direction
364 */
365void PNode::setRelDirSoft(const Quaternion& relDirSoft, float bias)
366{
367  if (likely(this->toDirection == NULL))
368    this->toDirection = new Quaternion();
369
370  *this->toDirection = relDirSoft;
371  this->bias = bias;
372  this->bRelDirChanged = true;
373}
374
375
376/**
377 * @see void PNode::setRelDirSoft (const Quaternion& relDir)
378 * @param x the x direction
379 * @param y the y direction
380 * @param z the z direction
381 *
382 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
383 */
384void PNode::setRelDirSoft(float x, float y, float z, float bias)
385{
386  this->setRelDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias);
387}
388
389
390/**
391 * @brief sets the absolute direction
392 * @param absDir absolute coordinates
393 */
394void PNode::setAbsDir (const Quaternion& absDir)
395{
396  if (this->toDirection!= NULL)
397  {
398    delete this->toDirection;
399    this->toDirection = NULL;
400  }
401
402  if (likely(this->parent != NULL))
403    this->relDirection = absDir / this->parent->getAbsDir();
404  else
405   this->relDirection = absDir;
406
407  this->bRelDirChanged = true;
408}
409
410
411/**
412 * @see void PNode::setAbsDir (const Quaternion& relDir)
413 * @param x the x direction
414 * @param y the y direction
415 * @param z the z direction
416 *
417 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
418 */
419void PNode::setAbsDir (float x, float y, float z)
420{
421  this->setAbsDir(Quaternion(Vector(x,y,z), Vector(0,1,0)));
422}
423
424
425/**
426 * @brief sets the absolute direction
427 * @param absDir absolute coordinates
428 * @param bias how fast to iterator to the new Position
429 */
430void PNode::setAbsDirSoft (const Quaternion& absDirSoft, float bias)
431{
432  if (this->toDirection == NULL)
433    this->toDirection = new Quaternion();
434
435  if (likely(this->parent != NULL))
436    *this->toDirection = absDirSoft / this->parent->getAbsDir();
437  else
438   *this->toDirection = absDirSoft;
439
440  this->bias = bias;
441  this->bRelDirChanged = true;
442}
443
444
445/**
446 * @see void PNode::setAbsDir (const Quaternion& relDir)
447 * @param x the x direction
448 * @param y the y direction
449 * @param z the z direction
450 *
451 * main difference is, that here you give a directional vector, that will be translated into a Quaternion
452 */
453void PNode::setAbsDirSoft (float x, float y, float z, float bias)
454{
455  this->setAbsDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias);
456}
457
458
459/**
460 * @brief shift Direction
461 * @param shift the direction around which to shift.
462 */
463void PNode::shiftDir (const Quaternion& shift)
464{
465  this->relDirection = this->relDirection * shift;
466  this->bRelDirChanged = true;
467}
468
469
470/**
471 * @brief adds a child and makes this node to a parent
472 * @param child child reference
473 * use this to add a child to this node.
474 */
475void PNode::addChild (PNode* child)
476{
477  if( likely(child->parent != NULL))
478      child->parent->eraseChild(child);
479  if (this->checkIntegrity(child))
480  {
481    child->parent = this;
482    if (unlikely(this != NULL))
483      this->children.push_back(child);
484    child->parentCoorChanged();
485  }
486  else
487  {
488    PRINTF(1)("Tried to reparent to own child '%s::%s' to '%s::%s'.\n",
489              this->getClassName(), this->getName(), child->getClassName(), child->getName());
490    child->parent = NULL;
491    child->parentCoorChanged();
492  }
493}
494
495
496/**
497 * @see PNode::addChild(PNode* child);
498 * @param childName the name of the child to add to this PNode
499 */
500void PNode::addChild (const char* childName)
501{
502  PNode* childNode = dynamic_cast<PNode*>(ClassList::getObject(childName, CL_PARENT_NODE));
503//  PRINTF(0)("Adding the Child: %s to: %s\n", childName, this->getName());
504//  assert( childNode != NULL );
505  if (childNode != NULL)
506  {
507    this->addChild(childNode);
508  }
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
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          Quaternion rotQuat = Quaternion::quatSlerp(this->relDirection,*this->toDirection, fabsf(dt)*this->bias);
750          if (this->relDirection.distance(rotQuat) >PNODE_ITERATION_DELTA)
751          {
752            this->relDirection = rotQuat;
753            this->bRelDirChanged;
754          }
755          else
756          {
757            delete this->toDirection;
758            this->toDirection = NULL;
759            PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
760          }
761        }
762
763        // MAIN UPDATE /////////////////////////////////////
764        this->lastAbsCoordinate = this->absCoordinate;
765
766        PRINTF(5)("PNode::update - '%s::%s' - (%f, %f, %f)\n", this->getClassName(), this->getName(),
767                      this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
768
769
770        if(this->bRelDirChanged && this->parentMode & PNODE_LOCAL_ROTATE )
771        {
772          /* update the current absDirection - remember * means rotation around sth.*/
773          this->prevRelCoordinate = this->relCoordinate;
774          this->absDirection = parent->getAbsDir() * this->relDirection;
775        }
776
777        if(likely(this->bRelCoorChanged && this->parentMode & PNODE_MOVEMENT))
778        {
779        /* update the current absCoordinate */
780          this->prevRelCoordinate = this->relCoordinate;
781          this->absCoordinate = this->parent->getAbsCoor() + this->relCoordinate;
782        }
783        else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged)
784        {
785          /* update the current absCoordinate */
786          this->prevRelCoordinate = this->relCoordinate;
787          this->absCoordinate = this->parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate);
788        }
789        /////////////////////////////////////////////////
790      }
791
792  else // Nodes without a Parent are handled faster :: MOST LIKELY THE NULLPARENT
793    {
794      PRINTF(4)("update ParentLess Node (%s::%s) - (%f, %f, %f)\n", this->getClassName(), this->getName(),
795                this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
796        if (this->bRelCoorChanged)
797        {
798          this->prevRelCoordinate = this->relCoordinate;
799          this->absCoordinate = this->relCoordinate;
800        }
801        if (this->bRelDirChanged)
802        {
803          this->prevRelDirection = this->relDirection;
804          this->absDirection = this->getAbsDir () * this->relDirection;
805        }
806      }
807    }
808
809    if(!this->children.empty() && (this->bActive || this->parentMode & PNODE_UPDATE_CHILDREN_IF_INACTIVE ))
810    {
811      list<PNode*>::iterator child;
812      for (child = this->children.begin(); child != this->children.end(); child ++)
813      {
814        /* if this node has changed, make sure, that all children are updated also */
815        if( likely(this->bRelCoorChanged))
816          (*child)->parentCoorChanged ();
817        if( likely(this->bRelDirChanged))
818          (*child)->parentDirChanged ();
819
820        (*child)->updateNode(dt);
821      }
822    }
823    this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
824    this->bRelCoorChanged = false;
825    this->bRelDirChanged = false;
826}
827
828
829
830
831
832/*************
833 * DEBUGGING *
834 *************/
835/**
836 * @brief counts total amount the children walking through the entire tree.
837 * @param nodes the counter
838 */
839void PNode::countChildNodes(int& nodes) const
840{
841  nodes++;
842  list<PNode*>::const_iterator child;
843  for (child = this->children.begin(); child != this->children.end(); child ++)
844    (*child)->countChildNodes(nodes);
845}
846
847
848/**
849 * @brief displays some information about this pNode
850 * @param depth The deph into which to debug the children of this PNode to.
851 * (0: all children will be debugged, 1: only this PNode, 2: this and direct children, ...)
852 * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output).
853 */
854void PNode::debugNode(unsigned int depth, unsigned int level) const
855{
856  for (unsigned int i = 0; i < level; i++)
857    PRINT(0)(" |");
858  if (this->children.size() > 0)
859    PRINT(0)(" +");
860  else
861    PRINT(0)(" -");
862
863  int childNodeCount = 0;
864  this->countChildNodes(childNodeCount);
865
866  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",
867           this->getClassName(),
868           this->getName(),
869           this->absCoordinate.x,
870           this->absCoordinate.y,
871           this->absCoordinate.z,
872           this->relCoordinate.x,
873           this->relCoordinate.y,
874           this->relCoordinate.z,
875           this->getAbsDirV().x,
876           this->getAbsDirV().y,
877           this->getAbsDirV().z,
878           this->parentingModeToChar(parentMode),
879           childNodeCount);
880  if (depth >= 2 || depth == 0)
881  {
882    list<PNode*>::const_iterator child;
883    for (child = this->children.begin(); child != this->children.end(); child ++)
884    {
885      if (depth == 0)
886        (*child)->debugNode(0, level + 1);
887      else
888        (*child)->debugNode(depth - 1, level +1);
889    }
890  }
891}
892
893/**
894 * @brief displays the PNode at its position with its rotation as a cube.
895 * @param  depth The deph into which to debug the children of this PNode to.
896 * (0: all children will be displayed, 1: only this PNode, 2: this and direct children, ...)
897 * @param size the Size of the Box to draw.
898 * @param color the color of the Box to display.
899 * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output).
900 */
901void PNode::debugDraw(unsigned int depth, float size, const Vector& color, unsigned int level) const
902{
903  // if this is the first Element we draw
904  if (level == 0)
905  {
906    glPushAttrib(GL_ENABLE_BIT); // save the Enable-attributes
907    glMatrixMode(GL_MODELVIEW);  // goto the ModelView Matrix
908
909    glDisable(GL_LIGHTING);      // disable lighting (we do not need them for just lighting)
910    glDisable(GL_BLEND);         // ''
911    glDisable(GL_TEXTURE_2D);    // ''
912    glDisable(GL_DEPTH_TEST);    // ''
913  }
914
915  glPushMatrix();                // repush the Matrix-stack
916  /* translate */
917  glTranslatef (this->getAbsCoor ().x,
918                this->getAbsCoor ().y,
919                this->getAbsCoor ().z);
920//  this->getAbsDir ().matrix (matrix);
921
922  /* rotate */
923  Vector tmpRot = this->getAbsDir().getSpacialAxis();
924  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
925  /* set the new Color */
926  glColor3f(color.x, color.y, color.z);
927  { /* draw a cube of size size */
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    glBegin(GL_LINE_STRIP);
936    glVertex3f(-.5*size, +.5*size,  -.5*size);
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    glEnd();
942
943    glBegin(GL_LINES);
944    glVertex3f(-.5*size, -.5*size,  -.5*size);
945    glVertex3f(-.5*size, +.5*size,  -.5*size);
946    glVertex3f(+.5*size, -.5*size,  -.5*size);
947    glVertex3f(+.5*size, +.5*size,  -.5*size);
948    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    glEnd();
953  }
954
955  glPopMatrix();
956  if (depth >= 2 || depth == 0)
957  {
958    /* rotate the current color in HSV space around 20 degree */
959    Vector childColor =  Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0));
960    list<PNode*>::const_iterator child;
961    for (child = this->children.begin(); child != this->children.end(); child ++)
962    {
963      // drawing the Dependency graph
964     if (this != PNode::getNullParent())
965      {
966       glBegin(GL_LINES);
967       glColor3f(color.x, color.y, color.z);
968       glVertex3f(this->getAbsCoor ().x,
969                  this->getAbsCoor ().y,
970                  this->getAbsCoor ().z);
971        glColor3f(childColor.x, childColor.y, childColor.z);
972        glVertex3f((*child)->getAbsCoor ().x,
973                   (*child)->getAbsCoor ().y,
974                   (*child)->getAbsCoor ().z);
975        glEnd();
976      }
977
978      /* if we want to draw the children too */
979      if (depth == 0) /* -> all of them */
980        (*child)->debugDraw(0, size, childColor, level+1);
981      else            /* -> only the Next one */
982        (*child)->debugDraw(depth - 1, size, childColor, level +1);
983    }
984  }
985  if (level == 0)
986    glPopAttrib(); /* pop the saved attributes back out */
987}
988
989
990
991/////////////////////
992// HELPER_FUCTIONS //
993/////////////////////
994
995/**
996 * @brief converts a parentingMode into a string that is the name of it
997 * @param parentingMode the ParentingMode to convert
998 * @return the converted string
999 */
1000const char* PNode::parentingModeToChar(int parentingMode)
1001{
1002  if (parentingMode == PNODE_LOCAL_ROTATE)
1003    return "local-rotate";
1004  else if (parentingMode == PNODE_ROTATE_MOVEMENT)
1005    return "rotate-movement";
1006  else if (parentingMode == PNODE_MOVEMENT)
1007    return "movement";
1008  else if (parentingMode == PNODE_ALL)
1009    return "all";
1010  else if (parentingMode == PNODE_ROTATE_AND_MOVE)
1011    return "rotate-and-move";
1012}
1013
1014/**
1015 * @brief converts a parenting-mode-string into a int
1016 * @param parentingMode the string naming the parentingMode
1017 * @return the int corresponding to the named parentingMode
1018 */
1019PARENT_MODE PNode::charToParentingMode(const char* parentingMode)
1020{
1021  if (!strcmp(parentingMode, "local-rotate"))
1022    return (PNODE_LOCAL_ROTATE);
1023  else  if (!strcmp(parentingMode, "rotate-movement"))
1024    return (PNODE_ROTATE_MOVEMENT);
1025  else  if (!strcmp(parentingMode, "movement"))
1026    return (PNODE_MOVEMENT);
1027  else  if (!strcmp(parentingMode, "all"))
1028    return (PNODE_ALL);
1029  else  if (!strcmp(parentingMode, "rotate-and-move"))
1030    return (PNODE_ROTATE_AND_MOVE);
1031}
1032
1033/**
1034 * Writes data from network containing information about the state
1035 * @param data pointer to data
1036 * @param length length of data
1037 * @param sender hostID of sender
1038 */
1039int PNode::writeState( const byte * data, int length, int sender )
1040{
1041  SYNCHELP_READ_BEGIN();
1042
1043  SYNCHELP_READ_FKT( BaseObject::writeState );
1044
1045  char * parentName = NULL;
1046  SYNCHELP_READ_STRINGM( parentName );
1047
1048  if ( strcmp(parentName, "")==0 )
1049  {
1050    setParent( (char*)NULL );
1051  }
1052  else
1053  {
1054    setParent( parentName );
1055  }
1056
1057  delete[] parentName;
1058
1059  int parentMode;
1060  SYNCHELP_READ_INT( parentMode );
1061  this->setParentMode((PARENT_MODE)parentMode);
1062
1063  float f1, f2, f3, f4;
1064
1065  SYNCHELP_READ_FLOAT( f1 );
1066  SYNCHELP_READ_FLOAT( f2 );
1067  SYNCHELP_READ_FLOAT( f3 );
1068  this->setRelCoor( f1, f2, f3 );
1069
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  int n;
1078  char * childName;
1079
1080  SYNCHELP_READ_INT( n );
1081
1082  for (int i = 0; i<n; i++)
1083  {
1084    SYNCHELP_READ_STRINGM( childName );
1085    //PRINTF(0)("childname = %s\n", childName);
1086    addChild( childName );
1087    delete childName;
1088    childName = NULL;
1089  }
1090
1091  return SYNCHELP_READ_N;
1092}
1093
1094/**
1095 * data copied in data will bee sent to another host
1096 * @param data pointer to data
1097 * @param maxLength max length of data
1098 * @return the number of bytes writen
1099 */
1100int PNode::readState( byte * data, int maxLength )
1101{
1102  SYNCHELP_WRITE_BEGIN();
1103
1104  SYNCHELP_WRITE_FKT( BaseObject::readState );
1105
1106  if ( this->parent )
1107  {
1108    SYNCHELP_WRITE_STRING( parent->getName() );
1109  }
1110  else
1111  {
1112    SYNCHELP_WRITE_STRING( "" );
1113  }
1114
1115  SYNCHELP_WRITE_INT( this->parentMode );
1116
1117  SYNCHELP_WRITE_FLOAT( this->relCoordinate.x );
1118  SYNCHELP_WRITE_FLOAT( this->relCoordinate.y );
1119  SYNCHELP_WRITE_FLOAT( this->relCoordinate.z );
1120
1121  SYNCHELP_WRITE_FLOAT( this->relDirection.w );
1122  SYNCHELP_WRITE_FLOAT( this->relDirection.v.x );
1123  SYNCHELP_WRITE_FLOAT( this->relDirection.v.y );
1124  SYNCHELP_WRITE_FLOAT( this->relDirection.v.z );
1125
1126  int n = children.size();
1127
1128  //check if camera is in children
1129  for (std::list<PNode*>::const_iterator it = children.begin(); it!=children.end(); it++)
1130  {
1131    if ( (*it)->isA(CL_CAMERA) )
1132      n--;
1133  }
1134  SYNCHELP_WRITE_INT( n );
1135
1136  for (std::list<PNode*>::const_iterator it = children.begin(); it!=children.end(); it++)
1137  {
1138    //dont add camera because there is only one camera attached to local player
1139    if ( !(*it)->isA(CL_CAMERA) )
1140      SYNCHELP_WRITE_STRING( (*it)->getName() );
1141  }
1142
1143  return SYNCHELP_WRITE_N;
1144}
1145
1146#define __FLAG_COOR 1
1147#define __FLAG_ROT  2
1148
1149#define __OFFSET 1.0
1150
1151/**
1152 * Writes data from network containing information about the state which has changed
1153 * @param data pointer to data
1154 * @param length length of data
1155 * @param sender hostID of sender
1156 */
1157int PNode::writeSync( const byte * data, int length, int sender )
1158{
1159  SYNCHELP_READ_BEGIN();
1160
1161  if ( this->getHostID()==this->getOwner() )
1162  {
1163    return SYNCHELP_READ_N;
1164  }
1165
1166  byte flags = 0;
1167  SYNCHELP_READ_BYTE( flags );
1168  //PRINTF(0)("%s::FLAGS = %d\n", this->getName(), flags);
1169
1170  float f1, f2, f3, f4;
1171
1172  if ( flags & __FLAG_COOR )
1173  {
1174    SYNCHELP_READ_FLOAT( f1 );
1175    SYNCHELP_READ_FLOAT( f2 );
1176    SYNCHELP_READ_FLOAT( f3 );
1177    this->setRelCoor( f1, f2, f3 );
1178  }
1179
1180  if ( flags & __FLAG_ROT )
1181  {
1182    SYNCHELP_READ_FLOAT( f1 );
1183    SYNCHELP_READ_FLOAT( f2 );
1184    SYNCHELP_READ_FLOAT( f3 );
1185    SYNCHELP_READ_FLOAT( f4 );
1186    this->setRelDir( Quaternion( Vector(f2, f3, f4), f1 ) );
1187  }
1188
1189  return SYNCHELP_READ_N;
1190}
1191
1192/**
1193 * data copied in data will bee sent to another host
1194 * @param data pointer to data
1195 * @param maxLength max length of data
1196 * @return the number of bytes writen
1197 */
1198int PNode::readSync( byte * data, int maxLength )
1199{
1200  SYNCHELP_WRITE_BEGIN();
1201
1202  if ( this->getHostID()!=this->getOwner() )
1203  {
1204    return SYNCHELP_WRITE_N;
1205  }
1206
1207  static float coorx = relCoordinate.x + 1;
1208  static float coory = relCoordinate.y + 1;
1209  static float coorz = relCoordinate.z + 1;
1210
1211  static float rotw = relDirection.w + 1;
1212  static float rotx = relDirection.v.x + 1;
1213  static float roty = relDirection.v.y + 1;
1214  static float rotz = relDirection.v.z + 1;
1215
1216  byte flags = 0;
1217  if ( fabs( coorx - relCoordinate.x ) > __OFFSET ||
1218       fabs( coory - relCoordinate.y ) > __OFFSET ||
1219       fabs( coorz - relCoordinate.z ) > __OFFSET )
1220    flags |= __FLAG_COOR;
1221
1222  if ( fabs( rotw - relDirection.w ) > __OFFSET ||
1223       fabs( rotx - relDirection.v.x ) > __OFFSET ||
1224       fabs( roty - relDirection.v.y ) > __OFFSET ||
1225       fabs( rotz - relDirection.v.z ) > __OFFSET )
1226    flags |= __FLAG_ROT;
1227
1228  SYNCHELP_WRITE_BYTE( flags );
1229  //PRINTF(0)("FLAGS = %d\n", flags);
1230
1231  coorx = relCoordinate.x;
1232  coory = relCoordinate.y;
1233  coorz = relCoordinate.z;
1234
1235
1236  rotw = relDirection.w;
1237  rotx = relDirection.v.x;
1238  roty = relDirection.v.y;
1239  rotz = relDirection.v.z;
1240
1241
1242  if ( flags & __FLAG_COOR )
1243  {
1244
1245    PRINTF(0)("%f %f %f\n", coorx, coory, coorz);
1246
1247    SYNCHELP_WRITE_FLOAT( this->relCoordinate.x );
1248    SYNCHELP_WRITE_FLOAT( this->relCoordinate.y );
1249    SYNCHELP_WRITE_FLOAT( this->relCoordinate.z );
1250  }
1251
1252  if ( flags & __FLAG_ROT )
1253  {
1254
1255    PRINTF(0)("%f %f %f %f\n", rotw, rotx, roty, rotz);
1256
1257    SYNCHELP_WRITE_FLOAT( this->relDirection.w );
1258    SYNCHELP_WRITE_FLOAT( this->relDirection.v.x );
1259    SYNCHELP_WRITE_FLOAT( this->relDirection.v.y );
1260    SYNCHELP_WRITE_FLOAT( this->relDirection.v.z );
1261  }
1262
1263  return SYNCHELP_WRITE_N;
1264}
Note: See TracBrowser for help on using the repository browser.