Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

new network system implemented. yet with a lot of empty function bodys

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