Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/parenting/src/p_node.cc @ 3323

Last change on this file since 3323 was 3302, checked in by patrick, 20 years ago

orxonox/branches/parenting: implemented parenting and added to framework. sill got some problems with how to pass events through the new entity list (now part of the parenting-framework). changed to a more accurate way of coordinat-ing, the openGL coord-orientation. therefore the world is realy strange because it flies into the wrong direction.

File size: 10.0 KB
RevLine 
[3246]1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific:
14   main-programmer: Patrick Boenzli
15   co-programmer: ...
[3265]16
17   \todo Null-Parent => center of the coord system - singleton
18   \todo Smooth-Parent: delay, speed
[3276]19   \todo destroy the stuff again, delete...
[3246]20*/
21
22
23#include "p_node.h"
24
25
26using namespace std;
27
28
29/**
30   \brief standard constructor
31
32   \todo this constructor is not jet implemented - do it
33*/
[3248]34PNode::PNode () 
35{
36  this->children = new tList<PNode>();
[3265]37  this->bRelCoorChanged = true;
38  this->bAbsCoorChanged = false;
39  this->bRelDirChanged = true;
40  this->bAbsDirChanged = false;
41  this->parent = NULL;
[3248]42}
[3246]43
44
45/**
[3265]46   \brief constructor with coodinates
47*/
48PNode::PNode (Vector* absCoordinate, PNode* parent )
49{
50  this->absCoordinate = *absCoordinate;
[3269]51  this->relCoordinate = this->absCoordinate - parent->getAbsCoor ();
[3265]52 
53  this->children = new tList<PNode>();
[3269]54  this->bRelCoorChanged = true;
55  this->bAbsCoorChanged = false;
[3265]56  this->bRelDirChanged = true;
57  this->bAbsDirChanged = false;
58  this->parent = parent;
59
60  parent->addChild (this);
61}
62
63
64/**
[3246]65   \brief standard deconstructor
66
67   \todo this deconstructor is not jet implemented - do it
68*/
[3248]69PNode::~PNode () 
70{
[3277]71  /*
72  delete &this->children;
73  delete &this->relCoordinate;
74  delete &this->absCoordinate;
75  delete &this->relDirection;
76  delete &this->absDirection;
77  */
78  this->parent = NULL;
79  /* there is currently a problem with cleaning up - fix*/
[3248]80}
[3246]81
[3247]82
83/**
[3277]84   \brief deletes the hole pnode tree
85
86   cleans up all pnodes
87*/
88void PNode::destroy ()
89{
90  PNode* pn = this->children->enumerate();
91  while( pn != NULL) 
92    { 
93      pn->destroy ();
94      pn = this->children->nextElement();
95    } 
96  this->children->destroy ();
97}
98
99
100/**
[3247]101   \brief get relative coordinates
102   \returns relative coordinates to its parent
103*/
104Vector PNode::getRelCoor ()
[3248]105{
106  Vector r = this->relCoordinate; /* return a copy, so it can't be modified */
107  return r;
108}
[3247]109
110
111/**
112   \brief set relative coordinates
113   \param relative coordinates to its parent
114
115   it is very importand, that you use this function, if you want to update the
116   relCoordinates. If you don't use this, the PNode won't recognize, that something
117   has changed and won't update the children Nodes.
118*/
[3269]119void PNode::setRelCoor (Vector* relCoord)
[3265]120{
121  this->bRelCoorChanged = true;
[3269]122  this->relCoordinate = *relCoord;
[3265]123}
[3247]124
125
126/**
127   \brief get absolute coordinates
128   \returns absolute coordinates from (0,0,0)
129*/
130Vector PNode::getAbsCoor ()
[3265]131{
132  return this->absCoordinate;
133}
[3247]134
135
136/**
137   \brief get relative coordinates
138   \returns relative coordinates to its parent
139
140   it is very importand, that you use this function, if you want to update the
141   absCoordinates. If you don't use this, the PNode won't recognize, that something
142   has changed and won't update the children Nodes.
143*/
[3269]144void PNode::setAbsCoor (Vector* absCoord)
[3265]145{
146  this->bAbsCoorChanged = true;
[3269]147  this->absCoordinate = *absCoord;
[3265]148}
[3247]149
150
151/**
[3248]152   \brief shift coordinate (abs and rel)
153   \param shift vector
154
155   this function shifts the current coordinates about the vector shift. this is
156   usefull because from some place else you can:
157   PNode* someNode = ...;
158   Vector objectMovement = calculateShift();
159   someNode->shiftCoor(objectMovement);
160
161   elsewhere you would have to:
162   PNode* someNode = ...;
163   Vector objectMovement = calculateShift();
164   Vector currentCoor = someNode->getRelCoor();
165   Vector newCoor = currentCoor + objectMovement;
166   someNode->setRelCoor(newCoor);
167   
168   yea right... shorter...
169
170*/
[3265]171void PNode::shiftCoor (Vector* shift)
172{
173  if( this->bAbsCoorChanged)
174    {
175      this->absCoordinate = this->absCoordinate + *shift;
176    }
177  else 
178    {
179      this->relCoordinate = this->relCoordinate + *shift;
180      this->bRelCoorChanged = true;
181    }
182}
[3248]183
184
[3265]185
[3248]186/**
[3247]187   \brief get relative direction
188   \returns relative direction to its parent
189*/
190Quaternion PNode::getRelDir ()
[3265]191{
192  return this->relDirection;
193}
[3247]194
195
196/**
197   \brief set relative direction
198   \param relative direction to its parent
199
200   it is very importand, that you use this function, if you want to update the
201   relDirection. If you don't use this, the PNode won't recognize, that something
202   has changed and won't update the children Nodes.
203*/
[3269]204void PNode::setRelDir (Quaternion* relDir)
[3265]205{
206  this->bRelCoorChanged = true;
[3269]207  this->relDirection = *relDir;
[3265]208}
[3247]209
210
211/**
212   \brief gets the absolute direction (0,0,1)
213   \returns absolute coordinates
214*/
215Quaternion PNode::getAbsDir ()
216{}
217
218
219/**
220   \brief sets the absolute direction (0,0,1)
221   \param absolute coordinates
222
223   it is very importand, that you use this function, if you want to update the
224   absDirection. If you don't use this, the PNode won't recognize, that something
225   has changed and won't update the children Nodes.
226*/
[3269]227void PNode::setAbsDir (Quaternion* absDir)
[3247]228{}
229
230
231/**
[3248]232   \brief shift coordinate (abs and rel)
233   \param shift vector
234
235   this function shifts the current coordinates about the vector shift. this is
236   usefull because from some place else you can:
237   PNode* someNode = ...;
238   Quaternion objectMovement = calculateShift();
239   someNode->shiftCoor(objectMovement);
240
241   elsewhere you would have to:
242   PNode* someNode = ...;
243   Quaternion objectMovement = calculateShift();
244   Quaternion currentCoor = someNode->getRelCoor();
245   Quaternion newCoor = currentCoor + objectMovement;
246   someNode->setRelCoor(newCoor);
247   
248   yea right... shorter...
249
250*/
[3265]251void PNode::shiftDir (Quaternion* shift)
[3248]252{}
253
254
255
256/**
[3247]257   \brief adds a child and makes this node to a parent
258   \param child reference
259
260   use this to add a child to this node.
261*/
262void PNode::addChild (PNode* pNode)
[3248]263{
264  this->addChild(pNode, DEFAULT_MODE);
265}
[3247]266
267
[3248]268/**
269   \brief adds a child and makes this node to a parent
270   \param child reference
271   \param on which changes the child should also change ist state
272
273   use this to add a child to this node.
274*/
275void PNode::addChild (PNode* pNode, parentingMode mode)
276{
277  pNode->mode = mode;
[3265]278  pNode->parent = this;
[3248]279  this->children->add (pNode);
280}
281
282
283/**
284   /brief removes a child from the node
285*/
[3247]286void PNode::removeChild (PNode* pNode)
[3248]287{
288  this->children->remove (pNode);
289}
[3247]290
291
[3248]292/**
293   \brief sets the parent of this PNode
294*/
[3247]295void PNode::setParent (PNode* parent)
[3248]296{
297  this->parent = parent;
298}
[3247]299
[3265]300/**
301   \brief set the mode of this parent manualy
302*/
303void PNode::setMode (parentingMode mode)
304{
305  this->mode = mode;
306}
[3248]307
308/**
[3265]309   \brief has to be called, if the parent coordinate has changed
310   
311   normaly this will be done by the parent itself automaticaly. If you call this, you
312   will force an update of the coordinated of the node.
313*/
314void PNode::parentCoorChanged ()
315{
316  this->bRelCoorChanged = true;
317}
318
319
320/**
321   \brief has to be called, if the parent direction has changed
322   
323   normaly this will be done by the parent itself automaticaly. If you call this, you
324   will force an update of the direction of the node.
325*/
326void PNode::parentDirChanged ()
327{
328  this->bRelDirChanged = true;
329}
330
331
332/**
[3248]333   \brief updates the absCoordinate/absDirection
334
335   this is used to go through the parent-tree to update all the absolute coordinates
336   and directions. this update should be done by the engine, so you don't have to
337   worry, normaly...
338*/
[3302]339void PNode::update (float timeStamp)
[3248]340{
[3302]341  printf ("PNode::update - %s - (%f, %f, %f)\n", this->objectName, this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
342
[3265]343      if( this->mode == MOVEMENT || this->mode == ALL)
344        {
345          if( this->bAbsCoorChanged /*&& this->timeStamp != DataTank::timeStamp*/)
346            {
[3269]347              printf("PNode::update () - this->bAbsCoorChanged = true\n");
[3265]348              /* if you have set the absolute coordinates this overrides all other changes */
349              this->relCoordinate = this->absCoordinate - parent->getAbsCoor ();
350            }
351          else if( this->bRelCoorChanged /*&& this->timeStamp != DataTank::timeStamp*/)
352            {
353              /*this is bad style... must be deleted later - just for testing*/
354              if( this->parent == NULL)
355                {
356                this->absCoordinate = this->relCoordinate;
357                }
358              else
359                this->absCoordinate = parent->getAbsCoor () + this->relCoordinate;            /* update the current absCoordinate */
360            }
361        }
362     
363      if( this->mode == ROTATION && this->mode == ALL)
364        {
365          if( this->bAbsDirChanged /*&& this->timeStamp != DataTank::timeStamp*/)
366            {
367              /* if you have set the absolute coordinates this overrides all other changes */
368              this->relDirection = this->absDirection - parent->getAbsDir ();
369            }
370          else if( this->bRelDirChanged /*&& this->timeStamp != DataTank::timeStamp*/)
371            {
372              /* update the current absDirection - remember * means rotation around sth.*/
373              this->absDirection = parent->getAbsDir () * this->relDirection;
374            }
375        }   
376      // }
[3249]377  PNode* pn = this->children->enumerate();
378  while( pn != NULL) 
379    { 
[3265]380      /* if this node has changed, make sure, that all children are updated also */
381      if( this->bRelCoorChanged || this->bAbsCoorChanged)
382        pn->parentCoorChanged ();
383      if( this->bRelDirChanged || this->bAbsDirChanged)
384        pn->parentDirChanged ();
[3249]385      pn->update(timeStamp);
386      pn = this->children->nextElement();
387    }
388
389  this->timeStamp = timeStamp;
[3265]390  this->bRelCoorChanged = false;
391  this->bAbsCoorChanged = false;
392  this->bRelDirChanged = false;
393  this->bAbsDirChanged = false;
394}
[3249]395
[3265]396
[3302]397/*
398  \brief tick
399*/
400void PNode::processTick (float dt)
401{
402  this->tick (dt);
403  PNode* pn = this->children->enumerate();
404  while( pn != NULL) 
405    { 
406      pn->processTick (dt);
407      pn = this->children->nextElement();
408    } 
409}
410
411
412void PNode::tick (float dt)
413{}
414
415
[3265]416void PNode::debug()
417{
418  printf("PNode::debug() - absCoord: (%f, %f, %f)\n", 
419         this->absCoordinate.x, 
420         this->absCoordinate.y,
421         this->absCoordinate.z);
[3248]422}
[3302]423
424
425/*
426  \brief set the name of the node
427
428  for debug purposes realy usefull, not used to work properly
429*/
430void PNode::setName (char* newName)
431{
432  this->objectName = newName;
433}
434
435
436/*
437  \brief gets the name of the node
438*/
439char* PNode::getName ()
440{
441  return this->objectName;
442}
Note: See TracBrowser for help on using the repository browser.