| 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 |  | 
|---|
| 26 | using namespace std; | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 |    \brief standard constructor | 
|---|
| 31 |  | 
|---|
| 32 |    \todo this constructor is not jet implemented - do it | 
|---|
| 33 | */ | 
|---|
| 34 | PNode::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 | */ | 
|---|
| 48 | PNode::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 | */ | 
|---|
| 69 | PNode::~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 | */ | 
|---|
| 88 | void 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 | */ | 
|---|
| 104 | Vector 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 | */ | 
|---|
| 119 | void 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 | */ | 
|---|
| 130 | Vector 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 | */ | 
|---|
| 144 | void 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 | */ | 
|---|
| 171 | void 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 | */ | 
|---|
| 190 | Quaternion 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 | */ | 
|---|
| 204 | void 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 | */ | 
|---|
| 215 | Quaternion 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 | */ | 
|---|
| 227 | void PNode::setAbsDir (Quaternion* absDir) | 
|---|
| 228 | {} | 
|---|
| 229 |  | 
|---|
| 230 |  | 
|---|
| 231 | /** | 
|---|
| 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 | */ | 
|---|
| 251 | void PNode::shiftDir (Quaternion* shift) | 
|---|
| 252 | {} | 
|---|
| 253 |  | 
|---|
| 254 |  | 
|---|
| 255 |  | 
|---|
| 256 | /** | 
|---|
| 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 | */ | 
|---|
| 262 | void PNode::addChild (PNode* pNode) | 
|---|
| 263 | { | 
|---|
| 264 |   this->addChild(pNode, DEFAULT_MODE); | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 |  | 
|---|
| 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 | */ | 
|---|
| 275 | void PNode::addChild (PNode* pNode, parentingMode mode) | 
|---|
| 276 | { | 
|---|
| 277 |   pNode->mode = mode; | 
|---|
| 278 |   pNode->parent = this; | 
|---|
| 279 |   this->children->add (pNode); | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 |  | 
|---|
| 283 | /** | 
|---|
| 284 |    /brief removes a child from the node | 
|---|
| 285 | */ | 
|---|
| 286 | void PNode::removeChild (PNode* pNode) | 
|---|
| 287 | { | 
|---|
| 288 |   this->children->remove (pNode); | 
|---|
| 289 | } | 
|---|
| 290 |  | 
|---|
| 291 |  | 
|---|
| 292 | /** | 
|---|
| 293 |    \brief sets the parent of this PNode | 
|---|
| 294 | */ | 
|---|
| 295 | void PNode::setParent (PNode* parent) | 
|---|
| 296 | { | 
|---|
| 297 |   this->parent = parent; | 
|---|
| 298 | } | 
|---|
| 299 |  | 
|---|
| 300 | /** | 
|---|
| 301 |    \brief set the mode of this parent manualy | 
|---|
| 302 | */ | 
|---|
| 303 | void PNode::setMode (parentingMode mode) | 
|---|
| 304 | { | 
|---|
| 305 |   this->mode = mode; | 
|---|
| 306 | } | 
|---|
| 307 |  | 
|---|
| 308 | /** | 
|---|
| 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 | */ | 
|---|
| 314 | void 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 | */ | 
|---|
| 326 | void PNode::parentDirChanged () | 
|---|
| 327 | { | 
|---|
| 328 |   this->bRelDirChanged = true; | 
|---|
| 329 | } | 
|---|
| 330 |  | 
|---|
| 331 |  | 
|---|
| 332 | /** | 
|---|
| 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 | */ | 
|---|
| 339 | void PNode::update (float timeStamp) | 
|---|
| 340 | { | 
|---|
| 341 |   printf ("PNode::update - %s - (%f, %f, %f)\n", this->objectName, this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); | 
|---|
| 342 |  | 
|---|
| 343 |       if( this->mode == MOVEMENT || this->mode == ALL) | 
|---|
| 344 |         { | 
|---|
| 345 |           if( this->bAbsCoorChanged /*&& this->timeStamp != DataTank::timeStamp*/) | 
|---|
| 346 |             { | 
|---|
| 347 |               printf("PNode::update () - this->bAbsCoorChanged = true\n"); | 
|---|
| 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 |       // } | 
|---|
| 377 |   PNode* pn = this->children->enumerate(); | 
|---|
| 378 |   while( pn != NULL)  | 
|---|
| 379 |     {  | 
|---|
| 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 (); | 
|---|
| 385 |       pn->update(timeStamp); | 
|---|
| 386 |       pn = this->children->nextElement(); | 
|---|
| 387 |     } | 
|---|
| 388 |  | 
|---|
| 389 |   this->timeStamp = timeStamp; | 
|---|
| 390 |   this->bRelCoorChanged = false; | 
|---|
| 391 |   this->bAbsCoorChanged = false; | 
|---|
| 392 |   this->bRelDirChanged = false; | 
|---|
| 393 |   this->bAbsDirChanged = false; | 
|---|
| 394 | } | 
|---|
| 395 |  | 
|---|
| 396 |  | 
|---|
| 397 | /* | 
|---|
| 398 |   \brief tick | 
|---|
| 399 | */ | 
|---|
| 400 | void 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 |  | 
|---|
| 412 | void PNode::tick (float dt) | 
|---|
| 413 | {} | 
|---|
| 414 |  | 
|---|
| 415 |  | 
|---|
| 416 | void PNode::debug() | 
|---|
| 417 | { | 
|---|
| 418 |   printf("PNode::debug() - absCoord: (%f, %f, %f)\n",  | 
|---|
| 419 |          this->absCoordinate.x,  | 
|---|
| 420 |          this->absCoordinate.y, | 
|---|
| 421 |          this->absCoordinate.z); | 
|---|
| 422 | } | 
|---|
| 423 |  | 
|---|
| 424 |  | 
|---|
| 425 | /* | 
|---|
| 426 |   \brief set the name of the node | 
|---|
| 427 |  | 
|---|
| 428 |   for debug purposes realy usefull, not used to work properly | 
|---|
| 429 | */ | 
|---|
| 430 | void PNode::setName (char* newName) | 
|---|
| 431 | { | 
|---|
| 432 |   this->objectName = newName; | 
|---|
| 433 | } | 
|---|
| 434 |  | 
|---|
| 435 |  | 
|---|
| 436 | /* | 
|---|
| 437 |   \brief gets the name of the node | 
|---|
| 438 | */ | 
|---|
| 439 | char* PNode::getName () | 
|---|
| 440 | { | 
|---|
| 441 |   return this->objectName; | 
|---|
| 442 | } | 
|---|