Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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