Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: fixed a nasty PNode bug …
@patrick: you were right about it being still debugged :)

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