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