Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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