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