[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(); |
---|
[5115] | 81 | PNode* pn = iterator->firstElement(); |
---|
[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 | { |
---|
[5113] | 167 | if (this->toCoordinate!= NULL) |
---|
| 168 | { |
---|
| 169 | delete this->toCoordinate; |
---|
| 170 | this->toCoordinate = NULL; |
---|
| 171 | } |
---|
| 172 | |
---|
[4993] | 173 | this->relCoordinate = relCoord; |
---|
[3675] | 174 | this->bRelCoorChanged = true; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
[4836] | 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 |
---|
[4993] | 182 | * @see void PNode::setRelCoor (const Vector& relCoord) |
---|
[4610] | 183 | */ |
---|
| 184 | void PNode::setRelCoor (float x, float y, float z) |
---|
| 185 | { |
---|
| 186 | this->setRelCoor(Vector(x, y, z)); |
---|
| 187 | } |
---|
| 188 | |
---|
[4992] | 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) |
---|
[4987] | 195 | { |
---|
[4993] | 196 | if (likely(this->toCoordinate == NULL)) |
---|
| 197 | this->toCoordinate = new Vector(); |
---|
[4987] | 198 | |
---|
[4993] | 199 | *this->toCoordinate = relCoordSoft; |
---|
[4992] | 200 | this->bias = bias; |
---|
[4987] | 201 | } |
---|
| 202 | |
---|
[4990] | 203 | |
---|
[4610] | 204 | /** |
---|
[4992] | 205 | * set relative coordinates smoothely |
---|
[4990] | 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 |
---|
[4993] | 209 | * @see void PNode::setRelCoorSoft (const Vector&, float) |
---|
[4990] | 210 | */ |
---|
[4992] | 211 | void PNode::setRelCoorSoft (float x, float y, float z, float bias) |
---|
[4990] | 212 | { |
---|
[4992] | 213 | this->setRelCoorSoft(Vector(x, y, z), bias); |
---|
[4990] | 214 | } |
---|
| 215 | |
---|
| 216 | /** |
---|
[4836] | 217 | * @param absCoord set absolute coordinate |
---|
[5091] | 218 | */ |
---|
[3809] | 219 | void PNode::setAbsCoor (const Vector& absCoord) |
---|
[3675] | 220 | { |
---|
[5113] | 221 | if (this->toCoordinate!= NULL) |
---|
| 222 | { |
---|
| 223 | delete this->toCoordinate; |
---|
| 224 | this->toCoordinate = NULL; |
---|
| 225 | } |
---|
| 226 | |
---|
[4993] | 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; |
---|
[3675] | 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
[4836] | 248 | * @param x x-coordinate. |
---|
| 249 | * @param y y-coordinate. |
---|
| 250 | * @param z z-coordinate. |
---|
[4987] | 251 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
---|
[4610] | 252 | */ |
---|
| 253 | void PNode::setAbsCoor(float x, float y, float z) |
---|
| 254 | { |
---|
| 255 | this->setAbsCoor(Vector(x, y, z)); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | /** |
---|
[5091] | 259 | * shift coordinate relative |
---|
[4836] | 260 | * @param shift shift vector |
---|
[3365] | 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); |
---|
[4570] | 274 | |
---|
[3365] | 275 | yea right... shorter... |
---|
[4987] | 276 | * |
---|
[3365] | 277 | */ |
---|
[3809] | 278 | void PNode::shiftCoor (const Vector& shift) |
---|
[3683] | 279 | { |
---|
[4993] | 280 | this->relCoordinate += shift; |
---|
| 281 | this->bRelCoorChanged = true; |
---|
[3683] | 282 | } |
---|
| 283 | |
---|
| 284 | /** |
---|
[4836] | 285 | * set relative direction |
---|
| 286 | * @param relDir to its parent |
---|
[5091] | 287 | */ |
---|
[3810] | 288 | void PNode::setRelDir (const Quaternion& relDir) |
---|
[3675] | 289 | { |
---|
[5113] | 290 | if (this->toDirection!= NULL) |
---|
| 291 | { |
---|
| 292 | delete this->toDirection; |
---|
| 293 | this->toDirection = NULL; |
---|
| 294 | } |
---|
[4993] | 295 | this->relDirection = relDir; |
---|
[3675] | 296 | this->bRelCoorChanged = true; |
---|
| 297 | } |
---|
| 298 | |
---|
[3365] | 299 | /** |
---|
[4771] | 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 | |
---|
[4990] | 312 | |
---|
[4771] | 313 | /** |
---|
[4990] | 314 | * sets the Relative Direction of this node to its parent in a Smoothed way |
---|
| 315 | * @param relDirSoft the direction to iterate to smoothely. |
---|
[4992] | 316 | * @param bias how fast to iterate to the new Direction |
---|
[4990] | 317 | */ |
---|
[4992] | 318 | void PNode::setRelDirSoft(const Quaternion& relDirSoft, float bias) |
---|
[4990] | 319 | { |
---|
| 320 | if (likely(this->toDirection == NULL)) |
---|
| 321 | this->toDirection = new Quaternion(); |
---|
| 322 | |
---|
| 323 | *this->toDirection = relDirSoft; |
---|
[4992] | 324 | this->bias = bias; |
---|
[4990] | 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 | */ |
---|
[4992] | 335 | void PNode::setRelDirSoft(float x, float y, float z, float bias) |
---|
[4990] | 336 | { |
---|
[4992] | 337 | this->setRelDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias); |
---|
[4990] | 338 | } |
---|
| 339 | |
---|
| 340 | /** |
---|
[5091] | 341 | * sets the absolute direction |
---|
[4836] | 342 | * @param absDir absolute coordinates |
---|
[5091] | 343 | */ |
---|
[3810] | 344 | void PNode::setAbsDir (const Quaternion& absDir) |
---|
[3675] | 345 | { |
---|
[5113] | 346 | if (this->toDirection!= NULL) |
---|
| 347 | { |
---|
| 348 | delete this->toDirection; |
---|
| 349 | this->toDirection = NULL; |
---|
| 350 | } |
---|
| 351 | |
---|
[5001] | 352 | if (likely(this->parent != NULL)) |
---|
| 353 | this->relDirection = absDir / this->parent->getAbsDir(); |
---|
[4996] | 354 | else |
---|
[5001] | 355 | this->relDirection = absDir; |
---|
[4993] | 356 | |
---|
| 357 | this->bRelDirChanged = true; |
---|
[3675] | 358 | } |
---|
| 359 | |
---|
| 360 | /** |
---|
[4771] | 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 | /** |
---|
[5091] | 374 | * shift Direction |
---|
| 375 | * @param shift the direction around which to shift. |
---|
| 376 | */ |
---|
[3802] | 377 | void PNode::shiftDir (const Quaternion& shift) |
---|
| 378 | { |
---|
[4993] | 379 | this->relDirection = this->relDirection * shift; |
---|
[3802] | 380 | this->bRelDirChanged = true; |
---|
| 381 | } |
---|
[3365] | 382 | |
---|
[3683] | 383 | /** |
---|
[4836] | 384 | * adds a child and makes this node to a parent |
---|
[5091] | 385 | * @param child child reference |
---|
[4836] | 386 | * @param parentMode on which changes the child should also change ist state |
---|
[4993] | 387 | * |
---|
| 388 | * use this to add a child to this node. |
---|
[3365] | 389 | */ |
---|
[4993] | 390 | void PNode::addChild (PNode* child, int parentMode) |
---|
[3365] | 391 | { |
---|
[4993] | 392 | if( likely(child->parent != NULL)) |
---|
[3521] | 393 | { |
---|
[4992] | 394 | PRINTF(4)("PNode::addChild() - reparenting node: removing it and adding it again\n"); |
---|
[4993] | 395 | child->parent->children->remove(child); |
---|
[3521] | 396 | } |
---|
[4993] | 397 | child->parentMode = parentMode; |
---|
| 398 | child->parent = this; |
---|
| 399 | this->children->add(child); |
---|
| 400 | child->parentCoorChanged(); |
---|
[3365] | 401 | } |
---|
| 402 | |
---|
| 403 | /** |
---|
[5091] | 404 | * @see PNode::addChild(PNode* child); |
---|
[4765] | 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 | /** |
---|
[4836] | 415 | * removes a child from the node |
---|
[5091] | 416 | * @param child the child to remove from this pNode. |
---|
[4993] | 417 | * |
---|
| 418 | * Children from pNode will not be lost, they are referenced to NullPointer |
---|
[3365] | 419 | */ |
---|
[4993] | 420 | void PNode::removeChild (PNode* child) |
---|
[3365] | 421 | { |
---|
[4993] | 422 | child->remove(); |
---|
| 423 | this->children->remove(child); |
---|
| 424 | child->parent = NULL; |
---|
[3365] | 425 | } |
---|
| 426 | |
---|
| 427 | /** |
---|
[4836] | 428 | * remove this pnode from the tree and adds all following to NullParent |
---|
[3537] | 429 | |
---|
[5091] | 430 | this can be the case, if an entity in the world is being destroyed. |
---|
[3537] | 431 | */ |
---|
| 432 | void PNode::remove() |
---|
| 433 | { |
---|
[3662] | 434 | NullParent* nullParent = NullParent::getInstance(); |
---|
| 435 | |
---|
[3668] | 436 | tIterator<PNode>* iterator = this->children->getIterator(); |
---|
[5115] | 437 | PNode* pn = iterator->firstElement(); |
---|
[4570] | 438 | |
---|
| 439 | while( pn != NULL) |
---|
| 440 | { |
---|
[4444] | 441 | nullParent->addChild(pn, pn->getParentMode()); |
---|
[3668] | 442 | pn = iterator->nextElement(); |
---|
[3537] | 443 | } |
---|
[3668] | 444 | delete iterator; |
---|
[3669] | 445 | this->parent->children->remove(this); |
---|
[3537] | 446 | } |
---|
| 447 | |
---|
| 448 | /** |
---|
[4993] | 449 | * sets the parent of this PNode |
---|
[4836] | 450 | * @param parent the Parent to set |
---|
[3365] | 451 | */ |
---|
| 452 | void PNode::setParent (PNode* parent) |
---|
| 453 | { |
---|
[3511] | 454 | parent->addChild(this); |
---|
[3365] | 455 | } |
---|
| 456 | |
---|
[4761] | 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 | |
---|
[4990] | 468 | /** |
---|
| 469 | * does the reparenting in a very smooth way |
---|
| 470 | * @param parentNode the new Node to connect this node to. |
---|
[4993] | 471 | * @param bias the speed to iterate to this new Positions |
---|
[4990] | 472 | */ |
---|
[4992] | 473 | void PNode::softReparent(PNode* parentNode, float bias) |
---|
[4987] | 474 | { |
---|
[4992] | 475 | if (this->parent == parentNode) |
---|
| 476 | return; |
---|
| 477 | |
---|
[4993] | 478 | if (likely(this->toCoordinate == NULL)) |
---|
[4989] | 479 | { |
---|
[4993] | 480 | this->toCoordinate = new Vector(); |
---|
| 481 | *this->toCoordinate = this->getRelCoor(); |
---|
[4989] | 482 | } |
---|
[4990] | 483 | if (likely(this->toDirection == NULL)) |
---|
| 484 | { |
---|
| 485 | this->toDirection = new Quaternion(); |
---|
| 486 | *this->toDirection = this->getRelDir(); |
---|
| 487 | } |
---|
[4992] | 488 | this->bias = bias; |
---|
[4987] | 489 | |
---|
| 490 | |
---|
[4990] | 491 | Vector tmpV = this->getAbsCoor(); |
---|
| 492 | Quaternion tmpQ = this->getAbsDir(); |
---|
[4987] | 493 | |
---|
| 494 | parentNode->addChild(this); |
---|
| 495 | |
---|
[5000] | 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()); |
---|
[4991] | 500 | |
---|
[4997] | 501 | this->setRelDir(tmpQ / parentNode->getAbsDir()); |
---|
[4987] | 502 | } |
---|
| 503 | |
---|
[4993] | 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 | */ |
---|
[4992] | 509 | void PNode::softReparent(const char* parentName, float bias) |
---|
[4987] | 510 | { |
---|
| 511 | PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE)); |
---|
| 512 | if (parentNode != NULL) |
---|
[4992] | 513 | this->softReparent(parentNode, bias); |
---|
[4987] | 514 | } |
---|
| 515 | |
---|
[3365] | 516 | /** |
---|
[4836] | 517 | * sets the mode of this parent manually |
---|
[4765] | 518 | * @param parentMode a String representing this parentingMode |
---|
| 519 | */ |
---|
| 520 | void PNode::setParentMode (const char* parentingMode) |
---|
| 521 | { |
---|
[4993] | 522 | this->setParentMode(PNode::charToParentingMode(parentingMode)); |
---|
[4765] | 523 | } |
---|
[3537] | 524 | |
---|
[3365] | 525 | /** |
---|
[4836] | 526 | * updates the absCoordinate/absDirection |
---|
| 527 | * @param dt The time passed since the last update |
---|
[3365] | 528 | |
---|
| 529 | this is used to go through the parent-tree to update all the absolute coordinates |
---|
[4570] | 530 | and directions. this update should be done by the engine, so you don't have to |
---|
[3365] | 531 | worry, normaly... |
---|
| 532 | */ |
---|
[3644] | 533 | void PNode::update (float dt) |
---|
[3365] | 534 | { |
---|
[4440] | 535 | if( likely(this->parent != NULL)) |
---|
| 536 | { |
---|
[4987] | 537 | // movement for nodes with smoothMove enabled |
---|
[4993] | 538 | if (unlikely(this->toCoordinate != NULL)) |
---|
[4987] | 539 | { |
---|
[4993] | 540 | Vector moveVect = (*this->toCoordinate - this->getRelCoor()) *dt*bias; |
---|
[4987] | 541 | |
---|
[5006] | 542 | if (likely(moveVect.len() >= PNODE_ITERATION_DELTA)) |
---|
[4987] | 543 | { |
---|
| 544 | this->shiftCoor(moveVect); |
---|
| 545 | } |
---|
| 546 | else |
---|
| 547 | { |
---|
[4993] | 548 | delete this->toCoordinate; |
---|
| 549 | this->toCoordinate = NULL; |
---|
[4988] | 550 | PRINTF(5)("SmoothMove of %s finished\n", this->getName()); |
---|
[4987] | 551 | } |
---|
| 552 | } |
---|
[4990] | 553 | if (unlikely(this->toDirection != NULL)) |
---|
| 554 | { |
---|
[5006] | 555 | Quaternion rotQuat = Quaternion::quatSlerp(Quaternion(), (*this->toDirection / this->relDirection), dt*this->bias); |
---|
| 556 | if (likely(rotQuat.getSpacialAxisAngle() > PNODE_ITERATION_DELTA)) |
---|
[4990] | 557 | { |
---|
| 558 | this->shiftDir(rotQuat); |
---|
| 559 | } |
---|
[4998] | 560 | else |
---|
[4990] | 561 | { |
---|
[5000] | 562 | delete this->toDirection; |
---|
| 563 | this->toDirection = NULL; |
---|
[5041] | 564 | PRINTF(5)("SmoothRotate of %s finished\n", this->getName()); |
---|
[4998] | 565 | } |
---|
[4990] | 566 | } |
---|
| 567 | |
---|
[4993] | 568 | // MAIN UPDATE ///////////////////////////////////// |
---|
[4440] | 569 | this->lastAbsCoordinate = this->absCoordinate; |
---|
[4145] | 570 | |
---|
[4987] | 571 | PRINTF(5)("PNode::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
[3800] | 572 | |
---|
[4570] | 573 | |
---|
[4993] | 574 | if( this->parentMode & PNODE_LOCAL_ROTATE && this->bRelDirChanged) |
---|
| 575 | { |
---|
| 576 | /* update the current absDirection - remember * means rotation around sth.*/ |
---|
[5050] | 577 | this->prevRelCoordinate = this->relCoordinate; |
---|
[5001] | 578 | this->absDirection = this->relDirection * parent->getAbsDir();; |
---|
[4993] | 579 | } |
---|
[4570] | 580 | |
---|
[5089] | 581 | if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged)) |
---|
[4993] | 582 | { |
---|
| 583 | /* update the current absCoordinate */ |
---|
[5050] | 584 | this->prevRelCoordinate = this->relCoordinate; |
---|
[5007] | 585 | this->absCoordinate = this->parent->getAbsCoor() + this->relCoordinate; |
---|
| 586 | } |
---|
[5089] | 587 | else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged) |
---|
[5007] | 588 | { |
---|
| 589 | /* update the current absCoordinate */ |
---|
[5083] | 590 | this->prevRelCoordinate = this->relCoordinate; |
---|
[4993] | 591 | this->absCoordinate = this->parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate); |
---|
| 592 | } |
---|
| 593 | ///////////////////////////////////////////////// |
---|
| 594 | } |
---|
[4440] | 595 | else |
---|
| 596 | { |
---|
| 597 | PRINTF(4)("NullParent::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
[4993] | 598 | if (this->bRelCoorChanged) |
---|
[5118] | 599 | { |
---|
| 600 | this->prevRelCoordinate = this->relCoordinate; |
---|
[4993] | 601 | this->absCoordinate = this->relCoordinate; |
---|
[5118] | 602 | } |
---|
[4993] | 603 | if (this->bRelDirChanged) |
---|
[5118] | 604 | { |
---|
| 605 | this->prevRelDirection = this->relDirection; |
---|
[4993] | 606 | this->absDirection = this->getAbsDir () * this->relDirection; |
---|
[5118] | 607 | } |
---|
[4993] | 608 | } |
---|
[3365] | 609 | |
---|
[4993] | 610 | if(this->children->getSize() > 0) |
---|
| 611 | { |
---|
[4440] | 612 | tIterator<PNode>* iterator = this->children->getIterator(); |
---|
[5115] | 613 | PNode* pn = iterator->firstElement(); |
---|
[4570] | 614 | while( pn != NULL) |
---|
[4574] | 615 | { |
---|
| 616 | /* if this node has changed, make sure, that all children are updated also */ |
---|
[4993] | 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 | } |
---|
[4574] | 625 | delete iterator; |
---|
[4440] | 626 | } |
---|
[4993] | 627 | this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt; |
---|
| 628 | this->bRelCoorChanged = false; |
---|
| 629 | this->bRelDirChanged = false; |
---|
[3365] | 630 | } |
---|
| 631 | |
---|
[3450] | 632 | /** |
---|
[4836] | 633 | * displays some information about this pNode |
---|
| 634 | * @param depth The deph into which to debug the children of this PNode to. |
---|
[5091] | 635 | * (0: all children will be debugged, 1: only this PNode, 2: this and direct children...) |
---|
[4836] | 636 | * @param level The n-th level of the Node we draw (this is internal and only for nice output) |
---|
[3450] | 637 | */ |
---|
[4574] | 638 | void PNode::debug(unsigned int depth, unsigned int level) const |
---|
[3365] | 639 | { |
---|
[4574] | 640 | for (unsigned int i = 0; i < level; i++) |
---|
[4575] | 641 | PRINT(0)(" |"); |
---|
[4574] | 642 | if (this->children->getSize() > 0) |
---|
[4575] | 643 | PRINT(0)(" +"); |
---|
| 644 | else |
---|
| 645 | PRINT(0)(" -"); |
---|
[4996] | 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", |
---|
[4574] | 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, |
---|
[4993] | 654 | this->relCoordinate.z, |
---|
[4996] | 655 | this->getAbsDirV().x, |
---|
| 656 | this->getAbsDirV().y, |
---|
| 657 | this->getAbsDirV().z, |
---|
[4993] | 658 | this->parentingModeToChar(parentMode)); |
---|
[4574] | 659 | if (depth >= 2 || depth == 0) |
---|
| 660 | { |
---|
| 661 | tIterator<PNode>* iterator = this->children->getIterator(); |
---|
| 662 | //PNode* pn = this->children->enumerate (); |
---|
[5115] | 663 | PNode* pn = iterator->firstElement(); |
---|
[4574] | 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 | } |
---|
[3365] | 674 | } |
---|
[5010] | 675 | #include "color.h" |
---|
[3365] | 676 | |
---|
[4570] | 677 | /** |
---|
[4836] | 678 | displays the PNode at its position with its rotation as a cube. |
---|
[4570] | 679 | */ |
---|
[5008] | 680 | void PNode::debugDraw(unsigned int depth, float size, Vector color) const |
---|
[4570] | 681 | { |
---|
| 682 | glMatrixMode(GL_MODELVIEW); |
---|
| 683 | glPushMatrix(); |
---|
[5008] | 684 | glDisable(GL_LIGHTING); |
---|
[4570] | 685 | |
---|
| 686 | /* translate */ |
---|
| 687 | glTranslatef (this->getAbsCoor ().x, |
---|
| 688 | this->getAbsCoor ().y, |
---|
| 689 | this->getAbsCoor ().z); |
---|
| 690 | /* rotate */ |
---|
[4998] | 691 | // this->getAbsDir ().matrix (matrix); |
---|
| 692 | // glMultMatrixf((float*)matrix); |
---|
| 693 | |
---|
| 694 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
[5008] | 695 | glColor3f(color.x, color.y, color.z); |
---|
[4998] | 696 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[4570] | 697 | { |
---|
| 698 | glBegin(GL_LINE_STRIP); |
---|
[4995] | 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); |
---|
[4570] | 704 | glEnd(); |
---|
| 705 | glBegin(GL_LINE_STRIP); |
---|
[4995] | 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); |
---|
[4570] | 711 | glEnd(); |
---|
[4995] | 712 | |
---|
[4570] | 713 | glBegin(GL_LINES); |
---|
[4995] | 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); |
---|
[4570] | 722 | glEnd(); |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | glPopMatrix(); |
---|
[5012] | 726 | glEnable(GL_LIGHTING); |
---|
[5007] | 727 | if (depth >= 2 || depth == 0) |
---|
| 728 | { |
---|
[5115] | 729 | Vector childColor = Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0)); |
---|
| 730 | |
---|
[5111] | 731 | tIterator<PNode>* iterator = this->children->getIterator(); |
---|
[5115] | 732 | PNode* pn = iterator->firstElement(); |
---|
[5007] | 733 | while( pn != NULL) |
---|
| 734 | { |
---|
| 735 | if (depth == 0) |
---|
[5008] | 736 | pn->debugDraw(0, size, childColor); |
---|
[5007] | 737 | else |
---|
[5008] | 738 | pn->debugDraw(depth - 1, size, childColor); |
---|
[5007] | 739 | pn = iterator->nextElement(); |
---|
| 740 | } |
---|
| 741 | delete iterator; |
---|
| 742 | } |
---|
[4570] | 743 | } |
---|
[4993] | 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 | } |
---|