Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: const Executor introduced

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