Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7126 was 7126, checked in by bensch, 18 years ago

orxonox/trunk: the delete Mechanism of PNode seems to work again.
This is done by always deleting the first node, and not walking through a list, that is eleminated during deletion time

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