Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6285 was 6253, checked in by bensch, 19 years ago

orxonox/trunk: fixed the *final* pnode bug

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