Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6402 was 6402, checked in by patrick, 18 years ago

network: more modularity for the GameWorld: data and process are now totaly separated from each other, as it should be. Now I will do some magic on the MultiPlayerWorld, see if it works :D

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