Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: saved pnode again

File size: 37.7 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)
[7008]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++;
[7008]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)
[7008]104  {
105    this->parent->eraseChild(this);
106    this->parent = NULL;
107  }
[6071]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)
[7008]129  .describe("Sets The relative position of the Node to its parent.");
[4771]130
[5671]131  LoadParam(root, "abs-coor", this, PNode, setAbsCoor)
[7008]132  .describe("Sets The absolute Position of the Node.");
[4610]133
[5671]134  LoadParam(root, "rel-dir", this, PNode, setRelDir)
[7008]135  .describe("Sets The relative rotation of the Node to its parent.");
[4761]136
[5671]137  LoadParam(root, "abs-dir", this, PNode, setAbsDir)
[7008]138  .describe("Sets The absolute rotation of the Node.");
[4771]139
[5671]140  LoadParam(root, "parent", this, PNode, setParent)
[7008]141  .describe("the Name of the Parent to set for this PNode");
[4765]142
[5671]143  LoadParam(root, "parent-mode", this, PNode, setParentMode)
[7008]144  .describe("the mode to connect this node to its parent ()");
[4765]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)
[7008]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  {
[7008]247    /* if you have set the absolute coordinates this overrides all other changes */
[4993]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;
[7008]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  {
[7008]289    /* if you have set the absolute coordinates this overrides all other changes */
[5406]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
[7008]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
[7008]439    *this->toDirection = absDirSoft;
[5414]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))
[7008]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
[7008]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));
[7008]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)
[7008]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
[7008]654  if (this->parentMode & PNODE_ROTATE_MOVEMENT && this->parent != NULL)
655    this->relCoordinate = this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor());
656  else
657    this->relCoordinate = tmpV - parentNode->getAbsCoor();
[4991]658
[7008]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    {
[7008]761      // movement for nodes with smoothMove enabled
762      if (unlikely(this->toCoordinate != NULL))
763      {
764        float shiftLen = fabsf(dt)*bias;
765        if (shiftLen >= 1.0)
766          shiftLen = 1.0;
767        Vector moveVect = (*this->toCoordinate - this->relCoordinate) * shiftLen;
768        if (likely(moveVect.len() >= PNODE_ITERATION_DELTA))
[4987]769        {
[7008]770          this->shiftCoor(moveVect);
[4987]771        }
[7008]772        else
[4987]773        {
[7008]774          delete this->toCoordinate;
775          this->toCoordinate = NULL;
776          PRINTF(5)("SmoothMove of %s finished\n", this->getName());
[4987]777        }
[7008]778      }
779      if (unlikely(this->toDirection != NULL))
780      {
781        float shiftLen = fabsf(dt)*bias;
782        if (shiftLen >= 1.0)
783          shiftLen = 1.0;
784        //printf("%s::%s %f\n", this->getClassName(), this->getName(), this->toStep );
785        Quaternion rotQuat = Quaternion::quatSlerp(this->relDirection,*this->toDirection, shiftLen);
786        if (this->relDirection.distance(rotQuat) > PNODE_ITERATION_DELTA)
787        {
788          this->relDirection = rotQuat;
789          this->bRelDirChanged;
790        }
791        else
792        {
793          delete this->toDirection;
794          this->toDirection = NULL;
795          PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
796          this->bRelDirChanged;
797        }
798      }
[4990]799
[7008]800      // MAIN UPDATE /////////////////////////////////////
801      this->lastAbsCoordinate = this->absCoordinate;
[4145]802
[7008]803      PRINTF(5)("PNode::update - '%s::%s' - (%f, %f, %f)\n", this->getClassName(), this->getName(),
804                this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
[3800]805
[4570]806
[7008]807      if(this->bRelDirChanged && this->parentMode & PNODE_LOCAL_ROTATE )
808      {
809        /* update the current absDirection - remember * means rotation around sth.*/
810        this->prevRelCoordinate = this->relCoordinate;
811        this->absDirection = parent->getAbsDir() * this->relDirection;
812      }
[4570]813
[7008]814      if(likely(this->bRelCoorChanged && this->parentMode & PNODE_MOVEMENT))
815      {
[6075]816        /* update the current absCoordinate */
[7008]817        this->prevRelCoordinate = this->relCoordinate;
818        this->absCoordinate = this->parent->getAbsCoor() + this->relCoordinate;
[5007]819      }
[7008]820      else if( this->parentMode & PNODE_ROTATE_MOVEMENT && (this->bRelCoorChanged || this->bRelDirChanged))
821      {
822        /* update the current absCoordinate */
823        this->prevRelCoordinate = this->relCoordinate;
824        this->absCoordinate = this->parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate);
825      }
826      /////////////////////////////////////////////////
827    }
[6075]828
[7008]829    else // Nodes without a Parent are handled faster :: MOST LIKELY THE NULLPARENT
[4440]830    {
[6075]831      PRINTF(4)("update ParentLess Node (%s::%s) - (%f, %f, %f)\n", this->getClassName(), this->getName(),
832                this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
[7008]833      if (this->bRelCoorChanged)
834      {
835        this->prevRelCoordinate = this->relCoordinate;
836        this->absCoordinate = this->relCoordinate;
[5118]837      }
[7008]838      if (this->bRelDirChanged)
839      {
840        this->prevRelDirection = this->relDirection;
841        this->absDirection = this->getAbsDir () * this->relDirection;
842      }
[4993]843    }
[7008]844  }
[3365]845
[7008]846  if(!this->children.empty() && (this->bActive || this->parentMode & PNODE_UPDATE_CHILDREN_IF_INACTIVE ))
847  {
848    list<PNode*>::iterator child;
849    for (child = this->children.begin(); child != this->children.end(); child ++)
[4993]850    {
[7008]851      /* if this node has changed, make sure, that all children are updated also */
852      if( likely(this->bRelCoorChanged))
853        (*child)->parentCoorChanged ();
854      if( likely(this->bRelDirChanged))
855        (*child)->parentDirChanged ();
[4993]856
[7008]857      (*child)->updateNode(dt);
[4440]858    }
[7008]859  }
860  this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
861  this->bRelCoorChanged = false;
862  this->bRelDirChanged = false;
[3365]863}
864
[5769]865
[6075]866
867
868
869/*************
870 * DEBUGGING *
871 *************/
[6048]872/**
873 * @brief counts total amount the children walking through the entire tree.
874 * @param nodes the counter
875 */
[5769]876void PNode::countChildNodes(int& nodes) const
877{
878  nodes++;
[5770]879  list<PNode*>::const_iterator child;
880  for (child = this->children.begin(); child != this->children.end(); child ++)
881    (*child)->countChildNodes(nodes);
[5769]882}
883
884
[3450]885/**
[6048]886 * @brief displays some information about this pNode
[4836]887 * @param depth The deph into which to debug the children of this PNode to.
[5383]888 * (0: all children will be debugged, 1: only this PNode, 2: this and direct children, ...)
889 * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output).
[5420]890 */
[5769]891void PNode::debugNode(unsigned int depth, unsigned int level) const
[3365]892{
[4574]893  for (unsigned int i = 0; i < level; i++)
[4575]894    PRINT(0)(" |");
[5770]895  if (this->children.size() > 0)
[4575]896    PRINT(0)(" +");
897  else
898    PRINT(0)(" -");
[5769]899
900  int childNodeCount = 0;
901  this->countChildNodes(childNodeCount);
902
903  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]904           this->getClassName(),
905           this->getName(),
906           this->absCoordinate.x,
907           this->absCoordinate.y,
908           this->absCoordinate.z,
909           this->relCoordinate.x,
910           this->relCoordinate.y,
[4993]911           this->relCoordinate.z,
[4996]912           this->getAbsDirV().x,
913           this->getAbsDirV().y,
914           this->getAbsDirV().z,
[5769]915           this->parentingModeToChar(parentMode),
916           childNodeCount);
[4574]917  if (depth >= 2 || depth == 0)
918  {
[5770]919    list<PNode*>::const_iterator child;
920    for (child = this->children.begin(); child != this->children.end(); child ++)
[4574]921    {
922      if (depth == 0)
[5770]923        (*child)->debugNode(0, level + 1);
[4574]924      else
[5770]925        (*child)->debugNode(depth - 1, level +1);
[4574]926    }
927  }
[3365]928}
929
[4570]930/**
[6048]931 * @brief displays the PNode at its position with its rotation as a cube.
[5383]932 * @param  depth The deph into which to debug the children of this PNode to.
933 * (0: all children will be displayed, 1: only this PNode, 2: this and direct children, ...)
934 * @param size the Size of the Box to draw.
935 * @param color the color of the Box to display.
936 * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output).
937 */
938void PNode::debugDraw(unsigned int depth, float size, const Vector& color, unsigned int level) const
[4570]939{
[5432]940  // if this is the first Element we draw
[5383]941  if (level == 0)
942  {
[5432]943    glPushAttrib(GL_ENABLE_BIT); // save the Enable-attributes
944    glMatrixMode(GL_MODELVIEW);  // goto the ModelView Matrix
[5383]945
[5432]946    glDisable(GL_LIGHTING);      // disable lighting (we do not need them for just lighting)
947    glDisable(GL_BLEND);         // ''
948    glDisable(GL_TEXTURE_2D);    // ''
[5438]949    glDisable(GL_DEPTH_TEST);    // ''
[5383]950  }
951
[5432]952  glPushMatrix();                // repush the Matrix-stack
[4570]953  /* translate */
954  glTranslatef (this->getAbsCoor ().x,
955                this->getAbsCoor ().y,
956                this->getAbsCoor ().z);
[7008]957  //  this->getAbsDir ().matrix (matrix);
[4998]958
[5432]959  /* rotate */
[4998]960  Vector tmpRot = this->getAbsDir().getSpacialAxis();
[5432]961  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
962  /* set the new Color */
[5008]963  glColor3f(color.x, color.y, color.z);
[5432]964  { /* draw a cube of size size */
[4570]965    glBegin(GL_LINE_STRIP);
[4995]966    glVertex3f(-.5*size, -.5*size,  -.5*size);
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);
[4570]971    glEnd();
972    glBegin(GL_LINE_STRIP);
[4995]973    glVertex3f(-.5*size, +.5*size,  -.5*size);
974    glVertex3f(+.5*size, +.5*size,  -.5*size);
975    glVertex3f(+.5*size, +.5*size,  +.5*size);
976    glVertex3f(-.5*size, +.5*size,  +.5*size);
977    glVertex3f(-.5*size, +.5*size,  -.5*size);
[4570]978    glEnd();
[4995]979
[4570]980    glBegin(GL_LINES);
[4995]981    glVertex3f(-.5*size, -.5*size,  -.5*size);
982    glVertex3f(-.5*size, +.5*size,  -.5*size);
983    glVertex3f(+.5*size, -.5*size,  -.5*size);
984    glVertex3f(+.5*size, +.5*size,  -.5*size);
985    glVertex3f(+.5*size, -.5*size,  +.5*size);
986    glVertex3f(+.5*size, +.5*size,  +.5*size);
987    glVertex3f(-.5*size, -.5*size,  +.5*size);
988    glVertex3f(-.5*size, +.5*size,  +.5*size);
[4570]989    glEnd();
990  }
[6780]991  glPopMatrix();
[4570]992
[5007]993  if (depth >= 2 || depth == 0)
994  {
[5432]995    /* rotate the current color in HSV space around 20 degree */
[5115]996    Vector childColor =  Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0));
[5770]997    list<PNode*>::const_iterator child;
998    for (child = this->children.begin(); child != this->children.end(); child ++)
[5007]999    {
[5394]1000      // drawing the Dependency graph
[7008]1001      if (this != PNode::getNullParent())
[5394]1002      {
[7008]1003        glBegin(GL_LINES);
1004        glColor3f(color.x, color.y, color.z);
1005        glVertex3f(this->getAbsCoor ().x,
1006                   this->getAbsCoor ().y,
1007                   this->getAbsCoor ().z);
[5394]1008        glColor3f(childColor.x, childColor.y, childColor.z);
[5770]1009        glVertex3f((*child)->getAbsCoor ().x,
1010                   (*child)->getAbsCoor ().y,
1011                   (*child)->getAbsCoor ().z);
[5394]1012        glEnd();
1013      }
[5915]1014
[5432]1015      /* if we want to draw the children too */
1016      if (depth == 0) /* -> all of them */
[5770]1017        (*child)->debugDraw(0, size, childColor, level+1);
[5432]1018      else            /* -> only the Next one */
[5770]1019        (*child)->debugDraw(depth - 1, size, childColor, level +1);
[5007]1020    }
1021  }
[5383]1022  if (level == 0)
[5432]1023    glPopAttrib(); /* pop the saved attributes back out */
[4570]1024}
[4993]1025
1026
1027
1028/////////////////////
1029// HELPER_FUCTIONS //
1030/////////////////////
1031
1032/**
[6048]1033 * @brief converts a parentingMode into a string that is the name of it
[4993]1034 * @param parentingMode the ParentingMode to convert
1035 * @return the converted string
1036 */
1037const char* PNode::parentingModeToChar(int parentingMode)
1038{
1039  if (parentingMode == PNODE_LOCAL_ROTATE)
1040    return "local-rotate";
1041  else if (parentingMode == PNODE_ROTATE_MOVEMENT)
1042    return "rotate-movement";
1043  else if (parentingMode == PNODE_MOVEMENT)
1044    return "movement";
1045  else if (parentingMode == PNODE_ALL)
1046    return "all";
1047  else if (parentingMode == PNODE_ROTATE_AND_MOVE)
1048    return "rotate-and-move";
1049}
1050
1051/**
[6048]1052 * @brief converts a parenting-mode-string into a int
[4993]1053 * @param parentingMode the string naming the parentingMode
1054 * @return the int corresponding to the named parentingMode
1055 */
1056PARENT_MODE PNode::charToParentingMode(const char* parentingMode)
1057{
1058  if (!strcmp(parentingMode, "local-rotate"))
1059    return (PNODE_LOCAL_ROTATE);
1060  else  if (!strcmp(parentingMode, "rotate-movement"))
1061    return (PNODE_ROTATE_MOVEMENT);
1062  else  if (!strcmp(parentingMode, "movement"))
1063    return (PNODE_MOVEMENT);
1064  else  if (!strcmp(parentingMode, "all"))
1065    return (PNODE_ALL);
1066  else  if (!strcmp(parentingMode, "rotate-and-move"))
1067    return (PNODE_ROTATE_AND_MOVE);
1068}
[6341]1069
1070/**
1071 * Writes data from network containing information about the state
1072 * @param data pointer to data
1073 * @param length length of data
1074 * @param sender hostID of sender
1075 */
1076int PNode::writeState( const byte * data, int length, int sender )
1077{
1078  SYNCHELP_READ_BEGIN();
1079
[6815]1080  SYNCHELP_READ_FKT( BaseObject::writeState, NWT_PN_BO_WRITESTATE );
[6341]1081
[7008]1082  //   char * parentName = NULL;
1083  //   SYNCHELP_READ_STRINGM( parentName );
1084  //
1085  //   if ( strcmp(parentName, "")==0 )
1086  //   {
1087  //     setParent( (char*)NULL );
1088  //   }
1089  //   else
1090  //   {
1091  //     setParent( parentName );
1092  //   }
1093  //
1094  //  delete[] parentName;
[6341]1095
1096  int parentMode;
[6815]1097  SYNCHELP_READ_INT( parentMode, NWT_PN_PARENTMODE );
[6341]1098  this->setParentMode((PARENT_MODE)parentMode);
1099
1100  float f1, f2, f3, f4;
1101
[6815]1102  SYNCHELP_READ_FLOAT( f1, NWT_PN_COORX );
1103  SYNCHELP_READ_FLOAT( f2, NWT_PN_COORY );
1104  SYNCHELP_READ_FLOAT( f3, NWT_PN_COORZ );
[6341]1105  this->setRelCoor( f1, f2, f3 );
1106
1107
[6815]1108  SYNCHELP_READ_FLOAT( f1, NWT_PN_ROTV );
1109  SYNCHELP_READ_FLOAT( f2, NWT_PN_ROTX );
1110  SYNCHELP_READ_FLOAT( f3, NWT_PN_ROTY );
1111  SYNCHELP_READ_FLOAT( f4, NWT_PN_ROTZ );
[6341]1112  this->setRelDir( Quaternion( Vector(f2, f3, f4), f1 ) );
1113
[7008]1114  //   int n;
1115  //   char * childName;
1116  //
1117  //   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]);
1118  //   SYNCHELP_READ_INT( n );
1119  //   PRINTF(0)("read %s:n=%d\n", this->getName(), n);
1120  //
1121  //   for (int i = 0; i<n; i++)
1122  //   {
1123  //     SYNCHELP_READ_STRINGM( childName );
1124  //     PRINTF(0)("RCVD CHILD = %s\n", childName);
1125  //     addChild( childName );
1126  //     delete childName;
1127  //     childName = NULL;
1128  //   }
[6341]1129
1130  return SYNCHELP_READ_N;
1131}
1132
1133/**
1134 * data copied in data will bee sent to another host
1135 * @param data pointer to data
1136 * @param maxLength max length of data
1137 * @return the number of bytes writen
1138 */
1139int PNode::readState( byte * data, int maxLength )
1140{
1141  SYNCHELP_WRITE_BEGIN();
1142
[6815]1143  SYNCHELP_WRITE_FKT( BaseObject::readState, NWT_PN_BO_WRITESTATE );
[6341]1144
[7008]1145  //   if ( this->parent )
1146  //   {
1147  //     SYNCHELP_WRITE_STRING( parent->getName() );
1148  //   }
1149  //   else
1150  //   {
1151  //     SYNCHELP_WRITE_STRING( "" );
1152  //   }
[6341]1153
[6815]1154  SYNCHELP_WRITE_INT( this->parentMode, NWT_PN_PARENTMODE );
[6341]1155
[6815]1156  SYNCHELP_WRITE_FLOAT( this->relCoordinate.x, NWT_PN_COORX );
1157  SYNCHELP_WRITE_FLOAT( this->relCoordinate.y, NWT_PN_COORY );
1158  SYNCHELP_WRITE_FLOAT( this->relCoordinate.z, NWT_PN_COORZ );
[6341]1159
[6815]1160  SYNCHELP_WRITE_FLOAT( this->relDirection.w, NWT_PN_ROTV );
1161  SYNCHELP_WRITE_FLOAT( this->relDirection.v.x, NWT_PN_ROTX );
1162  SYNCHELP_WRITE_FLOAT( this->relDirection.v.y, NWT_PN_ROTY );
1163  SYNCHELP_WRITE_FLOAT( this->relDirection.v.z, NWT_PN_ROTZ );
[6341]1164
[7008]1165  //   int n = children.size();
1166  //   //check if camera is in children
1167  //   for (std::list<PNode*>::const_iterator it = children.begin(); it!=children.end(); it++)
1168  //   {
1169  //     if ( (*it)->isA(CL_CAMERA) )
1170  //       n--;
1171  //   }
1172  //   PRINTF(0)("write %s:n=%d\n", this->getName(), n);
1173  //   SYNCHELP_WRITE_INT( n );
1174  //   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]);
1175  //
1176  //
1177  //   for (std::list<PNode*>::const_iterator it = children.begin(); it!=children.end(); it++)
1178  //   {
1179  //     //dont add camera because there is only one camera attached to local player
1180  //     if ( !(*it)->isA(CL_CAMERA) )
1181  //     {
1182  //       PRINTF(0)("SENDING CHILD: %s\n", (*it)->getName());
1183  //       SYNCHELP_WRITE_STRING( (*it)->getName() );
1184  //     }
1185  //   }
[6341]1186
1187  return SYNCHELP_WRITE_N;
1188}
[6634]1189
1190#define __FLAG_COOR 1
1191#define __FLAG_ROT  2
1192
1193#define __OFFSET_POS 1
1194#define __OFFSET_ROT 0.05
1195
1196/**
1197 * Writes data from network containing information about the state which has changed
1198 * @param data pointer to data
1199 * @param length length of data
1200 * @param sender hostID of sender
1201 */
1202int PNode::writeSync( const byte * data, int length, int sender )
1203{
1204  SYNCHELP_READ_BEGIN();
1205
1206  if ( this->getHostID()==this->getOwner() )
1207  {
1208    return SYNCHELP_READ_N;
1209  }
1210
1211  byte flags = 0;
[6815]1212  SYNCHELP_READ_BYTE( flags, NWT_PN_FLAGS );
[6634]1213  //PRINTF(0)("%s::FLAGS = %d\n", this->getName(), flags);
1214
1215  float f1, f2, f3, f4;
1216
1217  if ( flags & __FLAG_COOR )
1218  {
[6815]1219    SYNCHELP_READ_FLOAT( f1, NWT_PN_SCOORX );
1220    SYNCHELP_READ_FLOAT( f2, NWT_PN_SCOORY );
1221    SYNCHELP_READ_FLOAT( f3, NWT_PN_SCOORZ );
[6634]1222    PRINTF(0)("RCVD COOR: %f %f %f\n", f1, f2, f3);
1223    this->setRelCoor( f1, f2, f3 );
1224  }
1225
1226  if ( flags & __FLAG_ROT )
1227  {
[6815]1228    SYNCHELP_READ_FLOAT( f1, NWT_PN_SROTV );
1229    SYNCHELP_READ_FLOAT( f2, NWT_PN_SROTX );
1230    SYNCHELP_READ_FLOAT( f3, NWT_PN_SROTY );
1231    SYNCHELP_READ_FLOAT( f4, NWT_PN_SROTZ );
[6634]1232    PRINTF(0)("RCVD QUAT: %f %f %f %f\n", f1, f2, f3, f4);
1233    //this->setRelDir( Quaternion( Vector(f2, f3, f4), f1 ) );
1234    Quaternion q;
1235    q.w = f1;
1236    q.v.x = f2;
1237    q.v.y = f3;
1238    q.v.z = f4;
1239    this->setAbsDir( q );
1240  }
1241
1242  return SYNCHELP_READ_N;
1243}
1244
1245/**
1246 * data copied in data will bee sent to another host
1247 * @param data pointer to data
1248 * @param maxLength max length of data
1249 * @return the number of bytes writen
1250 */
1251int PNode::readSync( byte * data, int maxLength )
1252{
[6815]1253  //WARNING: if you change this file make sure you also change needsReadSync
[6634]1254  SYNCHELP_WRITE_BEGIN();
1255
[6959]1256  if ( this->getHostID()!=0 && this->getHostID()!=this->getOwner() )
[6634]1257  {
1258    return SYNCHELP_WRITE_N;
1259  }
1260
1261  byte flags = 0;
[6959]1262  if ( fabs( coorx - relCoordinate.x ) > __OFFSET_POS*0.05*this->velocity.len() ||
1263       fabs( coory - relCoordinate.y ) > __OFFSET_POS*0.05*this->velocity.len() ||
1264       fabs( coorz - relCoordinate.z ) > __OFFSET_POS*0.05*this->velocity.len() )
[6634]1265    flags |= __FLAG_COOR;
1266
1267  if ( fabs( rotw - absDirection.w ) > __OFFSET_ROT ||
1268       fabs( rotx - absDirection.v.x ) > __OFFSET_ROT ||
1269       fabs( roty - absDirection.v.y ) > __OFFSET_ROT ||
1270       fabs( rotz - absDirection.v.z ) > __OFFSET_ROT )
1271    flags |= __FLAG_ROT;
1272
1273
[6815]1274  SYNCHELP_WRITE_BYTE( flags, NWT_PN_FLAGS );
[6959]1275  PRINTF(0)("FLAGS = %d\n", flags);
[6634]1276
1277  if ( flags & __FLAG_COOR )
1278  {
1279
[6959]1280    PRINTF(0)("SEND COOR: %f %f %f\n", this->relCoordinate.x, this->relCoordinate.y, this->relCoordinate.z);
[6634]1281
[6815]1282    SYNCHELP_WRITE_FLOAT( this->relCoordinate.x, NWT_PN_SCOORX );
1283    SYNCHELP_WRITE_FLOAT( this->relCoordinate.y, NWT_PN_SCOORY );
1284    SYNCHELP_WRITE_FLOAT( this->relCoordinate.z, NWT_PN_SCOORZ );
[6634]1285
1286    coorx = relCoordinate.x;
1287    coory = relCoordinate.y;
1288    coorz = relCoordinate.z;
1289  }
1290
1291  if ( flags & __FLAG_ROT )
1292  {
1293
[6959]1294    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]1295
[6815]1296    SYNCHELP_WRITE_FLOAT( this->absDirection.w, NWT_PN_SROTV );
1297    SYNCHELP_WRITE_FLOAT( this->absDirection.v.x, NWT_PN_SROTX );
1298    SYNCHELP_WRITE_FLOAT( this->absDirection.v.y, NWT_PN_SROTY );
1299    SYNCHELP_WRITE_FLOAT( this->absDirection.v.z, NWT_PN_SROTZ );
[6634]1300
1301    rotw = absDirection.w;
1302    rotx = absDirection.v.x;
1303    roty = absDirection.v.y;
1304    rotz = absDirection.v.z;
1305  }
1306
1307  return SYNCHELP_WRITE_N;
1308}
[6815]1309
1310bool PNode::needsReadSync( )
1311{
[6959]1312  if ( fabs( coorx - relCoordinate.x ) > __OFFSET_POS*0.05*this->velocity.len() ||
1313       fabs( coory - relCoordinate.y ) > __OFFSET_POS*0.05*this->velocity.len() ||
1314       fabs( coorz - relCoordinate.z ) > __OFFSET_POS*0.05*this->velocity.len() )
[6815]1315    return true;
1316
1317  if ( fabs( rotw - absDirection.w ) > __OFFSET_ROT ||
1318       fabs( rotx - absDirection.v.x ) > __OFFSET_ROT ||
1319       fabs( roty - absDirection.v.y ) > __OFFSET_ROT ||
1320       fabs( rotz - absDirection.v.z ) > __OFFSET_ROT )
1321    return true;
[6873]1322
[6815]1323  return false;
1324}
Note: See TracBrowser for help on using the repository browser.