Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 4993 in orxonox.OLD


Ignore:
Timestamp:
Aug 13, 2005, 11:56:31 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: reimplemented a greate part of PNode, so now it uses only relCoord/relDir to calculate the absolute direction
calling setAbsDir will invoke setRelDir in the process

Location:
orxonox/trunk/src/lib/coord
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/lib/coord/p_node.cc

    r4992 r4993  
    1 
    2 
    31/*
    42   orxonox - the future of 3D-vertical-scrollers
     
    6058}
    6159
    62 
    6360/**
    6461 *  constructor with coodinates
     
    6663 * @param parent The parent-node of this node.
    6764*/
    68 PNode::PNode (const Vector& absCoordinate, PNode* parent )
     65PNode::PNode (const Vector& absCoor, PNode* parent )
    6966{
    7067  this->init(parent);
    7168
    72   this->absCoordinate = absCoordinate;
    73 
    7469  if (likely(parent != NULL))
    75   {
    76     this->relCoordinate = this->absCoordinate - parent->getAbsCoor();
    7770    parent->addChild (this);
    78   }
     71
     72  this->setAbsCoor(absCoor);
    7973}
    8074
     
    107101  this->children = new tList<PNode>();
    108102  this->bRelCoorChanged = true;
    109   this->bAbsCoorChanged = false;
    110103  this->bRelDirChanged = true;
    111   this->bAbsDirChanged = false;
    112104  this->parent = parent;
    113105
    114   this->toPosition = NULL;
     106  // iterators
     107  this->toCoordinate = NULL;
    115108  this->toDirection = NULL;
    116109  this->bias = 1.0;
     
    167160void PNode::setRelCoor (const Vector& relCoord)
    168161{
     162  this->relCoordinate = relCoord;
    169163  this->bRelCoorChanged = true;
    170   this->relCoordinate = relCoord;
    171164}
    172165
     
    176169 * @param y y-relative coordinates to its parent
    177170 * @param z z-relative coordinates to its parent
    178    \see  void PNode::setRelCoor (const Vector& relCoord)
     171 * @see  void PNode::setRelCoor (const Vector& relCoord)
    179172*/
    180173void PNode::setRelCoor (float x, float y, float z)
     
    190183void PNode::setRelCoorSoft(const Vector& relCoordSoft, float bias)
    191184{
    192   if (likely(this->toPosition == NULL))
    193     this->toPosition = new Vector();
    194 
    195   *this->toPosition = relCoordSoft;
     185  if (likely(this->toCoordinate == NULL))
     186    this->toCoordinate = new Vector();
     187
     188  *this->toCoordinate = relCoordSoft;
    196189  this->bias = bias;
    197190}
     
    203196 * @param y y-relative coordinates to its parent
    204197 * @param z z-relative coordinates to its parent
    205    \see  void PNode::setRelCoorSoft (const Vector&, float)
     198 * @see  void PNode::setRelCoorSoft (const Vector&, float)
    206199 */
    207200void PNode::setRelCoorSoft (float x, float y, float z, float bias)
     
    219212void PNode::setAbsCoor (const Vector& absCoord)
    220213{
    221   this->bAbsCoorChanged = true;
    222   this->absCoordinate = absCoord;
     214  if( likely(this->parentMode & PNODE_MOVEMENT))
     215  {
     216      /* if you have set the absolute coordinates this overrides all other changes */
     217    if (likely(this->parent != NULL))
     218      this->relCoordinate = absCoord - parent->getAbsCoor ();
     219    else
     220      this->relCoordinate = absCoord;
     221  }
     222  if( this->parentMode & PNODE_ROTATE_MOVEMENT)
     223  {
     224    if (likely(this->parent != NULL))
     225      this->relCoordinate = absCoord - parent->getAbsCoor ();
     226    else
     227      this->relCoordinate = absCoord;
     228  }
     229
     230  this->bRelCoorChanged = true;
     231//  this->absCoordinate = absCoord;
    223232}
    224233
     
    259268void PNode::shiftCoor (const Vector& shift)
    260269{
    261 
    262   if( unlikely(this->bAbsCoorChanged))
    263     {
    264       this->absCoordinate += shift;
    265     }
    266   else
    267     {
    268       this->relCoordinate += shift;
    269       this->bRelCoorChanged = true;
    270     }
     270  this->relCoordinate += shift;
     271  this->bRelCoorChanged = true;
    271272}
    272273
     
    281282void PNode::setRelDir (const Quaternion& relDir)
    282283{
     284  this->relDirection = relDir;
    283285  this->bRelCoorChanged = true;
    284   this->relDirection = relDir;
    285286}
    286287
     
    334335   absDirection. If you don't use this, the PNode won't recognize, that something
    335336   has changed and won't update the children Nodes.
     337 *
     338 * FIXME ERROR HERE THIS IS IMPLEMENTED FALSELY
     339 * @todo FIXME
    336340*/
    337341void PNode::setAbsDir (const Quaternion& absDir)
    338342{
    339   this->bAbsDirChanged = true;
    340   this->absDirection = absDir;
     343//  if (this->parent)
     344    this->relDirection = absDir;// - parent->getAbsDir();
     345//  else
     346//    this->relDirection = absDir;
     347
     348  this->bRelDirChanged = true;
     349//  this->absDirection = absDir;
    341350}
    342351
     
    357366 *  shift coordinate (abs and rel)
    358367 * @param shift vector
    359 
    360    this function shifts the current coordinates about the vector shift. this is
    361    usefull because from some place else you can:
    362    PNode* someNode = ...;
    363    Quaternion objectMovement = calculateShift();
    364    someNode->shiftCoor(objectMovement);
    365 
    366    elsewhere you would have to:
    367    PNode* someNode = ...;
    368    Quaternion objectMovement = calculateShift();
    369    Quaternion currentCoor = someNode->getRelCoor();
    370    Quaternion newCoor = currentCoor + objectMovement;
    371    someNode->setRelCoor(newCoor);
    372 
    373    yea right... shorter...
    374 
    375    @todo implement this
     368 * @todo implement this
    376369*/
    377370void PNode::shiftDir (const Quaternion& shift)
    378371{
     372  this->relDirection = this->relDirection * shift;
    379373  this->bRelDirChanged = true;
    380   this->relDirection = this->relDirection * shift;
    381374}
    382375
     
    385378 * @param pNode child reference
    386379 * @param parentMode on which changes the child should also change ist state
    387 
    388    use this to add a child to this node.
    389 */
    390 void PNode::addChild (PNode* pNode, int parentMode)
    391 {
    392   if( likely(pNode->parent != NULL))
     380 *
     381 * use this to add a child to this node.
     382*/
     383void PNode::addChild (PNode* child, int parentMode)
     384{
     385  if( likely(child->parent != NULL))
    393386    {
    394387      PRINTF(4)("PNode::addChild() - reparenting node: removing it and adding it again\n");
    395       pNode->parent->children->remove(pNode);
     388      child->parent->children->remove(child);
    396389    }
    397   pNode->parentMode = parentMode;
    398   pNode->parent = this;
    399   this->children->add(pNode);
     390  child->parentMode = parentMode;
     391  child->parent = this;
     392  this->children->add(child);
     393  child->parentCoorChanged();
    400394}
    401395
     
    411405}
    412406
    413 
    414407/**
    415408 *  removes a child from the node
    416409 * @param pNode the child to remove from this pNode.
    417 
    418    Children from pNode will not be lost, they are referenced to NullPointer
    419 */
    420 void PNode::removeChild (PNode* pNode)
    421 {
    422   pNode->remove();
    423   this->children->remove (pNode);
    424   pNode->parent = NULL;
    425 }
    426 
     410 *
     411 * Children from pNode will not be lost, they are referenced to NullPointer
     412*/
     413void PNode::removeChild (PNode* child)
     414{
     415  child->remove();
     416  this->children->remove(child);
     417  child->parent = NULL;
     418}
    427419
    428420/**
     
    447439}
    448440
    449 
    450 /**
    451  *  sets the parent of this PNode
     441/**
     442 * sets the parent of this PNode
    452443 * @param parent the Parent to set
    453444*/
     
    471462 * does the reparenting in a very smooth way
    472463 * @param parentNode the new Node to connect this node to.
     464 * @param bias the speed to iterate to this new Positions
    473465 */
    474466void PNode::softReparent(PNode* parentNode, float bias)
     
    477469    return;
    478470
    479   //this->setRelCoorSoft(this->getRelCoor());
    480   if (likely(this->toPosition == NULL))
     471  if (likely(this->toCoordinate == NULL))
    481472  {
    482     this->toPosition = new Vector();
    483     *this->toPosition = this->getRelCoor();
     473    this->toCoordinate = new Vector();
     474    *this->toCoordinate = this->getRelCoor();
    484475  }
    485476  if (likely(this->toDirection == NULL))
     
    504495}
    505496
     497/**
     498 * does the reparenting in a very smooth way
     499 * @param parentName the name of the Parent to reconnect to
     500 * @param bias the speed to iterate to this new Positions
     501 */
    506502void PNode::softReparent(const char* parentName, float bias)
    507503{
     
    511507}
    512508
    513 
    514 /**
    515  *  set the mode of this parent manualy
    516  * @param parentMode the mode of the bind-type.
    517 */
    518 void PNode::setParentMode (PARENT_MODE parentMode)
    519 {
    520   this->parentMode = parentMode;
    521 }
    522 
    523509/**
    524510 *  sets the mode of this parent manually
     
    527513void PNode::setParentMode (const char* parentingMode)
    528514{
    529   if (!strcmp(parentingMode, "local-rotate"))
    530     this->setParentMode(PNODE_LOCAL_ROTATE);
    531   else  if (!strcmp(parentingMode, "rotate-movement"))
    532     this->setParentMode(PNODE_ROTATE_MOVEMENT);
    533   else  if (!strcmp(parentingMode, "movement"))
    534     this->setParentMode(PNODE_MOVEMENT);
    535   else  if (!strcmp(parentingMode, "all"))
    536     this->setParentMode(PNODE_ALL);
    537   else  if (!strcmp(parentingMode, "rotate-and-move"))
    538     this->setParentMode(PNODE_ROTATE_AND_MOVE);
    539 }
    540 
    541 
    542 /**
    543  *  has to be called, if the parent coordinate has changed
    544 
    545    normaly this will be done by the parent itself automaticaly. If you call this, you
    546    will force an update of the coordinated of the node.
    547 */
    548 /*
    549 void PNode::parentCoorChanged ()
    550 {
    551   this->bRelCoorChanged = true;
    552 }
    553 */
     515  this->setParentMode(PNode::charToParentingMode(parentingMode));
     516}
    554517
    555518/**
     
    566529    {
    567530      // movement for nodes with smoothMove enabled
    568       if (unlikely(this->toPosition != NULL))
     531      if (unlikely(this->toCoordinate != NULL))
    569532      {
    570         Vector moveVect = (*this->toPosition - this->getRelCoor()) *dt*bias;
     533        Vector moveVect = (*this->toCoordinate - this->getRelCoor()) *dt*bias;
    571534
    572535        if (likely(moveVect.len() >= .001))
     
    576539        else
    577540        {
    578           delete this->toPosition;
    579           this->toPosition = NULL;
     541          delete this->toCoordinate;
     542          this->toCoordinate = NULL;
    580543          PRINTF(5)("SmoothMove of %s finished\n", this->getName());
    581544        }
     
    591554/*        else
    592555        {
    593           delete this->toPosition;
    594           this->toPosition = NULL;
     556          delete this->toCoordinate;
     557          this->toCoordinate = NULL;
    595558          PRINTF(5)("SmoothMove of %s finished\n", this->getName());
    596559        }*/
    597560      }
    598561
     562      // MAIN UPDATE /////////////////////////////////////
    599563      this->lastAbsCoordinate = this->absCoordinate;
    600564
    601565      PRINTF(5)("PNode::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
    602566
    603       if( likely(this->parentMode & PNODE_MOVEMENT))
    604         {
    605           if( unlikely(this->bAbsCoorChanged))
    606             {
    607               /* if you have set the absolute coordinates this overrides all other changes */
    608               this->relCoordinate = this->absCoordinate - parent->getAbsCoor ();
    609             }
    610           if( likely(this->bRelCoorChanged))
    611             {
    612               /* update the current absCoordinate */
    613               this->absCoordinate = parent->getAbsCoor() + this->relCoordinate;
    614             }
    615         }
    616 
    617       if( this->parentMode & PNODE_LOCAL_ROTATE)
    618         {
    619           if( unlikely(this->bAbsDirChanged))
    620             {
    621               /* if you have set the absolute coordinates this overrides all other changes */
    622               this->relDirection = this->absDirection - parent->getAbsDir();
    623             }
    624           else if( likely(this->bRelDirChanged))
    625             {
    626               /* update the current absDirection - remember * means rotation around sth.*/
    627               this->absDirection = parent->getAbsDir() * this->relDirection;
    628             }
    629         }
     567      if(likely(this->parentMode & PNODE_MOVEMENT))
     568      {
     569        /* update the current absCoordinate */
     570        this->absCoordinate = this->parent->getAbsCoor() + this->relCoordinate;
     571      }
     572
     573      if( this->parentMode & PNODE_LOCAL_ROTATE && this->bRelDirChanged)
     574      {
     575        /* update the current absDirection - remember * means rotation around sth.*/
     576        this->absDirection = parent->getAbsDir() * this->relDirection;
     577      }
    630578
    631579      if( this->parentMode & PNODE_ROTATE_MOVEMENT)
    632         {
    633           if( unlikely(this->bAbsCoorChanged))
    634             {
    635               /* if you have set the absolute coordinates this overrides all other changes */
    636               this->relCoordinate = this->absCoordinate - parent->getAbsCoor ();
    637             }
    638           else if( likely(this->bRelCoorChanged))
    639             {
    640               /* update the current absCoordinate */
    641               this->absCoordinate = parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate);
    642             }
    643         }
    644 
    645 
    646       tIterator<PNode>* iterator = this->children->getIterator();
    647       PNode* pn = iterator->nextElement();
    648       while( pn != NULL)
    649         {
    650           /* if this node has changed, make sure, that all children are updated also */
    651           if( likely(this->bRelCoorChanged || this->bAbsCoorChanged))
    652             pn->parentCoorChanged ();
    653           if( likely(this->bRelDirChanged || this->bAbsDirChanged))
    654             pn->parentDirChanged ();
    655 
    656           pn->update(dt);
    657           //pn = this->children->nextElement();
    658           pn = iterator->nextElement();
    659         }
    660       delete iterator;
    661 
    662       this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
    663       this->bRelCoorChanged = false;
    664       this->bAbsCoorChanged = false;
    665       this->bRelDirChanged = false;
    666       this->bAbsDirChanged = false;
    667     }
     580      {
     581        /* update the current absCoordinate */
     582        this->absCoordinate = this->parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate);
     583      }
     584      /////////////////////////////////////////////////
     585   }
    668586  else
    669587    {
    670588      PRINTF(4)("NullParent::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
    671       this->absCoordinate = this->relCoordinate;
    672       this->absDirection = this->getAbsDir () * this->relDirection;
    673 
     589      if (this->bRelCoorChanged)
     590        this->absCoordinate = this->relCoordinate;
     591      if (this->bRelDirChanged)
     592        this->absDirection = this->getAbsDir () * this->relDirection;
     593    }
     594
     595    if(this->children->getSize() > 0)
     596    {
    674597      tIterator<PNode>* iterator = this->children->getIterator();
    675       //PNode* pn = this->children->enumerate ();
    676598      PNode* pn = iterator->nextElement();
    677599      while( pn != NULL)
    678600      {
    679601        /* if this node has changed, make sure, that all children are updated also */
    680           if( this->bRelCoorChanged || this->bAbsCoorChanged)
    681             pn->parentCoorChanged ();
    682           if( this->bRelDirChanged || this->bAbsDirChanged)
    683             pn->parentDirChanged ();
    684           pn->update (dt);
    685           //pn = this->children->nextElement ();
    686           pn = iterator->nextElement();
    687         }
     602        if( likely(this->bRelCoorChanged))
     603          pn->parentCoorChanged ();
     604        if( likely(this->bRelDirChanged))
     605          pn->parentDirChanged ();
     606
     607        pn->update(dt);
     608          //pn = this->children->nextElement();
     609        pn = iterator->nextElement();
     610      }
    688611      delete iterator;
    689       this->bRelCoorChanged = false;
    690       this->bAbsCoorChanged = false;
    691       this->bRelDirChanged = false;
    692       this->bAbsDirChanged = false;
    693612    }
     613    this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
     614    this->bRelCoorChanged = false;
     615    this->bRelDirChanged = false;
    694616}
    695617
     
    708630  else
    709631    PRINT(0)(" -");
    710   PRINT(0)("PNode(%s::%s) - absCoord: (%0.2f, %0.2f, %0.2f), relCoord(%0.2f, %0.2f, %0.2f)\n",
     632  PRINT(0)("PNode(%s::%s) - absCoord: (%0.2f, %0.2f, %0.2f), relCoord(%0.2f, %0.2f, %0.2f) - %s\n",
    711633           this->getClassName(),
    712634           this->getName(),
     
    716638           this->relCoordinate.x,
    717639           this->relCoordinate.y,
    718            this->relCoordinate.z );
     640           this->relCoordinate.z,
     641           this->parentingModeToChar(parentMode));
    719642  if (depth >= 2 || depth == 0)
    720643  {
     
    777700  glPopMatrix();
    778701}
     702
     703
     704
     705/////////////////////
     706// HELPER_FUCTIONS //
     707/////////////////////
     708
     709/**
     710 * converts a parentingMode into a string that is the name of it
     711 * @param parentingMode the ParentingMode to convert
     712 * @return the converted string
     713 */
     714const char* PNode::parentingModeToChar(int parentingMode)
     715{
     716  if (parentingMode == PNODE_LOCAL_ROTATE)
     717    return "local-rotate";
     718  else if (parentingMode == PNODE_ROTATE_MOVEMENT)
     719    return "rotate-movement";
     720  else if (parentingMode == PNODE_MOVEMENT)
     721    return "movement";
     722  else if (parentingMode == PNODE_ALL)
     723    return "all";
     724  else if (parentingMode == PNODE_ROTATE_AND_MOVE)
     725    return "rotate-and-move";
     726}
     727
     728/**
     729 * converts a parenting-mode-string into a int
     730 * @param parentingMode the string naming the parentingMode
     731 * @return the int corresponding to the named parentingMode
     732 */
     733PARENT_MODE PNode::charToParentingMode(const char* parentingMode)
     734{
     735  if (!strcmp(parentingMode, "local-rotate"))
     736    return (PNODE_LOCAL_ROTATE);
     737  else  if (!strcmp(parentingMode, "rotate-movement"))
     738    return (PNODE_ROTATE_MOVEMENT);
     739  else  if (!strcmp(parentingMode, "movement"))
     740    return (PNODE_MOVEMENT);
     741  else  if (!strcmp(parentingMode, "all"))
     742    return (PNODE_ALL);
     743  else  if (!strcmp(parentingMode, "rotate-and-move"))
     744    return (PNODE_ROTATE_AND_MOVE);
     745}
  • orxonox/trunk/src/lib/coord/p_node.h

    r4992 r4993  
    11/*!
    2     \file p_node.h
     2    @file p_node.h
    33  *  Definition of a parenting node
    44
     
    5555  PNode ();
    5656  PNode(const TiXmlElement* root);
    57   PNode (const Vector& absCoordinate, PNode* pNode);
     57  PNode (const Vector& absCoor, PNode* pNode);
    5858  virtual ~PNode ();
    5959
     
    8989
    9090  /** @returns the Speed of the Node */
    91   inline float getSpeed() const {return this->velocity.len();}
     91  inline float getSpeed() const { return this->velocity.len(); };
    9292  /** @returns the Velocity of the Node */
    93   inline const Vector& getVelocity() const {return this->velocity;}
     93  inline const Vector& getVelocity() const { return this->velocity; };
    9494
    9595
    96   void addChild (PNode* pNode, int parentingMode = DEFAULT_MODE);
     96  void addChild (PNode* child, int parentingMode = DEFAULT_MODE);
    9797  void addChild (const char* childName);
    98   void removeChild (PNode* pNode);
     98  void removeChild (PNode* child);
    9999  void remove();
    100100
     
    107107  void softReparent(const char* parentName, float bias = 1.0);
    108108
    109   void setParentMode (PARENT_MODE parentMode);
     109  /** @param parentMode sets the parentingMode of this Node */
     110  void setParentMode (PARENT_MODE parentMode) { this->parentMode = parentMode; };
    110111  void setParentMode (const char* parentingMode);
    111112  /** @returns the Parenting mode of this node */
     
    117118  void debugDraw(float size = 1.0) const;
    118119
     120
     121  // helper functions //
     122  static const char* parentingModeToChar(int parentingMode);
     123  static PARENT_MODE charToParentingMode(const char* parentingMode);
    119124 private:
    120125  void init(PNode* parent);
     
    124129  inline void parentDirChanged () { this->bRelDirChanged = true; }
    125130  /** @returns the last calculated coordinate */
    126   inline Vector getLastAbsCoor() {return this->lastAbsCoordinate;}
     131  inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; }
    127132
    128133
    129134 private:
    130   bool            bAbsCoorChanged;    //!< If Absolute Coordinate has changed since last time we checked
    131135  bool            bRelCoorChanged;    //!< If Relative Coordinate has changed since last time we checked
    132   bool            bAbsDirChanged;     //!< If Absolute Direction has changed since last time we checked
    133136  bool            bRelDirChanged;     //!< If Relative Direction has changed since last time we checked
    134137
     
    142145
    143146
    144   Vector*         toPosition;         //!< a position to which to iterate. (This is used in conjunction with softReparent.and set*CoorSoft)
     147  Vector*         toCoordinate;       //!< a position to which to iterate. (This is used in conjunction with softReparent.and set*CoorSoft)
    145148  Quaternion*     toDirection;        //!< a direction to which to iterate. (This is used in conjunction with softReparent and set*DirSoft)
    146149  float           bias;               //!< how fast to iterate to the given position (default is 1)
Note: See TracChangeset for help on using the changeset viewer.