Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

comitted with strange bug :D

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