[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 |
---|
[3246] | 19 | */ |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | #include "p_node.h" |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | using namespace std; |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | \brief standard constructor |
---|
| 30 | |
---|
| 31 | \todo this constructor is not jet implemented - do it |
---|
| 32 | */ |
---|
[3248] | 33 | PNode::PNode () |
---|
| 34 | { |
---|
| 35 | this->children = new tList<PNode>(); |
---|
[3265] | 36 | this->bRelCoorChanged = true; |
---|
| 37 | this->bAbsCoorChanged = false; |
---|
| 38 | this->bRelDirChanged = true; |
---|
| 39 | this->bAbsDirChanged = false; |
---|
| 40 | this->parent = NULL; |
---|
[3248] | 41 | } |
---|
[3246] | 42 | |
---|
| 43 | |
---|
| 44 | /** |
---|
[3265] | 45 | \brief constructor with coodinates |
---|
| 46 | */ |
---|
| 47 | PNode::PNode (Vector* absCoordinate, PNode* parent ) |
---|
| 48 | { |
---|
| 49 | this->absCoordinate = *absCoordinate; |
---|
[3269] | 50 | this->relCoordinate = this->absCoordinate - parent->getAbsCoor (); |
---|
[3265] | 51 | |
---|
| 52 | this->children = new tList<PNode>(); |
---|
[3269] | 53 | this->bRelCoorChanged = true; |
---|
| 54 | this->bAbsCoorChanged = false; |
---|
[3265] | 55 | this->bRelDirChanged = true; |
---|
| 56 | this->bAbsDirChanged = false; |
---|
| 57 | this->parent = parent; |
---|
| 58 | |
---|
| 59 | parent->addChild (this); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | /** |
---|
[3246] | 64 | \brief standard deconstructor |
---|
| 65 | |
---|
| 66 | \todo this deconstructor is not jet implemented - do it |
---|
| 67 | */ |
---|
[3248] | 68 | PNode::~PNode () |
---|
| 69 | { |
---|
| 70 | this->children->destroy(); |
---|
| 71 | delete this->children; |
---|
| 72 | } |
---|
[3246] | 73 | |
---|
[3247] | 74 | |
---|
| 75 | /** |
---|
| 76 | \brief get relative coordinates |
---|
| 77 | \returns relative coordinates to its parent |
---|
| 78 | */ |
---|
| 79 | Vector PNode::getRelCoor () |
---|
[3248] | 80 | { |
---|
| 81 | Vector r = this->relCoordinate; /* return a copy, so it can't be modified */ |
---|
| 82 | return r; |
---|
| 83 | } |
---|
[3247] | 84 | |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | \brief set relative coordinates |
---|
| 88 | \param relative coordinates to its parent |
---|
| 89 | |
---|
| 90 | it is very importand, that you use this function, if you want to update the |
---|
| 91 | relCoordinates. If you don't use this, the PNode won't recognize, that something |
---|
| 92 | has changed and won't update the children Nodes. |
---|
| 93 | */ |
---|
[3269] | 94 | void PNode::setRelCoor (Vector* relCoord) |
---|
[3265] | 95 | { |
---|
| 96 | this->bRelCoorChanged = true; |
---|
[3269] | 97 | this->relCoordinate = *relCoord; |
---|
[3265] | 98 | } |
---|
[3247] | 99 | |
---|
| 100 | |
---|
| 101 | /** |
---|
| 102 | \brief get absolute coordinates |
---|
| 103 | \returns absolute coordinates from (0,0,0) |
---|
| 104 | */ |
---|
| 105 | Vector PNode::getAbsCoor () |
---|
[3265] | 106 | { |
---|
| 107 | return this->absCoordinate; |
---|
| 108 | } |
---|
[3247] | 109 | |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | \brief get relative coordinates |
---|
| 113 | \returns relative coordinates to its parent |
---|
| 114 | |
---|
| 115 | it is very importand, that you use this function, if you want to update the |
---|
| 116 | absCoordinates. 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] | 119 | void PNode::setAbsCoor (Vector* absCoord) |
---|
[3265] | 120 | { |
---|
| 121 | this->bAbsCoorChanged = true; |
---|
[3269] | 122 | this->absCoordinate = *absCoord; |
---|
[3265] | 123 | } |
---|
[3247] | 124 | |
---|
| 125 | |
---|
| 126 | /** |
---|
[3248] | 127 | \brief shift coordinate (abs and rel) |
---|
| 128 | \param shift vector |
---|
| 129 | |
---|
| 130 | this function shifts the current coordinates about the vector shift. this is |
---|
| 131 | usefull because from some place else you can: |
---|
| 132 | PNode* someNode = ...; |
---|
| 133 | Vector objectMovement = calculateShift(); |
---|
| 134 | someNode->shiftCoor(objectMovement); |
---|
| 135 | |
---|
| 136 | elsewhere you would have to: |
---|
| 137 | PNode* someNode = ...; |
---|
| 138 | Vector objectMovement = calculateShift(); |
---|
| 139 | Vector currentCoor = someNode->getRelCoor(); |
---|
| 140 | Vector newCoor = currentCoor + objectMovement; |
---|
| 141 | someNode->setRelCoor(newCoor); |
---|
| 142 | |
---|
| 143 | yea right... shorter... |
---|
| 144 | |
---|
| 145 | */ |
---|
[3265] | 146 | void PNode::shiftCoor (Vector* shift) |
---|
| 147 | { |
---|
| 148 | if( this->bAbsCoorChanged) |
---|
| 149 | { |
---|
| 150 | this->absCoordinate = this->absCoordinate + *shift; |
---|
| 151 | } |
---|
| 152 | else |
---|
| 153 | { |
---|
| 154 | this->relCoordinate = this->relCoordinate + *shift; |
---|
| 155 | this->bRelCoorChanged = true; |
---|
| 156 | } |
---|
[3248] | 157 | |
---|
[3265] | 158 | printf("PNode::shiftCoor() - relCoord: (%f, %f, %f)\n", |
---|
| 159 | this->relCoordinate.x, |
---|
| 160 | this->relCoordinate.y, |
---|
| 161 | this->relCoordinate.z); |
---|
| 162 | } |
---|
[3248] | 163 | |
---|
| 164 | |
---|
[3265] | 165 | |
---|
[3248] | 166 | /** |
---|
[3247] | 167 | \brief get relative direction |
---|
| 168 | \returns relative direction to its parent |
---|
| 169 | */ |
---|
| 170 | Quaternion PNode::getRelDir () |
---|
[3265] | 171 | { |
---|
| 172 | return this->relDirection; |
---|
| 173 | } |
---|
[3247] | 174 | |
---|
| 175 | |
---|
| 176 | /** |
---|
| 177 | \brief set relative direction |
---|
| 178 | \param relative direction to its parent |
---|
| 179 | |
---|
| 180 | it is very importand, that you use this function, if you want to update the |
---|
| 181 | relDirection. If you don't use this, the PNode won't recognize, that something |
---|
| 182 | has changed and won't update the children Nodes. |
---|
| 183 | */ |
---|
[3269] | 184 | void PNode::setRelDir (Quaternion* relDir) |
---|
[3265] | 185 | { |
---|
| 186 | this->bRelCoorChanged = true; |
---|
[3269] | 187 | this->relDirection = *relDir; |
---|
[3265] | 188 | } |
---|
[3247] | 189 | |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | \brief gets the absolute direction (0,0,1) |
---|
| 193 | \returns absolute coordinates |
---|
| 194 | */ |
---|
| 195 | Quaternion PNode::getAbsDir () |
---|
| 196 | {} |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | /** |
---|
| 200 | \brief sets the absolute direction (0,0,1) |
---|
| 201 | \param absolute coordinates |
---|
| 202 | |
---|
| 203 | it is very importand, that you use this function, if you want to update the |
---|
| 204 | absDirection. If you don't use this, the PNode won't recognize, that something |
---|
| 205 | has changed and won't update the children Nodes. |
---|
| 206 | */ |
---|
[3269] | 207 | void PNode::setAbsDir (Quaternion* absDir) |
---|
[3247] | 208 | {} |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | /** |
---|
[3248] | 212 | \brief shift coordinate (abs and rel) |
---|
| 213 | \param shift vector |
---|
| 214 | |
---|
| 215 | this function shifts the current coordinates about the vector shift. this is |
---|
| 216 | usefull because from some place else you can: |
---|
| 217 | PNode* someNode = ...; |
---|
| 218 | Quaternion objectMovement = calculateShift(); |
---|
| 219 | someNode->shiftCoor(objectMovement); |
---|
| 220 | |
---|
| 221 | elsewhere you would have to: |
---|
| 222 | PNode* someNode = ...; |
---|
| 223 | Quaternion objectMovement = calculateShift(); |
---|
| 224 | Quaternion currentCoor = someNode->getRelCoor(); |
---|
| 225 | Quaternion newCoor = currentCoor + objectMovement; |
---|
| 226 | someNode->setRelCoor(newCoor); |
---|
| 227 | |
---|
| 228 | yea right... shorter... |
---|
| 229 | |
---|
| 230 | */ |
---|
[3265] | 231 | void PNode::shiftDir (Quaternion* shift) |
---|
[3248] | 232 | {} |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | |
---|
| 236 | /** |
---|
[3247] | 237 | \brief adds a child and makes this node to a parent |
---|
| 238 | \param child reference |
---|
| 239 | |
---|
| 240 | use this to add a child to this node. |
---|
| 241 | */ |
---|
| 242 | void PNode::addChild (PNode* pNode) |
---|
[3248] | 243 | { |
---|
| 244 | this->addChild(pNode, DEFAULT_MODE); |
---|
| 245 | } |
---|
[3247] | 246 | |
---|
| 247 | |
---|
[3248] | 248 | /** |
---|
| 249 | \brief adds a child and makes this node to a parent |
---|
| 250 | \param child reference |
---|
| 251 | \param on which changes the child should also change ist state |
---|
| 252 | |
---|
| 253 | use this to add a child to this node. |
---|
| 254 | */ |
---|
| 255 | void PNode::addChild (PNode* pNode, parentingMode mode) |
---|
| 256 | { |
---|
| 257 | pNode->mode = mode; |
---|
[3265] | 258 | pNode->parent = this; |
---|
[3248] | 259 | this->children->add (pNode); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | /** |
---|
| 264 | /brief removes a child from the node |
---|
| 265 | */ |
---|
[3247] | 266 | void PNode::removeChild (PNode* pNode) |
---|
[3248] | 267 | { |
---|
| 268 | this->children->remove (pNode); |
---|
| 269 | } |
---|
[3247] | 270 | |
---|
| 271 | |
---|
[3248] | 272 | /** |
---|
| 273 | \brief sets the parent of this PNode |
---|
| 274 | */ |
---|
[3247] | 275 | void PNode::setParent (PNode* parent) |
---|
[3248] | 276 | { |
---|
| 277 | this->parent = parent; |
---|
| 278 | } |
---|
[3247] | 279 | |
---|
[3265] | 280 | /** |
---|
| 281 | \brief set the mode of this parent manualy |
---|
| 282 | */ |
---|
| 283 | void PNode::setMode (parentingMode mode) |
---|
| 284 | { |
---|
| 285 | this->mode = mode; |
---|
| 286 | } |
---|
[3248] | 287 | |
---|
| 288 | /** |
---|
[3265] | 289 | \brief has to be called, if the parent coordinate has changed |
---|
| 290 | |
---|
| 291 | normaly this will be done by the parent itself automaticaly. If you call this, you |
---|
| 292 | will force an update of the coordinated of the node. |
---|
| 293 | */ |
---|
| 294 | void PNode::parentCoorChanged () |
---|
| 295 | { |
---|
| 296 | this->bRelCoorChanged = true; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | |
---|
| 300 | /** |
---|
| 301 | \brief has to be called, if the parent direction has changed |
---|
| 302 | |
---|
| 303 | normaly this will be done by the parent itself automaticaly. If you call this, you |
---|
| 304 | will force an update of the direction of the node. |
---|
| 305 | */ |
---|
| 306 | void PNode::parentDirChanged () |
---|
| 307 | { |
---|
| 308 | this->bRelDirChanged = true; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | |
---|
| 312 | /** |
---|
[3248] | 313 | \brief updates the absCoordinate/absDirection |
---|
| 314 | |
---|
| 315 | this is used to go through the parent-tree to update all the absolute coordinates |
---|
| 316 | and directions. this update should be done by the engine, so you don't have to |
---|
| 317 | worry, normaly... |
---|
| 318 | */ |
---|
[3265] | 319 | void PNode::update (long timeStamp) |
---|
[3248] | 320 | { |
---|
[3265] | 321 | |
---|
| 322 | if( this->mode == MOVEMENT || this->mode == ALL) |
---|
| 323 | { |
---|
| 324 | if( this->bAbsCoorChanged /*&& this->timeStamp != DataTank::timeStamp*/) |
---|
| 325 | { |
---|
[3269] | 326 | printf("PNode::update () - this->bAbsCoorChanged = true\n"); |
---|
[3265] | 327 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
| 328 | this->relCoordinate = this->absCoordinate - parent->getAbsCoor (); |
---|
| 329 | } |
---|
| 330 | else if( this->bRelCoorChanged /*&& this->timeStamp != DataTank::timeStamp*/) |
---|
| 331 | { |
---|
| 332 | /*this is bad style... must be deleted later - just for testing*/ |
---|
| 333 | if( this->parent == NULL) |
---|
| 334 | { |
---|
| 335 | this->absCoordinate = this->relCoordinate; |
---|
| 336 | } |
---|
| 337 | else |
---|
| 338 | this->absCoordinate = parent->getAbsCoor () + this->relCoordinate; /* update the current absCoordinate */ |
---|
| 339 | } |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | if( this->mode == ROTATION && this->mode == ALL) |
---|
| 343 | { |
---|
| 344 | if( this->bAbsDirChanged /*&& this->timeStamp != DataTank::timeStamp*/) |
---|
| 345 | { |
---|
| 346 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
| 347 | this->relDirection = this->absDirection - parent->getAbsDir (); |
---|
| 348 | } |
---|
| 349 | else if( this->bRelDirChanged /*&& this->timeStamp != DataTank::timeStamp*/) |
---|
| 350 | { |
---|
| 351 | /* update the current absDirection - remember * means rotation around sth.*/ |
---|
| 352 | this->absDirection = parent->getAbsDir () * this->relDirection; |
---|
| 353 | } |
---|
| 354 | } |
---|
| 355 | // } |
---|
[3249] | 356 | PNode* pn = this->children->enumerate(); |
---|
| 357 | while( pn != NULL) |
---|
| 358 | { |
---|
[3265] | 359 | /* if this node has changed, make sure, that all children are updated also */ |
---|
| 360 | if( this->bRelCoorChanged || this->bAbsCoorChanged) |
---|
| 361 | pn->parentCoorChanged (); |
---|
| 362 | if( this->bRelDirChanged || this->bAbsDirChanged) |
---|
| 363 | pn->parentDirChanged (); |
---|
[3249] | 364 | pn->update(timeStamp); |
---|
| 365 | pn = this->children->nextElement(); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | this->timeStamp = timeStamp; |
---|
[3265] | 369 | this->bRelCoorChanged = false; |
---|
| 370 | this->bAbsCoorChanged = false; |
---|
| 371 | this->bRelDirChanged = false; |
---|
| 372 | this->bAbsDirChanged = false; |
---|
| 373 | } |
---|
[3249] | 374 | |
---|
[3265] | 375 | |
---|
| 376 | void PNode::debug() |
---|
| 377 | { |
---|
| 378 | printf("PNode::debug() - absCoord: (%f, %f, %f)\n", |
---|
| 379 | this->absCoordinate.x, |
---|
| 380 | this->absCoordinate.y, |
---|
| 381 | this->absCoordinate.z); |
---|
[3248] | 382 | } |
---|