Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

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

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

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