Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/nico/src/p_node.cc @ 3716

Last change on this file since 3716 was 3399, checked in by bensch, 20 years ago

orxonox/branches/nico: merged trunk into branches/nico
merged with command:
svn merge ../trunk/ nico/ -r 3238:HEAD
resolved conflict in favor of branches/nico.

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
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*/
227void PNode::setAbsDir (Quaternion* absDir)
228{
229  this->bAbsDirChanged = true;
230  this->absDirection = *absDir;
231}
232
233
234/**
235   \brief shift coordinate (abs and rel)
236   \param shift vector
237
238   this function shifts the current coordinates about the vector shift. this is
239   usefull because from some place else you can:
240   PNode* someNode = ...;
241   Quaternion objectMovement = calculateShift();
242   someNode->shiftCoor(objectMovement);
243
244   elsewhere you would have to:
245   PNode* someNode = ...;
246   Quaternion objectMovement = calculateShift();
247   Quaternion currentCoor = someNode->getRelCoor();
248   Quaternion newCoor = currentCoor + objectMovement;
249   someNode->setRelCoor(newCoor);
250   
251   yea right... shorter...
252
253*/
254void PNode::shiftDir (Quaternion* shift)
255{}
256
257
258
259/**
260   \brief adds a child and makes this node to a parent
261   \param child reference
262
263   use this to add a child to this node.
264*/
265void PNode::addChild (PNode* pNode)
266{
267  this->addChild(pNode, DEFAULT_MODE);
268}
269
270
271/**
272   \brief adds a child and makes this node to a parent
273   \param child reference
274   \param on which changes the child should also change ist state
275
276   use this to add a child to this node.
277*/
278void PNode::addChild (PNode* pNode, parentingMode mode)
279{
280  pNode->mode = mode;
281  pNode->parent = this;
282  this->children->add (pNode);
283}
284
285
286/**
287   /brief removes a child from the node
288*/
289void PNode::removeChild (PNode* pNode)
290{
291  this->children->remove (pNode);
292}
293
294
295/**
296   \brief sets the parent of this PNode
297*/
298void PNode::setParent (PNode* parent)
299{
300  this->parent = parent;
301}
302
303/**
304   \brief set the mode of this parent manualy
305*/
306void PNode::setMode (parentingMode mode)
307{
308  this->mode = mode;
309}
310
311/**
312   \brief has to be called, if the parent coordinate has changed
313   
314   normaly this will be done by the parent itself automaticaly. If you call this, you
315   will force an update of the coordinated of the node.
316*/
317void PNode::parentCoorChanged ()
318{
319  this->bRelCoorChanged = true;
320}
321
322
323/**
324   \brief has to be called, if the parent direction has changed
325   
326   normaly this will be done by the parent itself automaticaly. If you call this, you
327   will force an update of the direction of the node.
328*/
329void PNode::parentDirChanged ()
330{
331  this->bRelDirChanged = true;
332}
333
334
335/**
336   \brief updates the absCoordinate/absDirection
337
338   this is used to go through the parent-tree to update all the absolute coordinates
339   and directions. this update should be done by the engine, so you don't have to
340   worry, normaly...
341*/
342void PNode::update (float timeStamp)
343{
344  printf ("PNode::update - %s - (%f, %f, %f)\n", this->objectName, this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
345
346      if( this->mode == MOVEMENT || this->mode == ALL)
347        {
348          if( this->bAbsCoorChanged /*&& this->timeStamp != DataTank::timeStamp*/)
349            {
350              printf("PNode::update () - this->bAbsCoorChanged = true\n");
351              /* if you have set the absolute coordinates this overrides all other changes */
352              this->relCoordinate = this->absCoordinate - parent->getAbsCoor ();
353            }
354          else if( this->bRelCoorChanged /*&& this->timeStamp != DataTank::timeStamp*/)
355            {
356              /*this is bad style... must be deleted later - just for testing*/
357              if( this->parent == NULL)
358                {
359                this->absCoordinate = this->relCoordinate;
360                }
361              else
362                this->absCoordinate = parent->getAbsCoor () + this->relCoordinate;            /* update the current absCoordinate */
363            }
364        }
365     
366      if( this->mode == ROTATION && this->mode == ALL)
367        {
368          if( this->bAbsDirChanged /*&& this->timeStamp != DataTank::timeStamp*/)
369            {
370              /* if you have set the absolute coordinates this overrides all other changes */
371              this->relDirection = this->absDirection - parent->getAbsDir ();
372            }
373          else if( this->bRelDirChanged /*&& this->timeStamp != DataTank::timeStamp*/)
374            {
375              /* update the current absDirection - remember * means rotation around sth.*/
376              this->absDirection = parent->getAbsDir () * this->relDirection;
377            }
378        }   
379      // }
380  PNode* pn = this->children->enumerate();
381  while( pn != NULL) 
382    { 
383      /* if this node has changed, make sure, that all children are updated also */
384      if( this->bRelCoorChanged || this->bAbsCoorChanged)
385        pn->parentCoorChanged ();
386      if( this->bRelDirChanged || this->bAbsDirChanged)
387        pn->parentDirChanged ();
388      pn->update(timeStamp);
389      pn = this->children->nextElement();
390    }
391
392  this->timeStamp = timeStamp;
393  this->bRelCoorChanged = false;
394  this->bAbsCoorChanged = false;
395  this->bRelDirChanged = false;
396  this->bAbsDirChanged = false;
397}
398
399
400/*
401  \brief tick
402*/
403void PNode::processTick (float dt)
404{
405  this->tick (dt);
406  PNode* pn = this->children->enumerate();
407  while( pn != NULL) 
408    { 
409      pn->processTick (dt);
410      pn = this->children->nextElement();
411    } 
412}
413
414
415void PNode::tick (float dt)
416{}
417
418
419void PNode::debug()
420{
421  printf("PNode::debug() - absCoord: (%f, %f, %f)\n", 
422         this->absCoordinate.x, 
423         this->absCoordinate.y,
424         this->absCoordinate.z);
425}
426
427
428/*
429  \brief set the name of the node
430
431  for debug purposes realy usefull, not used to work properly
432*/
433void PNode::setName (char* newName)
434{
435  this->objectName = newName;
436}
437
438
439/*
440  \brief gets the name of the node
441*/
442char* PNode::getName ()
443{
444  return this->objectName;
445}
Note: See TracBrowser for help on using the repository browser.