Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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