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