Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: fixed a nasty PNode bug …
@patrick: you were right about it being still debugged :)

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