Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/p_node.cc @ 3438

Last change on this file since 3438 was 3433, checked in by bensch, 19 years ago

orxonox/trunk: merged back the trackManager to the trunk.
merged with command:
svn merge -r 3369:HEAD branches/trackManager trunk
resolved conflicts in world.cc additive differences.

File size: 10.0 KB
Line 
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: ...
16
17   \todo Null-Parent => center of the coord system - singleton
18   \todo Smooth-Parent: delay, speed
19   \todo destroy the stuff again, delete...
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*/
34PNode::PNode () 
35{
36  this->children = new tList<PNode>();
37  this->bRelCoorChanged = true;
38  this->bAbsCoorChanged = false;
39  this->bRelDirChanged = true;
40  this->bAbsDirChanged = false;
41  this->parent = NULL;
42}
43
44
45/**
46   \brief constructor with coodinates
47*/
48PNode::PNode (Vector* absCoordinate, PNode* parent )
49{
50  this->absCoordinate = *absCoordinate;
51  this->relCoordinate = this->absCoordinate - parent->getAbsCoor ();
52 
53  this->children = new tList<PNode>();
54  this->bRelCoorChanged = true;
55  this->bAbsCoorChanged = false;
56  this->bRelDirChanged = true;
57  this->bAbsDirChanged = false;
58  this->parent = parent;
59
60  parent->addChild (this);
61}
62
63
64/**
65   \brief standard deconstructor
66
67   \todo this deconstructor is not jet implemented - do it
68*/
69PNode::~PNode () 
70{
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*/
80}
81
82
83/**
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/**
101   \brief get relative coordinates
102   \returns relative coordinates to its parent
103*/
104Vector PNode::getRelCoor ()
105{
106  Vector r = this->relCoordinate; /* return a copy, so it can't be modified */
107  return r;
108}
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*/
119void PNode::setRelCoor (Vector* relCoord)
120{
121  this->bRelCoorChanged = true;
122  this->relCoordinate = *relCoord;
123}
124
125
126/**
127   \brief get absolute coordinates
128   \returns absolute coordinates from (0,0,0)
129*/
130Vector PNode::getAbsCoor ()
131{
132  return this->absCoordinate;
133}
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*/
144void PNode::setAbsCoor (Vector* absCoord)
145{
146  this->bAbsCoorChanged = true;
147  this->absCoordinate = *absCoord;
148}
149
150
151/**
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*/
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}
183
184
185
186/**
187   \brief get relative direction
188   \returns relative direction to its parent
189*/
190Quaternion PNode::getRelDir ()
191{
192  return this->relDirection;
193}
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*/
204void PNode::setRelDir (Quaternion* relDir)
205{
206  this->bRelCoorChanged = true;
207  this->relDirection = *relDir;
208}
209
210
211/**
212   \brief gets the absolute direction (0,0,1)
213   \returns absolute coordinates
214*/
215Quaternion PNode::getAbsDir ()
216{
217  return this->absDirection;
218}
219
220
221/**
222   \brief sets the absolute direction (0,0,1)
223   \param absolute coordinates
224
225   it is very importand, that you use this function, if you want to update the
226   absDirection. If you don't use this, the PNode won't recognize, that something
227   has changed and won't update the children Nodes.
228*/
229void PNode::setAbsDir (Quaternion* absDir)
230{
231  this->bAbsDirChanged = true;
232  this->absDirection = *absDir;
233}
234
235
236/**
237   \brief shift coordinate (abs and rel)
238   \param shift vector
239
240   this function shifts the current coordinates about the vector shift. this is
241   usefull because from some place else you can:
242   PNode* someNode = ...;
243   Quaternion objectMovement = calculateShift();
244   someNode->shiftCoor(objectMovement);
245
246   elsewhere you would have to:
247   PNode* someNode = ...;
248   Quaternion objectMovement = calculateShift();
249   Quaternion currentCoor = someNode->getRelCoor();
250   Quaternion newCoor = currentCoor + objectMovement;
251   someNode->setRelCoor(newCoor);
252   
253   yea right... shorter...
254
255*/
256void PNode::shiftDir (Quaternion* shift)
257{}
258
259
260
261/**
262   \brief adds a child and makes this node to a parent
263   \param child reference
264
265   use this to add a child to this node.
266*/
267void PNode::addChild (PNode* pNode)
268{
269  this->addChild(pNode, DEFAULT_MODE);
270}
271
272
273/**
274   \brief adds a child and makes this node to a parent
275   \param child reference
276   \param on which changes the child should also change ist state
277
278   use this to add a child to this node.
279*/
280void PNode::addChild (PNode* pNode, parentingMode mode)
281{
282  pNode->mode = mode;
283  pNode->parent = this;
284  this->children->add (pNode);
285}
286
287
288/**
289   /brief removes a child from the node
290*/
291void PNode::removeChild (PNode* pNode)
292{
293  this->children->remove (pNode);
294}
295
296
297/**
298   \brief sets the parent of this PNode
299*/
300void PNode::setParent (PNode* parent)
301{
302  this->parent = parent;
303}
304
305/**
306   \brief set the mode of this parent manualy
307*/
308void PNode::setMode (parentingMode mode)
309{
310  this->mode = mode;
311}
312
313/**
314   \brief has to be called, if the parent coordinate has changed
315   
316   normaly this will be done by the parent itself automaticaly. If you call this, you
317   will force an update of the coordinated of the node.
318*/
319void PNode::parentCoorChanged ()
320{
321  this->bRelCoorChanged = true;
322}
323
324
325/**
326   \brief has to be called, if the parent direction has changed
327   
328   normaly this will be done by the parent itself automaticaly. If you call this, you
329   will force an update of the direction of the node.
330*/
331void PNode::parentDirChanged ()
332{
333  this->bRelDirChanged = true;
334}
335
336
337/**
338   \brief updates the absCoordinate/absDirection
339
340   this is used to go through the parent-tree to update all the absolute coordinates
341   and directions. this update should be done by the engine, so you don't have to
342   worry, normaly...
343*/
344void PNode::update (float timeStamp)
345{
346  printf ("PNode::update - %s - (%f, %f, %f)\n", this->objectName, this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
347
348      if( this->mode == MOVEMENT || this->mode == ALL)
349        {
350          if( this->bAbsCoorChanged /*&& this->timeStamp != DataTank::timeStamp*/)
351            {
352              printf("PNode::update () - this->bAbsCoorChanged = true\n");
353              /* if you have set the absolute coordinates this overrides all other changes */
354              this->relCoordinate = this->absCoordinate - parent->getAbsCoor ();
355            }
356          else if( this->bRelCoorChanged /*&& this->timeStamp != DataTank::timeStamp*/)
357            {
358              /*this is bad style... must be deleted later - just for testing*/
359              if( this->parent == NULL)
360                {
361                this->absCoordinate = this->relCoordinate;
362                }
363              else
364                this->absCoordinate = parent->getAbsCoor () + this->relCoordinate;            /* update the current absCoordinate */
365            }
366        }
367     
368      if( this->mode == ROTATION && this->mode == ALL)
369        {
370          if( this->bAbsDirChanged /*&& this->timeStamp != DataTank::timeStamp*/)
371            {
372              /* if you have set the absolute coordinates this overrides all other changes */
373              this->relDirection = this->absDirection - parent->getAbsDir ();
374            }
375          else if( this->bRelDirChanged /*&& this->timeStamp != DataTank::timeStamp*/)
376            {
377              /* update the current absDirection - remember * means rotation around sth.*/
378              this->absDirection = parent->getAbsDir () * this->relDirection;
379            }
380        }   
381      // }
382  PNode* pn = this->children->enumerate();
383  while( pn != NULL) 
384    { 
385      /* if this node has changed, make sure, that all children are updated also */
386      if( this->bRelCoorChanged || this->bAbsCoorChanged)
387        pn->parentCoorChanged ();
388      if( this->bRelDirChanged || this->bAbsDirChanged)
389        pn->parentDirChanged ();
390      pn->update(timeStamp);
391      pn = this->children->nextElement();
392    }
393
394  this->timeStamp = timeStamp;
395  this->bRelCoorChanged = false;
396  this->bAbsCoorChanged = false;
397  this->bRelDirChanged = false;
398  this->bAbsDirChanged = false;
399}
400
401
402/*
403  \brief tick
404*/
405void PNode::processTick (float dt)
406{
407  this->tick (dt);
408  PNode* pn = this->children->enumerate();
409  while( pn != NULL) 
410    { 
411      pn->processTick (dt);
412      pn = this->children->nextElement();
413    } 
414}
415
416
417void PNode::tick (float dt)
418{}
419
420
421void PNode::debug()
422{
423  printf("PNode::debug() - absCoord: (%f, %f, %f)\n", 
424         this->absCoordinate.x, 
425         this->absCoordinate.y,
426         this->absCoordinate.z);
427}
428
429
430/*
431  \brief set the name of the node
432
433  for debug purposes realy usefull, not used to work properly
434*/
435void PNode::setName (char* newName)
436{
437  this->objectName = newName;
438}
439
440
441/*
442  \brief gets the name of the node
443*/
444char* PNode::getName ()
445{
446  return this->objectName;
447}
Note: See TracBrowser for help on using the repository browser.