Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7410 was 7221, checked in by bensch, 19 years ago

orxonox/trunk: merged the std-branche back, it runs on windows and Linux

svn merge https://svn.orxonox.net/orxonox/branches/std . -r7202:HEAD

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