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