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