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