Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the proxy bache back with no conflicts

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