[4570] | 1 | /* |
---|
[3246] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
[5419] | 13 | co-programmer: Benjamin Grauer |
---|
[3246] | 14 | */ |
---|
| 15 | |
---|
[3590] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PNODE |
---|
[3246] | 17 | |
---|
| 18 | #include "p_node.h" |
---|
[4761] | 19 | #include "null_parent.h" |
---|
| 20 | |
---|
| 21 | #include "load_param.h" |
---|
| 22 | #include "class_list.h" |
---|
| 23 | |
---|
[3607] | 24 | #include "stdincl.h" |
---|
[3860] | 25 | #include "compiler.h" |
---|
[3608] | 26 | #include "error.h" |
---|
| 27 | #include "debug.h" |
---|
| 28 | #include "list.h" |
---|
| 29 | #include "vector.h" |
---|
| 30 | |
---|
[5406] | 31 | #include "color.h" |
---|
[3246] | 32 | |
---|
| 33 | using namespace std; |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | /** |
---|
[4836] | 37 | * standard constructor |
---|
[5420] | 38 | */ |
---|
[4570] | 39 | PNode::PNode () |
---|
[3365] | 40 | { |
---|
[3552] | 41 | init(NULL); |
---|
[3529] | 42 | |
---|
[4436] | 43 | NullParent::getInstance()->addChild(this); |
---|
[3365] | 44 | } |
---|
[3246] | 45 | |
---|
[4448] | 46 | /** |
---|
[4836] | 47 | * @param root the load-Element for the PNode |
---|
[5420] | 48 | */ |
---|
[4436] | 49 | PNode::PNode(const TiXmlElement* root) |
---|
| 50 | { |
---|
| 51 | this->init(NULL); |
---|
[4444] | 52 | this->loadParams(root); |
---|
[4570] | 53 | |
---|
[5372] | 54 | if (this->parent == NULL) |
---|
| 55 | NullParent::getInstance()->addChild(this); |
---|
[4436] | 56 | } |
---|
[3246] | 57 | |
---|
| 58 | /** |
---|
[4836] | 59 | * constructor with coodinates |
---|
| 60 | * @param absCoordinate the Absolute coordinate of the Object |
---|
| 61 | * @param parent The parent-node of this node. |
---|
[5420] | 62 | */ |
---|
[4993] | 63 | PNode::PNode (const Vector& absCoor, PNode* parent ) |
---|
[3365] | 64 | { |
---|
[3552] | 65 | this->init(parent); |
---|
[3365] | 66 | |
---|
[3860] | 67 | if (likely(parent != NULL)) |
---|
[3800] | 68 | parent->addChild (this); |
---|
[4993] | 69 | |
---|
| 70 | this->setAbsCoor(absCoor); |
---|
[3365] | 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
[5296] | 74 | * standard deconstructor |
---|
| 75 | * |
---|
| 76 | * There are two general ways to delete a PNode |
---|
| 77 | * 1. delete instance; |
---|
| 78 | * -> result |
---|
| 79 | * delete this Node and all its children and children's children... |
---|
| 80 | * (danger if you still need the children's instance somewhere else!!) |
---|
| 81 | * |
---|
| 82 | * 2. instance->remove2D(); delete instance; |
---|
| 83 | * -> result: |
---|
| 84 | * moves its children to the NullParent |
---|
| 85 | * then deletes the Element. |
---|
| 86 | */ |
---|
[4570] | 87 | PNode::~PNode () |
---|
[3365] | 88 | { |
---|
[5296] | 89 | // remove the Node, delete it's children. |
---|
| 90 | tIterator<PNode>* iterator = this->children->getIterator(); |
---|
| 91 | PNode* child = iterator->firstElement(); |
---|
| 92 | |
---|
| 93 | while( child != NULL) |
---|
[5214] | 94 | { |
---|
[5296] | 95 | delete child; |
---|
| 96 | child = iterator->nextElement(); |
---|
[5214] | 97 | } |
---|
[5296] | 98 | delete iterator; |
---|
| 99 | |
---|
| 100 | if (this->parent != NULL) |
---|
| 101 | { |
---|
| 102 | this->parent->children->remove(this); |
---|
| 103 | this->parent = NULL; |
---|
| 104 | } |
---|
[5214] | 105 | delete this->children; |
---|
[5296] | 106 | |
---|
| 107 | // remove all other allocated memory. |
---|
[5088] | 108 | if (this->toCoordinate != NULL) |
---|
| 109 | delete this->toCoordinate; |
---|
| 110 | if (this->toDirection != NULL) |
---|
| 111 | delete this->toDirection; |
---|
[3365] | 112 | } |
---|
[3246] | 113 | |
---|
[5296] | 114 | |
---|
[4448] | 115 | /** |
---|
[4836] | 116 | * initializes a PNode |
---|
| 117 | * @param parent the parent for this PNode |
---|
[5420] | 118 | */ |
---|
[3552] | 119 | void PNode::init(PNode* parent) |
---|
| 120 | { |
---|
[4742] | 121 | this->setClassID(CL_PARENT_NODE, "PNode"); |
---|
[5211] | 122 | |
---|
[3552] | 123 | this->children = new tList<PNode>(); |
---|
| 124 | this->bRelCoorChanged = true; |
---|
| 125 | this->bRelDirChanged = true; |
---|
[4570] | 126 | this->parent = parent; |
---|
[5209] | 127 | this->parentMode = PNODE_PARENT_MODE_DEFAULT; |
---|
[4987] | 128 | |
---|
[4993] | 129 | // iterators |
---|
| 130 | this->toCoordinate = NULL; |
---|
[4990] | 131 | this->toDirection = NULL; |
---|
[4992] | 132 | this->bias = 1.0; |
---|
[3552] | 133 | } |
---|
[3365] | 134 | |
---|
[4448] | 135 | /** |
---|
[4836] | 136 | * loads parameters of the PNode |
---|
| 137 | * @param root the XML-element to load the properties of |
---|
[5420] | 138 | */ |
---|
[4436] | 139 | void PNode::loadParams(const TiXmlElement* root) |
---|
| 140 | { |
---|
| 141 | static_cast<BaseObject*>(this)->loadParams(root); |
---|
[4610] | 142 | |
---|
[5671] | 143 | LoadParam(root, "rel-coor", this, PNode, setRelCoor) |
---|
[4771] | 144 | .describe("Sets The relative position of the Node to its parent."); |
---|
| 145 | |
---|
[5671] | 146 | LoadParam(root, "abs-coor", this, PNode, setAbsCoor) |
---|
[4610] | 147 | .describe("Sets The absolute Position of the Node."); |
---|
| 148 | |
---|
[5671] | 149 | LoadParam(root, "rel-dir", this, PNode, setRelDir) |
---|
[4771] | 150 | .describe("Sets The relative rotation of the Node to its parent."); |
---|
[4761] | 151 | |
---|
[5671] | 152 | LoadParam(root, "abs-dir", this, PNode, setAbsDir) |
---|
[4771] | 153 | .describe("Sets The absolute rotation of the Node."); |
---|
| 154 | |
---|
[5671] | 155 | LoadParam(root, "parent", this, PNode, setParent) |
---|
[4761] | 156 | .describe("the Name of the Parent of this PNode"); |
---|
[4765] | 157 | |
---|
[5671] | 158 | LoadParam(root, "parent-mode", this, PNode, setParentMode) |
---|
[4765] | 159 | .describe("the mode to connect this node to its parent ()"); |
---|
| 160 | |
---|
| 161 | // cycling properties |
---|
[4785] | 162 | if (root != NULL) |
---|
[4765] | 163 | { |
---|
[5654] | 164 | LOAD_PARAM_START_CYCLE(root, element); |
---|
[4785] | 165 | { |
---|
[5654] | 166 | LoadParam_CYCLE(element, "parent", this, PNode, addChild) |
---|
[4785] | 167 | .describe("adds a new Child to the current Node."); |
---|
[4765] | 168 | |
---|
[4785] | 169 | } |
---|
[5654] | 170 | LOAD_PARAM_END_CYCLE(element); |
---|
[4765] | 171 | } |
---|
[4436] | 172 | } |
---|
[3365] | 173 | |
---|
| 174 | /** |
---|
[4836] | 175 | * set relative coordinates |
---|
| 176 | * @param relCoord relative coordinates to its parent |
---|
[5420] | 177 | * |
---|
| 178 | * |
---|
| 179 | * it is very importand, that you use this function, if you want to update the |
---|
| 180 | * relCoordinates. If you don't use this, the PNode won't recognize, that something |
---|
| 181 | * has changed and won't update the children Nodes. |
---|
| 182 | */ |
---|
[3810] | 183 | void PNode::setRelCoor (const Vector& relCoord) |
---|
[3675] | 184 | { |
---|
[5113] | 185 | if (this->toCoordinate!= NULL) |
---|
| 186 | { |
---|
| 187 | delete this->toCoordinate; |
---|
| 188 | this->toCoordinate = NULL; |
---|
| 189 | } |
---|
| 190 | |
---|
[4993] | 191 | this->relCoordinate = relCoord; |
---|
[3675] | 192 | this->bRelCoorChanged = true; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /** |
---|
[4836] | 196 | * set relative coordinates |
---|
| 197 | * @param x x-relative coordinates to its parent |
---|
| 198 | * @param y y-relative coordinates to its parent |
---|
| 199 | * @param z z-relative coordinates to its parent |
---|
[4993] | 200 | * @see void PNode::setRelCoor (const Vector& relCoord) |
---|
[5420] | 201 | */ |
---|
[4610] | 202 | void PNode::setRelCoor (float x, float y, float z) |
---|
| 203 | { |
---|
| 204 | this->setRelCoor(Vector(x, y, z)); |
---|
| 205 | } |
---|
| 206 | |
---|
[4992] | 207 | /** |
---|
| 208 | * sets a new relative position smoothely |
---|
| 209 | * @param relCoordSoft the new Position to iterate to |
---|
| 210 | * @param bias how fast to iterate to this position |
---|
| 211 | */ |
---|
| 212 | void PNode::setRelCoorSoft(const Vector& relCoordSoft, float bias) |
---|
[4987] | 213 | { |
---|
[4993] | 214 | if (likely(this->toCoordinate == NULL)) |
---|
| 215 | this->toCoordinate = new Vector(); |
---|
[4987] | 216 | |
---|
[4993] | 217 | *this->toCoordinate = relCoordSoft; |
---|
[4992] | 218 | this->bias = bias; |
---|
[4987] | 219 | } |
---|
| 220 | |
---|
[4990] | 221 | |
---|
[4610] | 222 | /** |
---|
[4992] | 223 | * set relative coordinates smoothely |
---|
[4990] | 224 | * @param x x-relative coordinates to its parent |
---|
| 225 | * @param y y-relative coordinates to its parent |
---|
| 226 | * @param z z-relative coordinates to its parent |
---|
[4993] | 227 | * @see void PNode::setRelCoorSoft (const Vector&, float) |
---|
[4990] | 228 | */ |
---|
[4992] | 229 | void PNode::setRelCoorSoft (float x, float y, float z, float bias) |
---|
[4990] | 230 | { |
---|
[4992] | 231 | this->setRelCoorSoft(Vector(x, y, z), bias); |
---|
[4990] | 232 | } |
---|
| 233 | |
---|
[5382] | 234 | |
---|
[4990] | 235 | /** |
---|
[4836] | 236 | * @param absCoord set absolute coordinate |
---|
[5091] | 237 | */ |
---|
[3809] | 238 | void PNode::setAbsCoor (const Vector& absCoord) |
---|
[3675] | 239 | { |
---|
[5113] | 240 | if (this->toCoordinate!= NULL) |
---|
| 241 | { |
---|
| 242 | delete this->toCoordinate; |
---|
| 243 | this->toCoordinate = NULL; |
---|
| 244 | } |
---|
| 245 | |
---|
[4993] | 246 | if( likely(this->parentMode & PNODE_MOVEMENT)) |
---|
| 247 | { |
---|
| 248 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
| 249 | if (likely(this->parent != NULL)) |
---|
| 250 | this->relCoordinate = absCoord - parent->getAbsCoor (); |
---|
| 251 | else |
---|
| 252 | this->relCoordinate = absCoord; |
---|
| 253 | } |
---|
| 254 | if( this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
| 255 | { |
---|
| 256 | if (likely(this->parent != NULL)) |
---|
| 257 | this->relCoordinate = absCoord - parent->getAbsCoor (); |
---|
| 258 | else |
---|
| 259 | this->relCoordinate = absCoord; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | this->bRelCoorChanged = true; |
---|
| 263 | // this->absCoordinate = absCoord; |
---|
[3675] | 264 | } |
---|
| 265 | |
---|
[5382] | 266 | |
---|
[3675] | 267 | /** |
---|
[4836] | 268 | * @param x x-coordinate. |
---|
| 269 | * @param y y-coordinate. |
---|
| 270 | * @param z z-coordinate. |
---|
[4987] | 271 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
---|
[4610] | 272 | */ |
---|
| 273 | void PNode::setAbsCoor(float x, float y, float z) |
---|
| 274 | { |
---|
| 275 | this->setAbsCoor(Vector(x, y, z)); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | /** |
---|
[5406] | 279 | * @param absCoord set absolute coordinate |
---|
| 280 | * @todo check off |
---|
| 281 | */ |
---|
| 282 | void PNode::setAbsCoorSoft (const Vector& absCoordSoft, float bias) |
---|
| 283 | { |
---|
| 284 | if (this->toCoordinate == NULL) |
---|
| 285 | this->toCoordinate = new Vector; |
---|
| 286 | |
---|
| 287 | if( likely(this->parentMode & PNODE_MOVEMENT)) |
---|
| 288 | { |
---|
| 289 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
| 290 | if (likely(this->parent != NULL)) |
---|
| 291 | *this->toCoordinate = absCoordSoft - parent->getAbsCoor (); |
---|
| 292 | else |
---|
| 293 | *this->toCoordinate = absCoordSoft; |
---|
| 294 | } |
---|
| 295 | if( this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
| 296 | { |
---|
| 297 | if (likely(this->parent != NULL)) |
---|
| 298 | *this->toCoordinate = absCoordSoft - parent->getAbsCoor (); |
---|
| 299 | else |
---|
| 300 | *this->toCoordinate = absCoordSoft; |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | |
---|
| 305 | /** |
---|
[5091] | 306 | * shift coordinate relative |
---|
[4836] | 307 | * @param shift shift vector |
---|
[4987] | 308 | * |
---|
[5420] | 309 | * this function shifts the current coordinates about the vector shift. this is |
---|
| 310 | * usefull because from some place else you can: |
---|
| 311 | * PNode* someNode = ...; |
---|
| 312 | * Vector objectMovement = calculateShift(); |
---|
| 313 | * someNode->shiftCoor(objectMovement); |
---|
| 314 | * |
---|
| 315 | * this is the internal method of: |
---|
| 316 | * PNode* someNode = ...; |
---|
| 317 | * Vector objectMovement = calculateShift(); |
---|
| 318 | * Vector currentCoor = someNode->getRelCoor(); |
---|
| 319 | * Vector newCoor = currentCoor + objectMovement; |
---|
| 320 | * someNode->setRelCoor(newCoor); |
---|
| 321 | * |
---|
[5382] | 322 | */ |
---|
[3809] | 323 | void PNode::shiftCoor (const Vector& shift) |
---|
[3683] | 324 | { |
---|
[4993] | 325 | this->relCoordinate += shift; |
---|
| 326 | this->bRelCoorChanged = true; |
---|
[3683] | 327 | } |
---|
| 328 | |
---|
| 329 | /** |
---|
[4836] | 330 | * set relative direction |
---|
| 331 | * @param relDir to its parent |
---|
[5091] | 332 | */ |
---|
[3810] | 333 | void PNode::setRelDir (const Quaternion& relDir) |
---|
[3675] | 334 | { |
---|
[5113] | 335 | if (this->toDirection!= NULL) |
---|
| 336 | { |
---|
| 337 | delete this->toDirection; |
---|
| 338 | this->toDirection = NULL; |
---|
| 339 | } |
---|
[4993] | 340 | this->relDirection = relDir; |
---|
[3675] | 341 | this->bRelCoorChanged = true; |
---|
| 342 | } |
---|
| 343 | |
---|
[3365] | 344 | /** |
---|
[4771] | 345 | * @see void PNode::setRelDir (const Quaternion& relDir) |
---|
| 346 | * @param x the x direction |
---|
| 347 | * @param y the y direction |
---|
| 348 | * @param z the z direction |
---|
| 349 | * |
---|
| 350 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
| 351 | */ |
---|
| 352 | void PNode::setRelDir (float x, float y, float z) |
---|
| 353 | { |
---|
| 354 | this->setRelDir(Quaternion(Vector(x,y,z), Vector(0,1,0))); |
---|
| 355 | } |
---|
| 356 | |
---|
[4990] | 357 | |
---|
[4771] | 358 | /** |
---|
[4990] | 359 | * sets the Relative Direction of this node to its parent in a Smoothed way |
---|
| 360 | * @param relDirSoft the direction to iterate to smoothely. |
---|
[4992] | 361 | * @param bias how fast to iterate to the new Direction |
---|
[4990] | 362 | */ |
---|
[4992] | 363 | void PNode::setRelDirSoft(const Quaternion& relDirSoft, float bias) |
---|
[4990] | 364 | { |
---|
| 365 | if (likely(this->toDirection == NULL)) |
---|
| 366 | this->toDirection = new Quaternion(); |
---|
| 367 | |
---|
| 368 | *this->toDirection = relDirSoft; |
---|
[4992] | 369 | this->bias = bias; |
---|
[4990] | 370 | } |
---|
| 371 | |
---|
| 372 | /** |
---|
| 373 | * @see void PNode::setRelDirSoft (const Quaternion& relDir) |
---|
| 374 | * @param x the x direction |
---|
| 375 | * @param y the y direction |
---|
| 376 | * @param z the z direction |
---|
| 377 | * |
---|
| 378 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
| 379 | */ |
---|
[4992] | 380 | void PNode::setRelDirSoft(float x, float y, float z, float bias) |
---|
[4990] | 381 | { |
---|
[4992] | 382 | this->setRelDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias); |
---|
[4990] | 383 | } |
---|
| 384 | |
---|
| 385 | /** |
---|
[5091] | 386 | * sets the absolute direction |
---|
[4836] | 387 | * @param absDir absolute coordinates |
---|
[5091] | 388 | */ |
---|
[3810] | 389 | void PNode::setAbsDir (const Quaternion& absDir) |
---|
[3675] | 390 | { |
---|
[5113] | 391 | if (this->toDirection!= NULL) |
---|
| 392 | { |
---|
| 393 | delete this->toDirection; |
---|
| 394 | this->toDirection = NULL; |
---|
| 395 | } |
---|
| 396 | |
---|
[5001] | 397 | if (likely(this->parent != NULL)) |
---|
| 398 | this->relDirection = absDir / this->parent->getAbsDir(); |
---|
[4996] | 399 | else |
---|
[5001] | 400 | this->relDirection = absDir; |
---|
[4993] | 401 | |
---|
| 402 | this->bRelDirChanged = true; |
---|
[3675] | 403 | } |
---|
| 404 | |
---|
| 405 | /** |
---|
[4771] | 406 | * @see void PNode::setAbsDir (const Quaternion& relDir) |
---|
| 407 | * @param x the x direction |
---|
| 408 | * @param y the y direction |
---|
| 409 | * @param z the z direction |
---|
| 410 | * |
---|
| 411 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
| 412 | */ |
---|
| 413 | void PNode::setAbsDir (float x, float y, float z) |
---|
| 414 | { |
---|
| 415 | this->setAbsDir(Quaternion(Vector(x,y,z), Vector(0,1,0))); |
---|
| 416 | } |
---|
| 417 | |
---|
| 418 | /** |
---|
[5420] | 419 | * sets the absolute direction |
---|
[5414] | 420 | * @param absDir absolute coordinates |
---|
| 421 | */ |
---|
| 422 | void PNode::setAbsDirSoft (const Quaternion& absDirSoft, float bias) |
---|
| 423 | { |
---|
| 424 | if (this->toDirection == NULL) |
---|
| 425 | this->toDirection = new Quaternion(); |
---|
| 426 | |
---|
| 427 | if (likely(this->parent != NULL)) |
---|
| 428 | *this->toDirection = absDirSoft / this->parent->getAbsDir(); |
---|
| 429 | else |
---|
| 430 | *this->toDirection = absDirSoft; |
---|
| 431 | |
---|
| 432 | this->bias = bias; |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | /** |
---|
| 436 | * @see void PNode::setAbsDir (const Quaternion& relDir) |
---|
| 437 | * @param x the x direction |
---|
| 438 | * @param y the y direction |
---|
| 439 | * @param z the z direction |
---|
| 440 | * |
---|
| 441 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
| 442 | */ |
---|
| 443 | void PNode::setAbsDirSoft (float x, float y, float z, float bias) |
---|
| 444 | { |
---|
| 445 | this->setAbsDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias); |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | |
---|
| 449 | /** |
---|
[5091] | 450 | * shift Direction |
---|
| 451 | * @param shift the direction around which to shift. |
---|
| 452 | */ |
---|
[3802] | 453 | void PNode::shiftDir (const Quaternion& shift) |
---|
| 454 | { |
---|
[4993] | 455 | this->relDirection = this->relDirection * shift; |
---|
[3802] | 456 | this->bRelDirChanged = true; |
---|
| 457 | } |
---|
[3365] | 458 | |
---|
[3683] | 459 | /** |
---|
[4836] | 460 | * adds a child and makes this node to a parent |
---|
[5091] | 461 | * @param child child reference |
---|
[4993] | 462 | * use this to add a child to this node. |
---|
[5420] | 463 | */ |
---|
[5382] | 464 | void PNode::addChild (PNode* child) |
---|
[3365] | 465 | { |
---|
[4993] | 466 | if( likely(child->parent != NULL)) |
---|
[3521] | 467 | { |
---|
[5255] | 468 | PRINTF(5)("PNode::addChild() - reparenting node: removing it and adding it again\n"); |
---|
[4993] | 469 | child->parent->children->remove(child); |
---|
[3521] | 470 | } |
---|
[4993] | 471 | child->parent = this; |
---|
[5397] | 472 | if (unlikely(this != NULL)) |
---|
| 473 | this->children->add(child); |
---|
[4993] | 474 | child->parentCoorChanged(); |
---|
[3365] | 475 | } |
---|
| 476 | |
---|
| 477 | /** |
---|
[5091] | 478 | * @see PNode::addChild(PNode* child); |
---|
[4765] | 479 | * @param childName the name of the child to add to this PNode |
---|
| 480 | */ |
---|
| 481 | void PNode::addChild (const char* childName) |
---|
| 482 | { |
---|
| 483 | PNode* childNode = dynamic_cast<PNode*>(ClassList::getObject(childName, CL_PARENT_NODE)); |
---|
| 484 | if (childNode != NULL) |
---|
| 485 | this->addChild(childNode); |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | /** |
---|
[4836] | 489 | * removes a child from the node |
---|
[5091] | 490 | * @param child the child to remove from this pNode. |
---|
[4993] | 491 | * |
---|
| 492 | * Children from pNode will not be lost, they are referenced to NullPointer |
---|
[5420] | 493 | */ |
---|
[4993] | 494 | void PNode::removeChild (PNode* child) |
---|
[3365] | 495 | { |
---|
[5214] | 496 | if (child != NULL) |
---|
| 497 | { |
---|
| 498 | child->remove(); |
---|
| 499 | // this->children->remove(child); |
---|
| 500 | // child->parent = NULL; |
---|
| 501 | } |
---|
[3365] | 502 | } |
---|
| 503 | |
---|
| 504 | /** |
---|
[4836] | 505 | * remove this pnode from the tree and adds all following to NullParent |
---|
[5420] | 506 | * |
---|
| 507 | * this can be the case, if an entity in the world is being destroyed. |
---|
| 508 | */ |
---|
[3537] | 509 | void PNode::remove() |
---|
| 510 | { |
---|
[3668] | 511 | tIterator<PNode>* iterator = this->children->getIterator(); |
---|
[5115] | 512 | PNode* pn = iterator->firstElement(); |
---|
[4570] | 513 | |
---|
| 514 | while( pn != NULL) |
---|
| 515 | { |
---|
[5382] | 516 | NullParent::getInstance()->addChild(pn); |
---|
[3668] | 517 | pn = iterator->nextElement(); |
---|
[3537] | 518 | } |
---|
[3668] | 519 | delete iterator; |
---|
[5214] | 520 | if (this->parent != NULL) |
---|
| 521 | this->parent->children->remove(this); |
---|
[3537] | 522 | } |
---|
| 523 | |
---|
| 524 | /** |
---|
[4993] | 525 | * sets the parent of this PNode |
---|
[4836] | 526 | * @param parent the Parent to set |
---|
[5420] | 527 | */ |
---|
[3365] | 528 | void PNode::setParent (PNode* parent) |
---|
| 529 | { |
---|
[3511] | 530 | parent->addChild(this); |
---|
[3365] | 531 | } |
---|
| 532 | |
---|
[4761] | 533 | /** |
---|
| 534 | * @see PNode::setParent(PNode* parent); |
---|
| 535 | * @param parentName the name of the Parent to set to this PNode |
---|
| 536 | */ |
---|
| 537 | void PNode::setParent (const char* parentName) |
---|
| 538 | { |
---|
| 539 | PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE)); |
---|
| 540 | if (parentNode != NULL) |
---|
| 541 | parentNode->addChild(this); |
---|
| 542 | } |
---|
| 543 | |
---|
[4990] | 544 | /** |
---|
| 545 | * does the reparenting in a very smooth way |
---|
| 546 | * @param parentNode the new Node to connect this node to. |
---|
[4993] | 547 | * @param bias the speed to iterate to this new Positions |
---|
[4990] | 548 | */ |
---|
[5382] | 549 | void PNode::setParentSoft(PNode* parentNode, float bias) |
---|
[4987] | 550 | { |
---|
[5382] | 551 | // return if the new parent and the old one match |
---|
[4992] | 552 | if (this->parent == parentNode) |
---|
| 553 | return; |
---|
| 554 | |
---|
[5382] | 555 | // store the Valures to iterate to. |
---|
[4993] | 556 | if (likely(this->toCoordinate == NULL)) |
---|
[4989] | 557 | { |
---|
[4993] | 558 | this->toCoordinate = new Vector(); |
---|
| 559 | *this->toCoordinate = this->getRelCoor(); |
---|
[4989] | 560 | } |
---|
[4990] | 561 | if (likely(this->toDirection == NULL)) |
---|
| 562 | { |
---|
| 563 | this->toDirection = new Quaternion(); |
---|
| 564 | *this->toDirection = this->getRelDir(); |
---|
| 565 | } |
---|
[4992] | 566 | this->bias = bias; |
---|
[4987] | 567 | |
---|
| 568 | |
---|
[4990] | 569 | Vector tmpV = this->getAbsCoor(); |
---|
| 570 | Quaternion tmpQ = this->getAbsDir(); |
---|
[4987] | 571 | |
---|
| 572 | parentNode->addChild(this); |
---|
| 573 | |
---|
[5382] | 574 | if (this->parentMode & PNODE_ROTATE_MOVEMENT && this->parent != NULL) |
---|
| 575 | this->relCoordinate = this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor()); |
---|
[5000] | 576 | else |
---|
[5382] | 577 | this->relCoordinate = tmpV - parentNode->getAbsCoor(); |
---|
[4991] | 578 | |
---|
[5382] | 579 | this->relDirection = tmpQ / parentNode->getAbsDir(); |
---|
[4987] | 580 | } |
---|
| 581 | |
---|
[4993] | 582 | /** |
---|
| 583 | * does the reparenting in a very smooth way |
---|
| 584 | * @param parentName the name of the Parent to reconnect to |
---|
| 585 | * @param bias the speed to iterate to this new Positions |
---|
| 586 | */ |
---|
[5382] | 587 | void PNode::setParentSoft(const char* parentName, float bias) |
---|
[4987] | 588 | { |
---|
| 589 | PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE)); |
---|
| 590 | if (parentNode != NULL) |
---|
[5382] | 591 | this->setParentSoft(parentNode, bias); |
---|
[4987] | 592 | } |
---|
| 593 | |
---|
[3365] | 594 | /** |
---|
[4836] | 595 | * sets the mode of this parent manually |
---|
[4765] | 596 | * @param parentMode a String representing this parentingMode |
---|
| 597 | */ |
---|
| 598 | void PNode::setParentMode (const char* parentingMode) |
---|
| 599 | { |
---|
[4993] | 600 | this->setParentMode(PNode::charToParentingMode(parentingMode)); |
---|
[4765] | 601 | } |
---|
[3537] | 602 | |
---|
[3365] | 603 | /** |
---|
[4836] | 604 | * updates the absCoordinate/absDirection |
---|
| 605 | * @param dt The time passed since the last update |
---|
[5420] | 606 | * |
---|
| 607 | * this is used to go through the parent-tree to update all the absolute coordinates |
---|
| 608 | * and directions. this update should be done by the engine, so you don't have to |
---|
| 609 | * worry, normaly... |
---|
| 610 | */ |
---|
[3644] | 611 | void PNode::update (float dt) |
---|
[3365] | 612 | { |
---|
[4440] | 613 | if( likely(this->parent != NULL)) |
---|
| 614 | { |
---|
[4987] | 615 | // movement for nodes with smoothMove enabled |
---|
[4993] | 616 | if (unlikely(this->toCoordinate != NULL)) |
---|
[4987] | 617 | { |
---|
[5382] | 618 | Vector moveVect = (*this->toCoordinate - this->relCoordinate) *fabsf(dt)*bias; |
---|
[5006] | 619 | if (likely(moveVect.len() >= PNODE_ITERATION_DELTA)) |
---|
[4987] | 620 | { |
---|
| 621 | this->shiftCoor(moveVect); |
---|
| 622 | } |
---|
| 623 | else |
---|
| 624 | { |
---|
[4993] | 625 | delete this->toCoordinate; |
---|
| 626 | this->toCoordinate = NULL; |
---|
[4988] | 627 | PRINTF(5)("SmoothMove of %s finished\n", this->getName()); |
---|
[4987] | 628 | } |
---|
| 629 | } |
---|
[4990] | 630 | if (unlikely(this->toDirection != NULL)) |
---|
| 631 | { |
---|
[5419] | 632 | Quaternion rotQuat = Quaternion::quatSlerp(this->relDirection,*this->toDirection, fabsf(dt)*this->bias); |
---|
| 633 | if (this->relDirection.distance(rotQuat) >PNODE_ITERATION_DELTA) |
---|
[4990] | 634 | { |
---|
[5419] | 635 | this->relDirection = rotQuat; |
---|
| 636 | this->bRelDirChanged; |
---|
[4990] | 637 | } |
---|
[4998] | 638 | else |
---|
[4990] | 639 | { |
---|
[5000] | 640 | delete this->toDirection; |
---|
| 641 | this->toDirection = NULL; |
---|
[5041] | 642 | PRINTF(5)("SmoothRotate of %s finished\n", this->getName()); |
---|
[4998] | 643 | } |
---|
[4990] | 644 | } |
---|
| 645 | |
---|
[4993] | 646 | // MAIN UPDATE ///////////////////////////////////// |
---|
[4440] | 647 | this->lastAbsCoordinate = this->absCoordinate; |
---|
[4145] | 648 | |
---|
[4987] | 649 | PRINTF(5)("PNode::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
[3800] | 650 | |
---|
[4570] | 651 | |
---|
[4993] | 652 | if( this->parentMode & PNODE_LOCAL_ROTATE && this->bRelDirChanged) |
---|
| 653 | { |
---|
| 654 | /* update the current absDirection - remember * means rotation around sth.*/ |
---|
[5050] | 655 | this->prevRelCoordinate = this->relCoordinate; |
---|
[5001] | 656 | this->absDirection = this->relDirection * parent->getAbsDir();; |
---|
[4993] | 657 | } |
---|
[4570] | 658 | |
---|
[5089] | 659 | if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged)) |
---|
[4993] | 660 | { |
---|
| 661 | /* update the current absCoordinate */ |
---|
[5050] | 662 | this->prevRelCoordinate = this->relCoordinate; |
---|
[5007] | 663 | this->absCoordinate = this->parent->getAbsCoor() + this->relCoordinate; |
---|
| 664 | } |
---|
[5089] | 665 | else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged) |
---|
[5007] | 666 | { |
---|
| 667 | /* update the current absCoordinate */ |
---|
[5083] | 668 | this->prevRelCoordinate = this->relCoordinate; |
---|
[4993] | 669 | this->absCoordinate = this->parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate); |
---|
| 670 | } |
---|
| 671 | ///////////////////////////////////////////////// |
---|
| 672 | } |
---|
[4440] | 673 | else |
---|
| 674 | { |
---|
| 675 | PRINTF(4)("NullParent::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
[4993] | 676 | if (this->bRelCoorChanged) |
---|
[5118] | 677 | { |
---|
| 678 | this->prevRelCoordinate = this->relCoordinate; |
---|
[4993] | 679 | this->absCoordinate = this->relCoordinate; |
---|
[5118] | 680 | } |
---|
[4993] | 681 | if (this->bRelDirChanged) |
---|
[5118] | 682 | { |
---|
| 683 | this->prevRelDirection = this->relDirection; |
---|
[4993] | 684 | this->absDirection = this->getAbsDir () * this->relDirection; |
---|
[5118] | 685 | } |
---|
[4993] | 686 | } |
---|
[3365] | 687 | |
---|
[4993] | 688 | if(this->children->getSize() > 0) |
---|
| 689 | { |
---|
[4440] | 690 | tIterator<PNode>* iterator = this->children->getIterator(); |
---|
[5115] | 691 | PNode* pn = iterator->firstElement(); |
---|
[4570] | 692 | while( pn != NULL) |
---|
[4574] | 693 | { |
---|
| 694 | /* if this node has changed, make sure, that all children are updated also */ |
---|
[4993] | 695 | if( likely(this->bRelCoorChanged)) |
---|
| 696 | pn->parentCoorChanged (); |
---|
| 697 | if( likely(this->bRelDirChanged)) |
---|
| 698 | pn->parentDirChanged (); |
---|
| 699 | |
---|
| 700 | pn->update(dt); |
---|
| 701 | pn = iterator->nextElement(); |
---|
| 702 | } |
---|
[4574] | 703 | delete iterator; |
---|
[4440] | 704 | } |
---|
[4993] | 705 | this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt; |
---|
| 706 | this->bRelCoorChanged = false; |
---|
| 707 | this->bRelDirChanged = false; |
---|
[3365] | 708 | } |
---|
| 709 | |
---|
[3450] | 710 | /** |
---|
[4836] | 711 | * displays some information about this pNode |
---|
| 712 | * @param depth The deph into which to debug the children of this PNode to. |
---|
[5383] | 713 | * (0: all children will be debugged, 1: only this PNode, 2: this and direct children, ...) |
---|
| 714 | * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output). |
---|
[5420] | 715 | */ |
---|
[4574] | 716 | void PNode::debug(unsigned int depth, unsigned int level) const |
---|
[3365] | 717 | { |
---|
[4574] | 718 | for (unsigned int i = 0; i < level; i++) |
---|
[4575] | 719 | PRINT(0)(" |"); |
---|
[4574] | 720 | if (this->children->getSize() > 0) |
---|
[4575] | 721 | PRINT(0)(" +"); |
---|
| 722 | else |
---|
| 723 | PRINT(0)(" -"); |
---|
[4996] | 724 | PRINT(0)("PNode(%s::%s) - absCoord: (%0.2f, %0.2f, %0.2f), relCoord(%0.2f, %0.2f, %0.2f), direction(%0.2f, %0.2f, %0.2f) - %s\n", |
---|
[4574] | 725 | this->getClassName(), |
---|
| 726 | this->getName(), |
---|
| 727 | this->absCoordinate.x, |
---|
| 728 | this->absCoordinate.y, |
---|
| 729 | this->absCoordinate.z, |
---|
| 730 | this->relCoordinate.x, |
---|
| 731 | this->relCoordinate.y, |
---|
[4993] | 732 | this->relCoordinate.z, |
---|
[4996] | 733 | this->getAbsDirV().x, |
---|
| 734 | this->getAbsDirV().y, |
---|
| 735 | this->getAbsDirV().z, |
---|
[4993] | 736 | this->parentingModeToChar(parentMode)); |
---|
[4574] | 737 | if (depth >= 2 || depth == 0) |
---|
| 738 | { |
---|
| 739 | tIterator<PNode>* iterator = this->children->getIterator(); |
---|
| 740 | //PNode* pn = this->children->enumerate (); |
---|
[5115] | 741 | PNode* pn = iterator->firstElement(); |
---|
[4574] | 742 | while( pn != NULL) |
---|
| 743 | { |
---|
| 744 | if (depth == 0) |
---|
| 745 | pn->debug(0, level + 1); |
---|
| 746 | else |
---|
| 747 | pn->debug(depth - 1, level +1); |
---|
| 748 | pn = iterator->nextElement(); |
---|
| 749 | } |
---|
| 750 | delete iterator; |
---|
| 751 | } |
---|
[3365] | 752 | } |
---|
| 753 | |
---|
[4570] | 754 | /** |
---|
[5383] | 755 | * displays the PNode at its position with its rotation as a cube. |
---|
| 756 | * @param depth The deph into which to debug the children of this PNode to. |
---|
| 757 | * (0: all children will be displayed, 1: only this PNode, 2: this and direct children, ...) |
---|
| 758 | * @param size the Size of the Box to draw. |
---|
| 759 | * @param color the color of the Box to display. |
---|
| 760 | * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output). |
---|
| 761 | */ |
---|
| 762 | void PNode::debugDraw(unsigned int depth, float size, const Vector& color, unsigned int level) const |
---|
[4570] | 763 | { |
---|
[5432] | 764 | // if this is the first Element we draw |
---|
[5383] | 765 | if (level == 0) |
---|
| 766 | { |
---|
[5432] | 767 | glPushAttrib(GL_ENABLE_BIT); // save the Enable-attributes |
---|
| 768 | glMatrixMode(GL_MODELVIEW); // goto the ModelView Matrix |
---|
[5383] | 769 | |
---|
[5432] | 770 | glDisable(GL_LIGHTING); // disable lighting (we do not need them for just lighting) |
---|
| 771 | glDisable(GL_BLEND); // '' |
---|
| 772 | glDisable(GL_TEXTURE_2D); // '' |
---|
[5438] | 773 | glDisable(GL_DEPTH_TEST); // '' |
---|
[5383] | 774 | } |
---|
| 775 | |
---|
[5432] | 776 | glPushMatrix(); // repush the Matrix-stack |
---|
[4570] | 777 | /* translate */ |
---|
| 778 | glTranslatef (this->getAbsCoor ().x, |
---|
| 779 | this->getAbsCoor ().y, |
---|
| 780 | this->getAbsCoor ().z); |
---|
[4998] | 781 | // this->getAbsDir ().matrix (matrix); |
---|
| 782 | |
---|
[5432] | 783 | /* rotate */ |
---|
[4998] | 784 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
[5432] | 785 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 786 | /* set the new Color */ |
---|
[5008] | 787 | glColor3f(color.x, color.y, color.z); |
---|
[5432] | 788 | { /* draw a cube of size size */ |
---|
[4570] | 789 | glBegin(GL_LINE_STRIP); |
---|
[4995] | 790 | glVertex3f(-.5*size, -.5*size, -.5*size); |
---|
| 791 | glVertex3f(+.5*size, -.5*size, -.5*size); |
---|
| 792 | glVertex3f(+.5*size, -.5*size, +.5*size); |
---|
| 793 | glVertex3f(-.5*size, -.5*size, +.5*size); |
---|
| 794 | glVertex3f(-.5*size, -.5*size, -.5*size); |
---|
[4570] | 795 | glEnd(); |
---|
| 796 | glBegin(GL_LINE_STRIP); |
---|
[4995] | 797 | glVertex3f(-.5*size, +.5*size, -.5*size); |
---|
| 798 | glVertex3f(+.5*size, +.5*size, -.5*size); |
---|
| 799 | glVertex3f(+.5*size, +.5*size, +.5*size); |
---|
| 800 | glVertex3f(-.5*size, +.5*size, +.5*size); |
---|
| 801 | glVertex3f(-.5*size, +.5*size, -.5*size); |
---|
[4570] | 802 | glEnd(); |
---|
[4995] | 803 | |
---|
[4570] | 804 | glBegin(GL_LINES); |
---|
[4995] | 805 | glVertex3f(-.5*size, -.5*size, -.5*size); |
---|
| 806 | glVertex3f(-.5*size, +.5*size, -.5*size); |
---|
| 807 | glVertex3f(+.5*size, -.5*size, -.5*size); |
---|
| 808 | glVertex3f(+.5*size, +.5*size, -.5*size); |
---|
| 809 | glVertex3f(+.5*size, -.5*size, +.5*size); |
---|
| 810 | glVertex3f(+.5*size, +.5*size, +.5*size); |
---|
| 811 | glVertex3f(-.5*size, -.5*size, +.5*size); |
---|
| 812 | glVertex3f(-.5*size, +.5*size, +.5*size); |
---|
[4570] | 813 | glEnd(); |
---|
| 814 | } |
---|
| 815 | |
---|
| 816 | glPopMatrix(); |
---|
[5007] | 817 | if (depth >= 2 || depth == 0) |
---|
| 818 | { |
---|
[5432] | 819 | /* rotate the current color in HSV space around 20 degree */ |
---|
[5115] | 820 | Vector childColor = Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0)); |
---|
[5111] | 821 | tIterator<PNode>* iterator = this->children->getIterator(); |
---|
[5115] | 822 | PNode* pn = iterator->firstElement(); |
---|
[5007] | 823 | while( pn != NULL) |
---|
| 824 | { |
---|
[5394] | 825 | // drawing the Dependency graph |
---|
[5406] | 826 | if (this != NullParent::getInstance()) |
---|
[5394] | 827 | { |
---|
| 828 | glBegin(GL_LINES); |
---|
| 829 | glColor3f(color.x, color.y, color.z); |
---|
| 830 | glVertex3f(this->getAbsCoor ().x, |
---|
| 831 | this->getAbsCoor ().y, |
---|
| 832 | this->getAbsCoor ().z); |
---|
| 833 | glColor3f(childColor.x, childColor.y, childColor.z); |
---|
| 834 | glVertex3f(pn->getAbsCoor ().x, |
---|
| 835 | pn->getAbsCoor ().y, |
---|
| 836 | pn->getAbsCoor ().z); |
---|
| 837 | glEnd(); |
---|
| 838 | } |
---|
[5432] | 839 | /* if we want to draw the children too */ |
---|
| 840 | if (depth == 0) /* -> all of them */ |
---|
[5383] | 841 | pn->debugDraw(0, size, childColor, level+1); |
---|
[5432] | 842 | else /* -> only the Next one */ |
---|
[5383] | 843 | pn->debugDraw(depth - 1, size, childColor, level +1); |
---|
[5007] | 844 | pn = iterator->nextElement(); |
---|
| 845 | } |
---|
| 846 | delete iterator; |
---|
| 847 | } |
---|
[5383] | 848 | if (level == 0) |
---|
[5432] | 849 | glPopAttrib(); /* pop the saved attributes back out */ |
---|
[4570] | 850 | } |
---|
[4993] | 851 | |
---|
| 852 | |
---|
| 853 | |
---|
| 854 | ///////////////////// |
---|
| 855 | // HELPER_FUCTIONS // |
---|
| 856 | ///////////////////// |
---|
| 857 | |
---|
| 858 | /** |
---|
| 859 | * converts a parentingMode into a string that is the name of it |
---|
| 860 | * @param parentingMode the ParentingMode to convert |
---|
| 861 | * @return the converted string |
---|
| 862 | */ |
---|
| 863 | const char* PNode::parentingModeToChar(int parentingMode) |
---|
| 864 | { |
---|
| 865 | if (parentingMode == PNODE_LOCAL_ROTATE) |
---|
| 866 | return "local-rotate"; |
---|
| 867 | else if (parentingMode == PNODE_ROTATE_MOVEMENT) |
---|
| 868 | return "rotate-movement"; |
---|
| 869 | else if (parentingMode == PNODE_MOVEMENT) |
---|
| 870 | return "movement"; |
---|
| 871 | else if (parentingMode == PNODE_ALL) |
---|
| 872 | return "all"; |
---|
| 873 | else if (parentingMode == PNODE_ROTATE_AND_MOVE) |
---|
| 874 | return "rotate-and-move"; |
---|
| 875 | } |
---|
| 876 | |
---|
| 877 | /** |
---|
| 878 | * converts a parenting-mode-string into a int |
---|
| 879 | * @param parentingMode the string naming the parentingMode |
---|
| 880 | * @return the int corresponding to the named parentingMode |
---|
| 881 | */ |
---|
| 882 | PARENT_MODE PNode::charToParentingMode(const char* parentingMode) |
---|
| 883 | { |
---|
| 884 | if (!strcmp(parentingMode, "local-rotate")) |
---|
| 885 | return (PNODE_LOCAL_ROTATE); |
---|
| 886 | else if (!strcmp(parentingMode, "rotate-movement")) |
---|
| 887 | return (PNODE_ROTATE_MOVEMENT); |
---|
| 888 | else if (!strcmp(parentingMode, "movement")) |
---|
| 889 | return (PNODE_MOVEMENT); |
---|
| 890 | else if (!strcmp(parentingMode, "all")) |
---|
| 891 | return (PNODE_ALL); |
---|
| 892 | else if (!strcmp(parentingMode, "rotate-and-move")) |
---|
| 893 | return (PNODE_ROTATE_AND_MOVE); |
---|
| 894 | } |
---|