Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6076 was 6075, checked in by bensch, 19 years ago

orxonox/trunk: PNode rearange

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