Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/qt_gui/src/lib/coord/p_node.cc @ 7485

Last change on this file since 7485 was 7428, checked in by bensch, 19 years ago

orxonox/trunk: move the Pollution of the ShellCommand to some other File

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