Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6341 was 6341, checked in by bensch, 18 years ago

orxonox/trunk: merged the network branche back to the trunk, so we do not get away from each other to fast

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