Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6683 was 6682, checked in by patrick, 19 years ago

network: less parenting algoritmics

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