| 1 | /* |
|---|
| 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 |
|---|
| 13 | co-programmer: |
|---|
| 14 | |
|---|
| 15 | @todo Smooth-Parent: delay, speed |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PNODE |
|---|
| 19 | |
|---|
| 20 | #include "p_node.h" |
|---|
| 21 | #include "null_parent.h" |
|---|
| 22 | |
|---|
| 23 | #include "load_param.h" |
|---|
| 24 | #include "class_list.h" |
|---|
| 25 | |
|---|
| 26 | #include "stdincl.h" |
|---|
| 27 | #include "compiler.h" |
|---|
| 28 | #include "error.h" |
|---|
| 29 | #include "debug.h" |
|---|
| 30 | #include "list.h" |
|---|
| 31 | #include "vector.h" |
|---|
| 32 | |
|---|
| 33 | //#include "vector.h" |
|---|
| 34 | //#include "quaternion.h" |
|---|
| 35 | |
|---|
| 36 | using namespace std; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * standard constructor |
|---|
| 41 | */ |
|---|
| 42 | PNode::PNode () |
|---|
| 43 | { |
|---|
| 44 | init(NULL); |
|---|
| 45 | |
|---|
| 46 | NullParent::getInstance()->addChild(this); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * @param root the load-Element for the PNode |
|---|
| 51 | */ |
|---|
| 52 | PNode::PNode(const TiXmlElement* root) |
|---|
| 53 | { |
|---|
| 54 | this->init(NULL); |
|---|
| 55 | this->loadParams(root); |
|---|
| 56 | |
|---|
| 57 | NullParent::getInstance()->addChild(this); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * constructor with coodinates |
|---|
| 62 | * @param absCoordinate the Absolute coordinate of the Object |
|---|
| 63 | * @param parent The parent-node of this node. |
|---|
| 64 | */ |
|---|
| 65 | PNode::PNode (const Vector& absCoor, PNode* parent ) |
|---|
| 66 | { |
|---|
| 67 | this->init(parent); |
|---|
| 68 | |
|---|
| 69 | if (likely(parent != NULL)) |
|---|
| 70 | parent->addChild (this); |
|---|
| 71 | |
|---|
| 72 | this->setAbsCoor(absCoor); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * standard deconstructor |
|---|
| 77 | */ |
|---|
| 78 | PNode::~PNode () |
|---|
| 79 | { |
|---|
| 80 | tIterator<PNode>* iterator = this->children->getIterator(); |
|---|
| 81 | PNode* pn = iterator->firstElement(); |
|---|
| 82 | while( pn != NULL) |
|---|
| 83 | { |
|---|
| 84 | delete pn; |
|---|
| 85 | pn = iterator->nextElement(); |
|---|
| 86 | } |
|---|
| 87 | delete iterator; |
|---|
| 88 | /* this deletes all children in the list */ |
|---|
| 89 | delete this->children; |
|---|
| 90 | if (this->parent) |
|---|
| 91 | this->parent->removeChild(this); |
|---|
| 92 | |
|---|
| 93 | if (this->toCoordinate != NULL) |
|---|
| 94 | delete this->toCoordinate; |
|---|
| 95 | if (this->toDirection != NULL) |
|---|
| 96 | delete this->toDirection; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | * initializes a PNode |
|---|
| 101 | * @param parent the parent for this PNode |
|---|
| 102 | */ |
|---|
| 103 | void PNode::init(PNode* parent) |
|---|
| 104 | { |
|---|
| 105 | this->setClassID(CL_PARENT_NODE, "PNode"); |
|---|
| 106 | this->children = new tList<PNode>(); |
|---|
| 107 | this->bRelCoorChanged = true; |
|---|
| 108 | this->bRelDirChanged = true; |
|---|
| 109 | this->parent = parent; |
|---|
| 110 | |
|---|
| 111 | // iterators |
|---|
| 112 | this->toCoordinate = NULL; |
|---|
| 113 | this->toDirection = NULL; |
|---|
| 114 | this->bias = 1.0; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * loads parameters of the PNode |
|---|
| 119 | * @param root the XML-element to load the properties of |
|---|
| 120 | */ |
|---|
| 121 | void PNode::loadParams(const TiXmlElement* root) |
|---|
| 122 | { |
|---|
| 123 | static_cast<BaseObject*>(this)->loadParams(root); |
|---|
| 124 | |
|---|
| 125 | LoadParam<PNode>(root, "rel-coor", this, &PNode::setRelCoor) |
|---|
| 126 | .describe("Sets The relative position of the Node to its parent."); |
|---|
| 127 | |
|---|
| 128 | LoadParam<PNode>(root, "abs-coor", this, &PNode::setAbsCoor) |
|---|
| 129 | .describe("Sets The absolute Position of the Node."); |
|---|
| 130 | |
|---|
| 131 | LoadParam<PNode>(root, "rel-dir", this, &PNode::setRelDir) |
|---|
| 132 | .describe("Sets The relative rotation of the Node to its parent."); |
|---|
| 133 | |
|---|
| 134 | LoadParam<PNode>(root, "abs-dir", this, &PNode::setAbsDir) |
|---|
| 135 | .describe("Sets The absolute rotation of the Node."); |
|---|
| 136 | |
|---|
| 137 | LoadParam<PNode>(root, "parent", this, &PNode::setParent) |
|---|
| 138 | .describe("the Name of the Parent of this PNode"); |
|---|
| 139 | |
|---|
| 140 | LoadParam<PNode>(root, "parent-mode", this, &PNode::setParentMode) |
|---|
| 141 | .describe("the mode to connect this node to its parent ()"); |
|---|
| 142 | |
|---|
| 143 | // cycling properties |
|---|
| 144 | if (root != NULL) |
|---|
| 145 | { |
|---|
| 146 | const TiXmlElement* element = root->FirstChildElement(); |
|---|
| 147 | while (element != NULL) |
|---|
| 148 | { |
|---|
| 149 | LoadParam<PNode>(element, "parent", this, &PNode::addChild, true) |
|---|
| 150 | .describe("adds a new Child to the current Node."); |
|---|
| 151 | |
|---|
| 152 | element = element->NextSiblingElement(); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | /** |
|---|
| 158 | * set relative coordinates |
|---|
| 159 | * @param relCoord relative coordinates to its parent |
|---|
| 160 | |
|---|
| 161 | it is very importand, that you use this function, if you want to update the |
|---|
| 162 | relCoordinates. If you don't use this, the PNode won't recognize, that something |
|---|
| 163 | has changed and won't update the children Nodes. |
|---|
| 164 | */ |
|---|
| 165 | void PNode::setRelCoor (const Vector& relCoord) |
|---|
| 166 | { |
|---|
| 167 | if (this->toCoordinate!= NULL) |
|---|
| 168 | { |
|---|
| 169 | delete this->toCoordinate; |
|---|
| 170 | this->toCoordinate = NULL; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | this->relCoordinate = relCoord; |
|---|
| 174 | this->bRelCoorChanged = true; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | /** |
|---|
| 178 | * set relative coordinates |
|---|
| 179 | * @param x x-relative coordinates to its parent |
|---|
| 180 | * @param y y-relative coordinates to its parent |
|---|
| 181 | * @param z z-relative coordinates to its parent |
|---|
| 182 | * @see void PNode::setRelCoor (const Vector& relCoord) |
|---|
| 183 | */ |
|---|
| 184 | void PNode::setRelCoor (float x, float y, float z) |
|---|
| 185 | { |
|---|
| 186 | this->setRelCoor(Vector(x, y, z)); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /** |
|---|
| 190 | * sets a new relative position smoothely |
|---|
| 191 | * @param relCoordSoft the new Position to iterate to |
|---|
| 192 | * @param bias how fast to iterate to this position |
|---|
| 193 | */ |
|---|
| 194 | void PNode::setRelCoorSoft(const Vector& relCoordSoft, float bias) |
|---|
| 195 | { |
|---|
| 196 | if (likely(this->toCoordinate == NULL)) |
|---|
| 197 | this->toCoordinate = new Vector(); |
|---|
| 198 | |
|---|
| 199 | *this->toCoordinate = relCoordSoft; |
|---|
| 200 | this->bias = bias; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | /** |
|---|
| 205 | * set relative coordinates smoothely |
|---|
| 206 | * @param x x-relative coordinates to its parent |
|---|
| 207 | * @param y y-relative coordinates to its parent |
|---|
| 208 | * @param z z-relative coordinates to its parent |
|---|
| 209 | * @see void PNode::setRelCoorSoft (const Vector&, float) |
|---|
| 210 | */ |
|---|
| 211 | void PNode::setRelCoorSoft (float x, float y, float z, float bias) |
|---|
| 212 | { |
|---|
| 213 | this->setRelCoorSoft(Vector(x, y, z), bias); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | /** |
|---|
| 217 | * @param absCoord set absolute coordinate |
|---|
| 218 | */ |
|---|
| 219 | void PNode::setAbsCoor (const Vector& absCoord) |
|---|
| 220 | { |
|---|
| 221 | if (this->toCoordinate!= NULL) |
|---|
| 222 | { |
|---|
| 223 | delete this->toCoordinate; |
|---|
| 224 | this->toCoordinate = NULL; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | if( likely(this->parentMode & PNODE_MOVEMENT)) |
|---|
| 228 | { |
|---|
| 229 | /* if you have set the absolute coordinates this overrides all other changes */ |
|---|
| 230 | if (likely(this->parent != NULL)) |
|---|
| 231 | this->relCoordinate = absCoord - parent->getAbsCoor (); |
|---|
| 232 | else |
|---|
| 233 | this->relCoordinate = absCoord; |
|---|
| 234 | } |
|---|
| 235 | if( this->parentMode & PNODE_ROTATE_MOVEMENT) |
|---|
| 236 | { |
|---|
| 237 | if (likely(this->parent != NULL)) |
|---|
| 238 | this->relCoordinate = absCoord - parent->getAbsCoor (); |
|---|
| 239 | else |
|---|
| 240 | this->relCoordinate = absCoord; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | this->bRelCoorChanged = true; |
|---|
| 244 | // this->absCoordinate = absCoord; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | /** |
|---|
| 248 | * @param x x-coordinate. |
|---|
| 249 | * @param y y-coordinate. |
|---|
| 250 | * @param z z-coordinate. |
|---|
| 251 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
|---|
| 252 | */ |
|---|
| 253 | void PNode::setAbsCoor(float x, float y, float z) |
|---|
| 254 | { |
|---|
| 255 | this->setAbsCoor(Vector(x, y, z)); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | /** |
|---|
| 259 | * shift coordinate relative |
|---|
| 260 | * @param shift shift vector |
|---|
| 261 | |
|---|
| 262 | this function shifts the current coordinates about the vector shift. this is |
|---|
| 263 | usefull because from some place else you can: |
|---|
| 264 | PNode* someNode = ...; |
|---|
| 265 | Vector objectMovement = calculateShift(); |
|---|
| 266 | someNode->shiftCoor(objectMovement); |
|---|
| 267 | |
|---|
| 268 | elsewhere you would have to: |
|---|
| 269 | PNode* someNode = ...; |
|---|
| 270 | Vector objectMovement = calculateShift(); |
|---|
| 271 | Vector currentCoor = someNode->getRelCoor(); |
|---|
| 272 | Vector newCoor = currentCoor + objectMovement; |
|---|
| 273 | someNode->setRelCoor(newCoor); |
|---|
| 274 | |
|---|
| 275 | yea right... shorter... |
|---|
| 276 | * |
|---|
| 277 | */ |
|---|
| 278 | void PNode::shiftCoor (const Vector& shift) |
|---|
| 279 | { |
|---|
| 280 | this->relCoordinate += shift; |
|---|
| 281 | this->bRelCoorChanged = true; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | /** |
|---|
| 285 | * set relative direction |
|---|
| 286 | * @param relDir to its parent |
|---|
| 287 | */ |
|---|
| 288 | void PNode::setRelDir (const Quaternion& relDir) |
|---|
| 289 | { |
|---|
| 290 | if (this->toDirection!= NULL) |
|---|
| 291 | { |
|---|
| 292 | delete this->toDirection; |
|---|
| 293 | this->toDirection = NULL; |
|---|
| 294 | } |
|---|
| 295 | this->relDirection = relDir; |
|---|
| 296 | this->bRelCoorChanged = true; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | /** |
|---|
| 300 | * @see void PNode::setRelDir (const Quaternion& relDir) |
|---|
| 301 | * @param x the x direction |
|---|
| 302 | * @param y the y direction |
|---|
| 303 | * @param z the z direction |
|---|
| 304 | * |
|---|
| 305 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
|---|
| 306 | */ |
|---|
| 307 | void PNode::setRelDir (float x, float y, float z) |
|---|
| 308 | { |
|---|
| 309 | this->setRelDir(Quaternion(Vector(x,y,z), Vector(0,1,0))); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | /** |
|---|
| 314 | * sets the Relative Direction of this node to its parent in a Smoothed way |
|---|
| 315 | * @param relDirSoft the direction to iterate to smoothely. |
|---|
| 316 | * @param bias how fast to iterate to the new Direction |
|---|
| 317 | */ |
|---|
| 318 | void PNode::setRelDirSoft(const Quaternion& relDirSoft, float bias) |
|---|
| 319 | { |
|---|
| 320 | if (likely(this->toDirection == NULL)) |
|---|
| 321 | this->toDirection = new Quaternion(); |
|---|
| 322 | |
|---|
| 323 | *this->toDirection = relDirSoft; |
|---|
| 324 | this->bias = bias; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | /** |
|---|
| 328 | * @see void PNode::setRelDirSoft (const Quaternion& relDir) |
|---|
| 329 | * @param x the x direction |
|---|
| 330 | * @param y the y direction |
|---|
| 331 | * @param z the z direction |
|---|
| 332 | * |
|---|
| 333 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
|---|
| 334 | */ |
|---|
| 335 | void PNode::setRelDirSoft(float x, float y, float z, float bias) |
|---|
| 336 | { |
|---|
| 337 | this->setRelDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias); |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | /** |
|---|
| 341 | * sets the absolute direction |
|---|
| 342 | * @param absDir absolute coordinates |
|---|
| 343 | */ |
|---|
| 344 | void PNode::setAbsDir (const Quaternion& absDir) |
|---|
| 345 | { |
|---|
| 346 | if (this->toDirection!= NULL) |
|---|
| 347 | { |
|---|
| 348 | delete this->toDirection; |
|---|
| 349 | this->toDirection = NULL; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | if (likely(this->parent != NULL)) |
|---|
| 353 | this->relDirection = absDir / this->parent->getAbsDir(); |
|---|
| 354 | else |
|---|
| 355 | this->relDirection = absDir; |
|---|
| 356 | |
|---|
| 357 | this->bRelDirChanged = true; |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | /** |
|---|
| 361 | * @see void PNode::setAbsDir (const Quaternion& relDir) |
|---|
| 362 | * @param x the x direction |
|---|
| 363 | * @param y the y direction |
|---|
| 364 | * @param z the z direction |
|---|
| 365 | * |
|---|
| 366 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
|---|
| 367 | */ |
|---|
| 368 | void PNode::setAbsDir (float x, float y, float z) |
|---|
| 369 | { |
|---|
| 370 | this->setAbsDir(Quaternion(Vector(x,y,z), Vector(0,1,0))); |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | /** |
|---|
| 374 | * shift Direction |
|---|
| 375 | * @param shift the direction around which to shift. |
|---|
| 376 | */ |
|---|
| 377 | void PNode::shiftDir (const Quaternion& shift) |
|---|
| 378 | { |
|---|
| 379 | this->relDirection = this->relDirection * shift; |
|---|
| 380 | this->bRelDirChanged = true; |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | /** |
|---|
| 384 | * adds a child and makes this node to a parent |
|---|
| 385 | * @param child child reference |
|---|
| 386 | * @param parentMode on which changes the child should also change ist state |
|---|
| 387 | * |
|---|
| 388 | * use this to add a child to this node. |
|---|
| 389 | */ |
|---|
| 390 | void PNode::addChild (PNode* child, int parentMode) |
|---|
| 391 | { |
|---|
| 392 | if( likely(child->parent != NULL)) |
|---|
| 393 | { |
|---|
| 394 | PRINTF(4)("PNode::addChild() - reparenting node: removing it and adding it again\n"); |
|---|
| 395 | child->parent->children->remove(child); |
|---|
| 396 | } |
|---|
| 397 | child->parentMode = parentMode; |
|---|
| 398 | child->parent = this; |
|---|
| 399 | this->children->add(child); |
|---|
| 400 | child->parentCoorChanged(); |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | /** |
|---|
| 404 | * @see PNode::addChild(PNode* child); |
|---|
| 405 | * @param childName the name of the child to add to this PNode |
|---|
| 406 | */ |
|---|
| 407 | void PNode::addChild (const char* childName) |
|---|
| 408 | { |
|---|
| 409 | PNode* childNode = dynamic_cast<PNode*>(ClassList::getObject(childName, CL_PARENT_NODE)); |
|---|
| 410 | if (childNode != NULL) |
|---|
| 411 | this->addChild(childNode); |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | /** |
|---|
| 415 | * removes a child from the node |
|---|
| 416 | * @param child the child to remove from this pNode. |
|---|
| 417 | * |
|---|
| 418 | * Children from pNode will not be lost, they are referenced to NullPointer |
|---|
| 419 | */ |
|---|
| 420 | void PNode::removeChild (PNode* child) |
|---|
| 421 | { |
|---|
| 422 | child->remove(); |
|---|
| 423 | this->children->remove(child); |
|---|
| 424 | child->parent = NULL; |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | /** |
|---|
| 428 | * remove this pnode from the tree and adds all following to NullParent |
|---|
| 429 | |
|---|
| 430 | this can be the case, if an entity in the world is being destroyed. |
|---|
| 431 | */ |
|---|
| 432 | void PNode::remove() |
|---|
| 433 | { |
|---|
| 434 | NullParent* nullParent = NullParent::getInstance(); |
|---|
| 435 | |
|---|
| 436 | tIterator<PNode>* iterator = this->children->getIterator(); |
|---|
| 437 | PNode* pn = iterator->firstElement(); |
|---|
| 438 | |
|---|
| 439 | while( pn != NULL) |
|---|
| 440 | { |
|---|
| 441 | nullParent->addChild(pn, pn->getParentMode()); |
|---|
| 442 | pn = iterator->nextElement(); |
|---|
| 443 | } |
|---|
| 444 | delete iterator; |
|---|
| 445 | this->parent->children->remove(this); |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | /** |
|---|
| 449 | * sets the parent of this PNode |
|---|
| 450 | * @param parent the Parent to set |
|---|
| 451 | */ |
|---|
| 452 | void PNode::setParent (PNode* parent) |
|---|
| 453 | { |
|---|
| 454 | parent->addChild(this); |
|---|
| 455 | } |
|---|
| 456 | |
|---|
| 457 | /** |
|---|
| 458 | * @see PNode::setParent(PNode* parent); |
|---|
| 459 | * @param parentName the name of the Parent to set to this PNode |
|---|
| 460 | */ |
|---|
| 461 | void PNode::setParent (const char* parentName) |
|---|
| 462 | { |
|---|
| 463 | PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE)); |
|---|
| 464 | if (parentNode != NULL) |
|---|
| 465 | parentNode->addChild(this); |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | /** |
|---|
| 469 | * does the reparenting in a very smooth way |
|---|
| 470 | * @param parentNode the new Node to connect this node to. |
|---|
| 471 | * @param bias the speed to iterate to this new Positions |
|---|
| 472 | */ |
|---|
| 473 | void PNode::softReparent(PNode* parentNode, float bias) |
|---|
| 474 | { |
|---|
| 475 | if (this->parent == parentNode) |
|---|
| 476 | return; |
|---|
| 477 | |
|---|
| 478 | if (likely(this->toCoordinate == NULL)) |
|---|
| 479 | { |
|---|
| 480 | this->toCoordinate = new Vector(); |
|---|
| 481 | *this->toCoordinate = this->getRelCoor(); |
|---|
| 482 | } |
|---|
| 483 | if (likely(this->toDirection == NULL)) |
|---|
| 484 | { |
|---|
| 485 | this->toDirection = new Quaternion(); |
|---|
| 486 | *this->toDirection = this->getRelDir(); |
|---|
| 487 | } |
|---|
| 488 | this->bias = bias; |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | Vector tmpV = this->getAbsCoor(); |
|---|
| 492 | Quaternion tmpQ = this->getAbsDir(); |
|---|
| 493 | |
|---|
| 494 | parentNode->addChild(this); |
|---|
| 495 | |
|---|
| 496 | if (this->parentMode & PNODE_ROTATE_MOVEMENT) |
|---|
| 497 | this->setRelCoor(this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor())); |
|---|
| 498 | else |
|---|
| 499 | this->setRelCoor(tmpV - parentNode->getAbsCoor()); |
|---|
| 500 | |
|---|
| 501 | this->setRelDir(tmpQ / parentNode->getAbsDir()); |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | /** |
|---|
| 505 | * does the reparenting in a very smooth way |
|---|
| 506 | * @param parentName the name of the Parent to reconnect to |
|---|
| 507 | * @param bias the speed to iterate to this new Positions |
|---|
| 508 | */ |
|---|
| 509 | void PNode::softReparent(const char* parentName, float bias) |
|---|
| 510 | { |
|---|
| 511 | PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE)); |
|---|
| 512 | if (parentNode != NULL) |
|---|
| 513 | this->softReparent(parentNode, bias); |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | /** |
|---|
| 517 | * sets the mode of this parent manually |
|---|
| 518 | * @param parentMode a String representing this parentingMode |
|---|
| 519 | */ |
|---|
| 520 | void PNode::setParentMode (const char* parentingMode) |
|---|
| 521 | { |
|---|
| 522 | this->setParentMode(PNode::charToParentingMode(parentingMode)); |
|---|
| 523 | } |
|---|
| 524 | |
|---|
| 525 | /** |
|---|
| 526 | * updates the absCoordinate/absDirection |
|---|
| 527 | * @param dt The time passed since the last update |
|---|
| 528 | |
|---|
| 529 | this is used to go through the parent-tree to update all the absolute coordinates |
|---|
| 530 | and directions. this update should be done by the engine, so you don't have to |
|---|
| 531 | worry, normaly... |
|---|
| 532 | */ |
|---|
| 533 | void PNode::update (float dt) |
|---|
| 534 | { |
|---|
| 535 | if( likely(this->parent != NULL)) |
|---|
| 536 | { |
|---|
| 537 | // movement for nodes with smoothMove enabled |
|---|
| 538 | if (unlikely(this->toCoordinate != NULL)) |
|---|
| 539 | { |
|---|
| 540 | Vector moveVect = (*this->toCoordinate - this->getRelCoor()) *dt*bias; |
|---|
| 541 | |
|---|
| 542 | if (likely(moveVect.len() >= PNODE_ITERATION_DELTA)) |
|---|
| 543 | { |
|---|
| 544 | this->shiftCoor(moveVect); |
|---|
| 545 | } |
|---|
| 546 | else |
|---|
| 547 | { |
|---|
| 548 | delete this->toCoordinate; |
|---|
| 549 | this->toCoordinate = NULL; |
|---|
| 550 | PRINTF(5)("SmoothMove of %s finished\n", this->getName()); |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | if (unlikely(this->toDirection != NULL)) |
|---|
| 554 | { |
|---|
| 555 | Quaternion rotQuat = Quaternion::quatSlerp(Quaternion(), (*this->toDirection / this->relDirection), dt*this->bias); |
|---|
| 556 | if (likely(rotQuat.getSpacialAxisAngle() > PNODE_ITERATION_DELTA)) |
|---|
| 557 | { |
|---|
| 558 | this->shiftDir(rotQuat); |
|---|
| 559 | } |
|---|
| 560 | else |
|---|
| 561 | { |
|---|
| 562 | delete this->toDirection; |
|---|
| 563 | this->toDirection = NULL; |
|---|
| 564 | PRINTF(5)("SmoothRotate of %s finished\n", this->getName()); |
|---|
| 565 | } |
|---|
| 566 | } |
|---|
| 567 | |
|---|
| 568 | // MAIN UPDATE ///////////////////////////////////// |
|---|
| 569 | this->lastAbsCoordinate = this->absCoordinate; |
|---|
| 570 | |
|---|
| 571 | PRINTF(5)("PNode::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | if( this->parentMode & PNODE_LOCAL_ROTATE && this->bRelDirChanged) |
|---|
| 575 | { |
|---|
| 576 | /* update the current absDirection - remember * means rotation around sth.*/ |
|---|
| 577 | this->prevRelCoordinate = this->relCoordinate; |
|---|
| 578 | this->absDirection = this->relDirection * parent->getAbsDir();; |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged)) |
|---|
| 582 | { |
|---|
| 583 | /* update the current absCoordinate */ |
|---|
| 584 | this->prevRelCoordinate = this->relCoordinate; |
|---|
| 585 | this->absCoordinate = this->parent->getAbsCoor() + this->relCoordinate; |
|---|
| 586 | } |
|---|
| 587 | else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged) |
|---|
| 588 | { |
|---|
| 589 | /* update the current absCoordinate */ |
|---|
| 590 | this->prevRelCoordinate = this->relCoordinate; |
|---|
| 591 | this->absCoordinate = this->parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate); |
|---|
| 592 | } |
|---|
| 593 | ///////////////////////////////////////////////// |
|---|
| 594 | } |
|---|
| 595 | else |
|---|
| 596 | { |
|---|
| 597 | PRINTF(4)("NullParent::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
|---|
| 598 | if (this->bRelCoorChanged) |
|---|
| 599 | { |
|---|
| 600 | this->prevRelCoordinate = this->relCoordinate; |
|---|
| 601 | this->absCoordinate = this->relCoordinate; |
|---|
| 602 | } |
|---|
| 603 | if (this->bRelDirChanged) |
|---|
| 604 | { |
|---|
| 605 | this->prevRelDirection = this->relDirection; |
|---|
| 606 | this->absDirection = this->getAbsDir () * this->relDirection; |
|---|
| 607 | } |
|---|
| 608 | } |
|---|
| 609 | |
|---|
| 610 | if(this->children->getSize() > 0) |
|---|
| 611 | { |
|---|
| 612 | tIterator<PNode>* iterator = this->children->getIterator(); |
|---|
| 613 | PNode* pn = iterator->firstElement(); |
|---|
| 614 | while( pn != NULL) |
|---|
| 615 | { |
|---|
| 616 | /* if this node has changed, make sure, that all children are updated also */ |
|---|
| 617 | if( likely(this->bRelCoorChanged)) |
|---|
| 618 | pn->parentCoorChanged (); |
|---|
| 619 | if( likely(this->bRelDirChanged)) |
|---|
| 620 | pn->parentDirChanged (); |
|---|
| 621 | |
|---|
| 622 | pn->update(dt); |
|---|
| 623 | pn = iterator->nextElement(); |
|---|
| 624 | } |
|---|
| 625 | delete iterator; |
|---|
| 626 | } |
|---|
| 627 | this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt; |
|---|
| 628 | this->bRelCoorChanged = false; |
|---|
| 629 | this->bRelDirChanged = false; |
|---|
| 630 | } |
|---|
| 631 | |
|---|
| 632 | /** |
|---|
| 633 | * displays some information about this pNode |
|---|
| 634 | * @param depth The deph into which to debug the children of this PNode to. |
|---|
| 635 | * (0: all children will be debugged, 1: only this PNode, 2: this and direct children...) |
|---|
| 636 | * @param level The n-th level of the Node we draw (this is internal and only for nice output) |
|---|
| 637 | */ |
|---|
| 638 | void PNode::debug(unsigned int depth, unsigned int level) const |
|---|
| 639 | { |
|---|
| 640 | for (unsigned int i = 0; i < level; i++) |
|---|
| 641 | PRINT(0)(" |"); |
|---|
| 642 | if (this->children->getSize() > 0) |
|---|
| 643 | PRINT(0)(" +"); |
|---|
| 644 | else |
|---|
| 645 | PRINT(0)(" -"); |
|---|
| 646 | 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", |
|---|
| 647 | this->getClassName(), |
|---|
| 648 | this->getName(), |
|---|
| 649 | this->absCoordinate.x, |
|---|
| 650 | this->absCoordinate.y, |
|---|
| 651 | this->absCoordinate.z, |
|---|
| 652 | this->relCoordinate.x, |
|---|
| 653 | this->relCoordinate.y, |
|---|
| 654 | this->relCoordinate.z, |
|---|
| 655 | this->getAbsDirV().x, |
|---|
| 656 | this->getAbsDirV().y, |
|---|
| 657 | this->getAbsDirV().z, |
|---|
| 658 | this->parentingModeToChar(parentMode)); |
|---|
| 659 | if (depth >= 2 || depth == 0) |
|---|
| 660 | { |
|---|
| 661 | tIterator<PNode>* iterator = this->children->getIterator(); |
|---|
| 662 | //PNode* pn = this->children->enumerate (); |
|---|
| 663 | PNode* pn = iterator->firstElement(); |
|---|
| 664 | while( pn != NULL) |
|---|
| 665 | { |
|---|
| 666 | if (depth == 0) |
|---|
| 667 | pn->debug(0, level + 1); |
|---|
| 668 | else |
|---|
| 669 | pn->debug(depth - 1, level +1); |
|---|
| 670 | pn = iterator->nextElement(); |
|---|
| 671 | } |
|---|
| 672 | delete iterator; |
|---|
| 673 | } |
|---|
| 674 | } |
|---|
| 675 | #include "color.h" |
|---|
| 676 | |
|---|
| 677 | /** |
|---|
| 678 | displays the PNode at its position with its rotation as a cube. |
|---|
| 679 | */ |
|---|
| 680 | void PNode::debugDraw(unsigned int depth, float size, Vector color) const |
|---|
| 681 | { |
|---|
| 682 | glMatrixMode(GL_MODELVIEW); |
|---|
| 683 | glPushMatrix(); |
|---|
| 684 | glDisable(GL_LIGHTING); |
|---|
| 685 | |
|---|
| 686 | /* translate */ |
|---|
| 687 | glTranslatef (this->getAbsCoor ().x, |
|---|
| 688 | this->getAbsCoor ().y, |
|---|
| 689 | this->getAbsCoor ().z); |
|---|
| 690 | /* rotate */ |
|---|
| 691 | // this->getAbsDir ().matrix (matrix); |
|---|
| 692 | // glMultMatrixf((float*)matrix); |
|---|
| 693 | |
|---|
| 694 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
|---|
| 695 | glColor3f(color.x, color.y, color.z); |
|---|
| 696 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
|---|
| 697 | { |
|---|
| 698 | glBegin(GL_LINE_STRIP); |
|---|
| 699 | glVertex3f(-.5*size, -.5*size, -.5*size); |
|---|
| 700 | glVertex3f(+.5*size, -.5*size, -.5*size); |
|---|
| 701 | glVertex3f(+.5*size, -.5*size, +.5*size); |
|---|
| 702 | glVertex3f(-.5*size, -.5*size, +.5*size); |
|---|
| 703 | glVertex3f(-.5*size, -.5*size, -.5*size); |
|---|
| 704 | glEnd(); |
|---|
| 705 | glBegin(GL_LINE_STRIP); |
|---|
| 706 | glVertex3f(-.5*size, +.5*size, -.5*size); |
|---|
| 707 | glVertex3f(+.5*size, +.5*size, -.5*size); |
|---|
| 708 | glVertex3f(+.5*size, +.5*size, +.5*size); |
|---|
| 709 | glVertex3f(-.5*size, +.5*size, +.5*size); |
|---|
| 710 | glVertex3f(-.5*size, +.5*size, -.5*size); |
|---|
| 711 | glEnd(); |
|---|
| 712 | |
|---|
| 713 | glBegin(GL_LINES); |
|---|
| 714 | glVertex3f(-.5*size, -.5*size, -.5*size); |
|---|
| 715 | glVertex3f(-.5*size, +.5*size, -.5*size); |
|---|
| 716 | glVertex3f(+.5*size, -.5*size, -.5*size); |
|---|
| 717 | glVertex3f(+.5*size, +.5*size, -.5*size); |
|---|
| 718 | glVertex3f(+.5*size, -.5*size, +.5*size); |
|---|
| 719 | glVertex3f(+.5*size, +.5*size, +.5*size); |
|---|
| 720 | glVertex3f(-.5*size, -.5*size, +.5*size); |
|---|
| 721 | glVertex3f(-.5*size, +.5*size, +.5*size); |
|---|
| 722 | glEnd(); |
|---|
| 723 | } |
|---|
| 724 | |
|---|
| 725 | glPopMatrix(); |
|---|
| 726 | glEnable(GL_LIGHTING); |
|---|
| 727 | if (depth >= 2 || depth == 0) |
|---|
| 728 | { |
|---|
| 729 | Vector childColor = Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0)); |
|---|
| 730 | |
|---|
| 731 | tIterator<PNode>* iterator = this->children->getIterator(); |
|---|
| 732 | PNode* pn = iterator->firstElement(); |
|---|
| 733 | while( pn != NULL) |
|---|
| 734 | { |
|---|
| 735 | if (depth == 0) |
|---|
| 736 | pn->debugDraw(0, size, childColor); |
|---|
| 737 | else |
|---|
| 738 | pn->debugDraw(depth - 1, size, childColor); |
|---|
| 739 | pn = iterator->nextElement(); |
|---|
| 740 | } |
|---|
| 741 | delete iterator; |
|---|
| 742 | } |
|---|
| 743 | } |
|---|
| 744 | |
|---|
| 745 | |
|---|
| 746 | |
|---|
| 747 | ///////////////////// |
|---|
| 748 | // HELPER_FUCTIONS // |
|---|
| 749 | ///////////////////// |
|---|
| 750 | |
|---|
| 751 | /** |
|---|
| 752 | * converts a parentingMode into a string that is the name of it |
|---|
| 753 | * @param parentingMode the ParentingMode to convert |
|---|
| 754 | * @return the converted string |
|---|
| 755 | */ |
|---|
| 756 | const char* PNode::parentingModeToChar(int parentingMode) |
|---|
| 757 | { |
|---|
| 758 | if (parentingMode == PNODE_LOCAL_ROTATE) |
|---|
| 759 | return "local-rotate"; |
|---|
| 760 | else if (parentingMode == PNODE_ROTATE_MOVEMENT) |
|---|
| 761 | return "rotate-movement"; |
|---|
| 762 | else if (parentingMode == PNODE_MOVEMENT) |
|---|
| 763 | return "movement"; |
|---|
| 764 | else if (parentingMode == PNODE_ALL) |
|---|
| 765 | return "all"; |
|---|
| 766 | else if (parentingMode == PNODE_ROTATE_AND_MOVE) |
|---|
| 767 | return "rotate-and-move"; |
|---|
| 768 | } |
|---|
| 769 | |
|---|
| 770 | /** |
|---|
| 771 | * converts a parenting-mode-string into a int |
|---|
| 772 | * @param parentingMode the string naming the parentingMode |
|---|
| 773 | * @return the int corresponding to the named parentingMode |
|---|
| 774 | */ |
|---|
| 775 | PARENT_MODE PNode::charToParentingMode(const char* parentingMode) |
|---|
| 776 | { |
|---|
| 777 | if (!strcmp(parentingMode, "local-rotate")) |
|---|
| 778 | return (PNODE_LOCAL_ROTATE); |
|---|
| 779 | else if (!strcmp(parentingMode, "rotate-movement")) |
|---|
| 780 | return (PNODE_ROTATE_MOVEMENT); |
|---|
| 781 | else if (!strcmp(parentingMode, "movement")) |
|---|
| 782 | return (PNODE_MOVEMENT); |
|---|
| 783 | else if (!strcmp(parentingMode, "all")) |
|---|
| 784 | return (PNODE_ALL); |
|---|
| 785 | else if (!strcmp(parentingMode, "rotate-and-move")) |
|---|
| 786 | return (PNODE_ROTATE_AND_MOVE); |
|---|
| 787 | } |
|---|