Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: const Executor introduced

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