[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. |
---|
| 211 | */ |
---|
[3809] | 212 | void PNode::setAbsCoor (const Vector& absCoord) |
---|
[3675] | 213 | { |
---|
[4993] | 214 | if( likely(this->parentMode & PNODE_MOVEMENT)) |
---|
| 215 | { |
---|
| 216 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
| 217 | if (likely(this->parent != NULL)) |
---|
| 218 | this->relCoordinate = absCoord - parent->getAbsCoor (); |
---|
| 219 | else |
---|
| 220 | this->relCoordinate = absCoord; |
---|
| 221 | } |
---|
| 222 | if( this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
| 223 | { |
---|
| 224 | if (likely(this->parent != NULL)) |
---|
| 225 | this->relCoordinate = absCoord - parent->getAbsCoor (); |
---|
| 226 | else |
---|
| 227 | this->relCoordinate = absCoord; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | this->bRelCoorChanged = true; |
---|
| 231 | // this->absCoordinate = absCoord; |
---|
[3675] | 232 | } |
---|
| 233 | |
---|
| 234 | /** |
---|
[4836] | 235 | * @param x x-coordinate. |
---|
| 236 | * @param y y-coordinate. |
---|
| 237 | * @param z z-coordinate. |
---|
[4987] | 238 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
---|
[4610] | 239 | */ |
---|
| 240 | void PNode::setAbsCoor(float x, float y, float z) |
---|
| 241 | { |
---|
| 242 | this->setAbsCoor(Vector(x, y, z)); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | /** |
---|
[4836] | 246 | * shift coordinate (abs and rel) |
---|
| 247 | * @param shift shift vector |
---|
[3365] | 248 | |
---|
| 249 | this function shifts the current coordinates about the vector shift. this is |
---|
| 250 | usefull because from some place else you can: |
---|
| 251 | PNode* someNode = ...; |
---|
| 252 | Vector objectMovement = calculateShift(); |
---|
| 253 | someNode->shiftCoor(objectMovement); |
---|
| 254 | |
---|
| 255 | elsewhere you would have to: |
---|
| 256 | PNode* someNode = ...; |
---|
| 257 | Vector objectMovement = calculateShift(); |
---|
| 258 | Vector currentCoor = someNode->getRelCoor(); |
---|
| 259 | Vector newCoor = currentCoor + objectMovement; |
---|
| 260 | someNode->setRelCoor(newCoor); |
---|
[4570] | 261 | |
---|
[3365] | 262 | yea right... shorter... |
---|
[4987] | 263 | * |
---|
| 264 | * @todo this is ambiguous, from the outside one does not know it absCoor has been changed |
---|
| 265 | * this might lead to strange artefacts !! |
---|
[3365] | 266 | |
---|
| 267 | */ |
---|
[3809] | 268 | void PNode::shiftCoor (const Vector& shift) |
---|
[3683] | 269 | { |
---|
[4993] | 270 | this->relCoordinate += shift; |
---|
| 271 | this->bRelCoorChanged = true; |
---|
[3683] | 272 | } |
---|
| 273 | |
---|
| 274 | /** |
---|
[4836] | 275 | * set relative direction |
---|
| 276 | * @param relDir to its parent |
---|
[3365] | 277 | |
---|
| 278 | it is very importand, that you use this function, if you want to update the |
---|
| 279 | relDirection. If you don't use this, the PNode won't recognize, that something |
---|
| 280 | has changed and won't update the children Nodes. |
---|
| 281 | */ |
---|
[3810] | 282 | void PNode::setRelDir (const Quaternion& relDir) |
---|
[3675] | 283 | { |
---|
[4993] | 284 | this->relDirection = relDir; |
---|
[3675] | 285 | this->bRelCoorChanged = true; |
---|
| 286 | } |
---|
| 287 | |
---|
[3365] | 288 | /** |
---|
[4771] | 289 | * @see void PNode::setRelDir (const Quaternion& relDir) |
---|
| 290 | * @param x the x direction |
---|
| 291 | * @param y the y direction |
---|
| 292 | * @param z the z direction |
---|
| 293 | * |
---|
| 294 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
| 295 | */ |
---|
| 296 | void PNode::setRelDir (float x, float y, float z) |
---|
| 297 | { |
---|
| 298 | this->setRelDir(Quaternion(Vector(x,y,z), Vector(0,1,0))); |
---|
| 299 | } |
---|
| 300 | |
---|
[4990] | 301 | |
---|
[4771] | 302 | /** |
---|
[4990] | 303 | * sets the Relative Direction of this node to its parent in a Smoothed way |
---|
| 304 | * @param relDirSoft the direction to iterate to smoothely. |
---|
[4992] | 305 | * @param bias how fast to iterate to the new Direction |
---|
[4990] | 306 | */ |
---|
[4992] | 307 | void PNode::setRelDirSoft(const Quaternion& relDirSoft, float bias) |
---|
[4990] | 308 | { |
---|
| 309 | if (likely(this->toDirection == NULL)) |
---|
| 310 | this->toDirection = new Quaternion(); |
---|
| 311 | |
---|
| 312 | *this->toDirection = relDirSoft; |
---|
[4992] | 313 | this->bias = bias; |
---|
[4990] | 314 | } |
---|
| 315 | |
---|
| 316 | /** |
---|
| 317 | * @see void PNode::setRelDirSoft (const Quaternion& relDir) |
---|
| 318 | * @param x the x direction |
---|
| 319 | * @param y the y direction |
---|
| 320 | * @param z the z direction |
---|
| 321 | * |
---|
| 322 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
| 323 | */ |
---|
[4992] | 324 | void PNode::setRelDirSoft(float x, float y, float z, float bias) |
---|
[4990] | 325 | { |
---|
[4992] | 326 | this->setRelDirSoft(Quaternion(Vector(x,y,z), Vector(0,1,0)), bias); |
---|
[4990] | 327 | } |
---|
| 328 | |
---|
| 329 | /** |
---|
[4836] | 330 | * sets the absolute direction (0,0,1) |
---|
| 331 | * @param absDir absolute coordinates |
---|
[4993] | 332 | * |
---|
[4996] | 333 | * it is very importand, that you use this function, if you want to update the |
---|
| 334 | * absDirection. If you don't use this, the PNode won't recognize, that something |
---|
| 335 | * has changed and won't update the children Nodes. |
---|
[3365] | 336 | */ |
---|
[3810] | 337 | void PNode::setAbsDir (const Quaternion& absDir) |
---|
[3675] | 338 | { |
---|
[4996] | 339 | if (this->parent) |
---|
[4997] | 340 | this->relDirection = absDir / parent->getAbsDir(); |
---|
[4996] | 341 | else |
---|
| 342 | this->relDirection = absDir; |
---|
[4993] | 343 | |
---|
| 344 | this->bRelDirChanged = true; |
---|
| 345 | // this->absDirection = absDir; |
---|
[3675] | 346 | } |
---|
| 347 | |
---|
| 348 | /** |
---|
[4771] | 349 | * @see void PNode::setAbsDir (const Quaternion& relDir) |
---|
| 350 | * @param x the x direction |
---|
| 351 | * @param y the y direction |
---|
| 352 | * @param z the z direction |
---|
| 353 | * |
---|
| 354 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
| 355 | */ |
---|
| 356 | void PNode::setAbsDir (float x, float y, float z) |
---|
| 357 | { |
---|
| 358 | this->setAbsDir(Quaternion(Vector(x,y,z), Vector(0,1,0))); |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | /** |
---|
[4836] | 362 | * shift coordinate (abs and rel) |
---|
| 363 | * @param shift vector |
---|
[4993] | 364 | * @todo implement this |
---|
[3365] | 365 | */ |
---|
[3802] | 366 | void PNode::shiftDir (const Quaternion& shift) |
---|
| 367 | { |
---|
[4993] | 368 | this->relDirection = this->relDirection * shift; |
---|
[3802] | 369 | this->bRelDirChanged = true; |
---|
| 370 | } |
---|
[3365] | 371 | |
---|
[3683] | 372 | /** |
---|
[4836] | 373 | * adds a child and makes this node to a parent |
---|
| 374 | * @param pNode child reference |
---|
| 375 | * @param parentMode on which changes the child should also change ist state |
---|
[4993] | 376 | * |
---|
| 377 | * use this to add a child to this node. |
---|
[3365] | 378 | */ |
---|
[4993] | 379 | void PNode::addChild (PNode* child, int parentMode) |
---|
[3365] | 380 | { |
---|
[4993] | 381 | if( likely(child->parent != NULL)) |
---|
[3521] | 382 | { |
---|
[4992] | 383 | PRINTF(4)("PNode::addChild() - reparenting node: removing it and adding it again\n"); |
---|
[4993] | 384 | child->parent->children->remove(child); |
---|
[3521] | 385 | } |
---|
[4993] | 386 | child->parentMode = parentMode; |
---|
| 387 | child->parent = this; |
---|
| 388 | this->children->add(child); |
---|
| 389 | child->parentCoorChanged(); |
---|
[3365] | 390 | } |
---|
| 391 | |
---|
| 392 | /** |
---|
[4765] | 393 | * @see PNode::addChild(PNode* parent); |
---|
| 394 | * @param childName the name of the child to add to this PNode |
---|
| 395 | */ |
---|
| 396 | void PNode::addChild (const char* childName) |
---|
| 397 | { |
---|
| 398 | PNode* childNode = dynamic_cast<PNode*>(ClassList::getObject(childName, CL_PARENT_NODE)); |
---|
| 399 | if (childNode != NULL) |
---|
| 400 | this->addChild(childNode); |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | /** |
---|
[4836] | 404 | * removes a child from the node |
---|
| 405 | * @param pNode the child to remove from this pNode. |
---|
[4993] | 406 | * |
---|
| 407 | * Children from pNode will not be lost, they are referenced to NullPointer |
---|
[3365] | 408 | */ |
---|
[4993] | 409 | void PNode::removeChild (PNode* child) |
---|
[3365] | 410 | { |
---|
[4993] | 411 | child->remove(); |
---|
| 412 | this->children->remove(child); |
---|
| 413 | child->parent = NULL; |
---|
[3365] | 414 | } |
---|
| 415 | |
---|
| 416 | /** |
---|
[4836] | 417 | * remove this pnode from the tree and adds all following to NullParent |
---|
[3537] | 418 | |
---|
| 419 | this can be the case, if an entity in the world is been destroyed. |
---|
| 420 | */ |
---|
| 421 | void PNode::remove() |
---|
| 422 | { |
---|
[3662] | 423 | NullParent* nullParent = NullParent::getInstance(); |
---|
| 424 | |
---|
[3668] | 425 | tIterator<PNode>* iterator = this->children->getIterator(); |
---|
| 426 | PNode* pn = iterator->nextElement(); |
---|
[4570] | 427 | |
---|
| 428 | while( pn != NULL) |
---|
| 429 | { |
---|
[4444] | 430 | nullParent->addChild(pn, pn->getParentMode()); |
---|
[3668] | 431 | pn = iterator->nextElement(); |
---|
[3537] | 432 | } |
---|
[3668] | 433 | delete iterator; |
---|
[3669] | 434 | this->parent->children->remove(this); |
---|
[3537] | 435 | } |
---|
| 436 | |
---|
| 437 | /** |
---|
[4993] | 438 | * sets the parent of this PNode |
---|
[4836] | 439 | * @param parent the Parent to set |
---|
[3365] | 440 | */ |
---|
| 441 | void PNode::setParent (PNode* parent) |
---|
| 442 | { |
---|
[3511] | 443 | parent->addChild(this); |
---|
[3365] | 444 | } |
---|
| 445 | |
---|
[4761] | 446 | /** |
---|
| 447 | * @see PNode::setParent(PNode* parent); |
---|
| 448 | * @param parentName the name of the Parent to set to this PNode |
---|
| 449 | */ |
---|
| 450 | void PNode::setParent (const char* parentName) |
---|
| 451 | { |
---|
| 452 | PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE)); |
---|
| 453 | if (parentNode != NULL) |
---|
| 454 | parentNode->addChild(this); |
---|
| 455 | } |
---|
| 456 | |
---|
[4990] | 457 | /** |
---|
| 458 | * does the reparenting in a very smooth way |
---|
| 459 | * @param parentNode the new Node to connect this node to. |
---|
[4993] | 460 | * @param bias the speed to iterate to this new Positions |
---|
[4990] | 461 | */ |
---|
[4992] | 462 | void PNode::softReparent(PNode* parentNode, float bias) |
---|
[4987] | 463 | { |
---|
[4992] | 464 | if (this->parent == parentNode) |
---|
| 465 | return; |
---|
| 466 | |
---|
[4993] | 467 | if (likely(this->toCoordinate == NULL)) |
---|
[4989] | 468 | { |
---|
[4993] | 469 | this->toCoordinate = new Vector(); |
---|
| 470 | *this->toCoordinate = this->getRelCoor(); |
---|
[4989] | 471 | } |
---|
[4990] | 472 | if (likely(this->toDirection == NULL)) |
---|
| 473 | { |
---|
| 474 | this->toDirection = new Quaternion(); |
---|
| 475 | *this->toDirection = this->getRelDir(); |
---|
| 476 | } |
---|
[4992] | 477 | this->bias = bias; |
---|
[4987] | 478 | |
---|
| 479 | |
---|
[4990] | 480 | Vector tmpV = this->getAbsCoor(); |
---|
| 481 | Quaternion tmpQ = this->getAbsDir(); |
---|
[4987] | 482 | |
---|
| 483 | parentNode->addChild(this); |
---|
| 484 | |
---|
[4991] | 485 | /* if (this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
| 486 | this->setRelCoor(parent->getRelDir().apply(tmpV - parent->getAbsCoor())); |
---|
| 487 | else*/ |
---|
[4990] | 488 | this->setRelCoor(tmpV - parentNode->getAbsCoor()); |
---|
[4991] | 489 | |
---|
[4997] | 490 | this->setRelDir(tmpQ / parentNode->getAbsDir()); |
---|
[4987] | 491 | } |
---|
| 492 | |
---|
[4993] | 493 | /** |
---|
| 494 | * does the reparenting in a very smooth way |
---|
| 495 | * @param parentName the name of the Parent to reconnect to |
---|
| 496 | * @param bias the speed to iterate to this new Positions |
---|
| 497 | */ |
---|
[4992] | 498 | void PNode::softReparent(const char* parentName, float bias) |
---|
[4987] | 499 | { |
---|
| 500 | PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE)); |
---|
| 501 | if (parentNode != NULL) |
---|
[4992] | 502 | this->softReparent(parentNode, bias); |
---|
[4987] | 503 | } |
---|
| 504 | |
---|
[3365] | 505 | /** |
---|
[4836] | 506 | * sets the mode of this parent manually |
---|
[4765] | 507 | * @param parentMode a String representing this parentingMode |
---|
| 508 | */ |
---|
| 509 | void PNode::setParentMode (const char* parentingMode) |
---|
| 510 | { |
---|
[4993] | 511 | this->setParentMode(PNode::charToParentingMode(parentingMode)); |
---|
[4765] | 512 | } |
---|
[3537] | 513 | |
---|
[3365] | 514 | /** |
---|
[4836] | 515 | * updates the absCoordinate/absDirection |
---|
| 516 | * @param dt The time passed since the last update |
---|
[3365] | 517 | |
---|
| 518 | this is used to go through the parent-tree to update all the absolute coordinates |
---|
[4570] | 519 | and directions. this update should be done by the engine, so you don't have to |
---|
[3365] | 520 | worry, normaly... |
---|
| 521 | */ |
---|
[3644] | 522 | void PNode::update (float dt) |
---|
[3365] | 523 | { |
---|
[4440] | 524 | if( likely(this->parent != NULL)) |
---|
| 525 | { |
---|
[4987] | 526 | // movement for nodes with smoothMove enabled |
---|
[4993] | 527 | if (unlikely(this->toCoordinate != NULL)) |
---|
[4987] | 528 | { |
---|
[4993] | 529 | Vector moveVect = (*this->toCoordinate - this->getRelCoor()) *dt*bias; |
---|
[4987] | 530 | |
---|
| 531 | if (likely(moveVect.len() >= .001)) |
---|
| 532 | { |
---|
| 533 | this->shiftCoor(moveVect); |
---|
| 534 | } |
---|
| 535 | else |
---|
| 536 | { |
---|
[4993] | 537 | delete this->toCoordinate; |
---|
| 538 | this->toCoordinate = NULL; |
---|
[4988] | 539 | PRINTF(5)("SmoothMove of %s finished\n", this->getName()); |
---|
[4987] | 540 | } |
---|
| 541 | } |
---|
[4990] | 542 | if (unlikely(this->toDirection != NULL)) |
---|
| 543 | { |
---|
[4998] | 544 | Quaternion rotQuat = Quaternion(.1, Vector(0,1,0));// (*this->toDirection / this->relDirection); |
---|
| 545 | // printf("1: "); |
---|
| 546 | // this->relDirection.debug(); |
---|
| 547 | // printf("2: "); |
---|
| 548 | // this->toDirection->debug(); |
---|
| 549 | // printf("3: "); |
---|
| 550 | // rotQuat.debug(); |
---|
| 551 | |
---|
| 552 | if (true)//likely(rotQuat.len() >= .001)) |
---|
[4990] | 553 | { |
---|
| 554 | this->shiftDir(rotQuat); |
---|
| 555 | } |
---|
[4998] | 556 | else |
---|
[4990] | 557 | { |
---|
[4993] | 558 | delete this->toCoordinate; |
---|
| 559 | this->toCoordinate = NULL; |
---|
[4990] | 560 | PRINTF(5)("SmoothMove of %s finished\n", this->getName()); |
---|
[4998] | 561 | } |
---|
[4990] | 562 | } |
---|
| 563 | |
---|
[4993] | 564 | // MAIN UPDATE ///////////////////////////////////// |
---|
[4440] | 565 | this->lastAbsCoordinate = this->absCoordinate; |
---|
[4145] | 566 | |
---|
[4987] | 567 | PRINTF(5)("PNode::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
[3800] | 568 | |
---|
[4993] | 569 | if(likely(this->parentMode & PNODE_MOVEMENT)) |
---|
| 570 | { |
---|
| 571 | /* update the current absCoordinate */ |
---|
| 572 | this->absCoordinate = this->parent->getAbsCoor() + this->relCoordinate; |
---|
| 573 | } |
---|
[4570] | 574 | |
---|
[4993] | 575 | if( this->parentMode & PNODE_LOCAL_ROTATE && this->bRelDirChanged) |
---|
| 576 | { |
---|
| 577 | /* update the current absDirection - remember * means rotation around sth.*/ |
---|
| 578 | this->absDirection = parent->getAbsDir() * this->relDirection; |
---|
| 579 | } |
---|
[4570] | 580 | |
---|
[4444] | 581 | if( this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
[4993] | 582 | { |
---|
| 583 | /* update the current absCoordinate */ |
---|
| 584 | this->absCoordinate = this->parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate); |
---|
| 585 | } |
---|
| 586 | ///////////////////////////////////////////////// |
---|
| 587 | } |
---|
[4440] | 588 | else |
---|
| 589 | { |
---|
| 590 | PRINTF(4)("NullParent::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
[4993] | 591 | if (this->bRelCoorChanged) |
---|
| 592 | this->absCoordinate = this->relCoordinate; |
---|
| 593 | if (this->bRelDirChanged) |
---|
| 594 | this->absDirection = this->getAbsDir () * this->relDirection; |
---|
| 595 | } |
---|
[3365] | 596 | |
---|
[4993] | 597 | if(this->children->getSize() > 0) |
---|
| 598 | { |
---|
[4440] | 599 | tIterator<PNode>* iterator = this->children->getIterator(); |
---|
| 600 | PNode* pn = iterator->nextElement(); |
---|
[4570] | 601 | while( pn != NULL) |
---|
[4574] | 602 | { |
---|
| 603 | /* if this node has changed, make sure, that all children are updated also */ |
---|
[4993] | 604 | if( likely(this->bRelCoorChanged)) |
---|
| 605 | pn->parentCoorChanged (); |
---|
| 606 | if( likely(this->bRelDirChanged)) |
---|
| 607 | pn->parentDirChanged (); |
---|
| 608 | |
---|
| 609 | pn->update(dt); |
---|
| 610 | //pn = this->children->nextElement(); |
---|
| 611 | pn = iterator->nextElement(); |
---|
| 612 | } |
---|
[4574] | 613 | delete iterator; |
---|
[4440] | 614 | } |
---|
[4993] | 615 | this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt; |
---|
| 616 | this->bRelCoorChanged = false; |
---|
| 617 | this->bRelDirChanged = false; |
---|
[3365] | 618 | } |
---|
| 619 | |
---|
[3450] | 620 | /** |
---|
[4836] | 621 | * displays some information about this pNode |
---|
| 622 | * @param depth The deph into which to debug the children of this PNode to. |
---|
[4574] | 623 | (0: all children will be debugged, 1: only this PNode, 2: this and direct children...) |
---|
[4836] | 624 | * @param level The n-th level of the Node we draw (this is internal and only for nice output) |
---|
[3450] | 625 | */ |
---|
[4574] | 626 | void PNode::debug(unsigned int depth, unsigned int level) const |
---|
[3365] | 627 | { |
---|
[4574] | 628 | for (unsigned int i = 0; i < level; i++) |
---|
[4575] | 629 | PRINT(0)(" |"); |
---|
[4574] | 630 | if (this->children->getSize() > 0) |
---|
[4575] | 631 | PRINT(0)(" +"); |
---|
| 632 | else |
---|
| 633 | PRINT(0)(" -"); |
---|
[4996] | 634 | 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] | 635 | this->getClassName(), |
---|
| 636 | this->getName(), |
---|
| 637 | this->absCoordinate.x, |
---|
| 638 | this->absCoordinate.y, |
---|
| 639 | this->absCoordinate.z, |
---|
| 640 | this->relCoordinate.x, |
---|
| 641 | this->relCoordinate.y, |
---|
[4993] | 642 | this->relCoordinate.z, |
---|
[4996] | 643 | this->getAbsDirV().x, |
---|
| 644 | this->getAbsDirV().y, |
---|
| 645 | this->getAbsDirV().z, |
---|
[4993] | 646 | this->parentingModeToChar(parentMode)); |
---|
[4574] | 647 | if (depth >= 2 || depth == 0) |
---|
| 648 | { |
---|
| 649 | tIterator<PNode>* iterator = this->children->getIterator(); |
---|
| 650 | //PNode* pn = this->children->enumerate (); |
---|
| 651 | PNode* pn = iterator->nextElement(); |
---|
| 652 | while( pn != NULL) |
---|
| 653 | { |
---|
| 654 | if (depth == 0) |
---|
| 655 | pn->debug(0, level + 1); |
---|
| 656 | else |
---|
| 657 | pn->debug(depth - 1, level +1); |
---|
| 658 | pn = iterator->nextElement(); |
---|
| 659 | } |
---|
| 660 | delete iterator; |
---|
| 661 | } |
---|
[3365] | 662 | } |
---|
| 663 | |
---|
[4570] | 664 | /** |
---|
[4836] | 665 | displays the PNode at its position with its rotation as a cube. |
---|
[4570] | 666 | */ |
---|
| 667 | void PNode::debugDraw(float size) const |
---|
| 668 | { |
---|
| 669 | glMatrixMode(GL_MODELVIEW); |
---|
| 670 | glPushMatrix(); |
---|
| 671 | |
---|
| 672 | /* translate */ |
---|
| 673 | glTranslatef (this->getAbsCoor ().x, |
---|
| 674 | this->getAbsCoor ().y, |
---|
| 675 | this->getAbsCoor ().z); |
---|
| 676 | /* rotate */ |
---|
[4998] | 677 | // this->getAbsDir ().matrix (matrix); |
---|
| 678 | // glMultMatrixf((float*)matrix); |
---|
| 679 | |
---|
| 680 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 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(); |
---|
| 711 | } |
---|
[4993] | 712 | |
---|
| 713 | |
---|
| 714 | |
---|
| 715 | ///////////////////// |
---|
| 716 | // HELPER_FUCTIONS // |
---|
| 717 | ///////////////////// |
---|
| 718 | |
---|
| 719 | /** |
---|
| 720 | * converts a parentingMode into a string that is the name of it |
---|
| 721 | * @param parentingMode the ParentingMode to convert |
---|
| 722 | * @return the converted string |
---|
| 723 | */ |
---|
| 724 | const char* PNode::parentingModeToChar(int parentingMode) |
---|
| 725 | { |
---|
| 726 | if (parentingMode == PNODE_LOCAL_ROTATE) |
---|
| 727 | return "local-rotate"; |
---|
| 728 | else if (parentingMode == PNODE_ROTATE_MOVEMENT) |
---|
| 729 | return "rotate-movement"; |
---|
| 730 | else if (parentingMode == PNODE_MOVEMENT) |
---|
| 731 | return "movement"; |
---|
| 732 | else if (parentingMode == PNODE_ALL) |
---|
| 733 | return "all"; |
---|
| 734 | else if (parentingMode == PNODE_ROTATE_AND_MOVE) |
---|
| 735 | return "rotate-and-move"; |
---|
| 736 | } |
---|
| 737 | |
---|
| 738 | /** |
---|
| 739 | * converts a parenting-mode-string into a int |
---|
| 740 | * @param parentingMode the string naming the parentingMode |
---|
| 741 | * @return the int corresponding to the named parentingMode |
---|
| 742 | */ |
---|
| 743 | PARENT_MODE PNode::charToParentingMode(const char* parentingMode) |
---|
| 744 | { |
---|
| 745 | if (!strcmp(parentingMode, "local-rotate")) |
---|
| 746 | return (PNODE_LOCAL_ROTATE); |
---|
| 747 | else if (!strcmp(parentingMode, "rotate-movement")) |
---|
| 748 | return (PNODE_ROTATE_MOVEMENT); |
---|
| 749 | else if (!strcmp(parentingMode, "movement")) |
---|
| 750 | return (PNODE_MOVEMENT); |
---|
| 751 | else if (!strcmp(parentingMode, "all")) |
---|
| 752 | return (PNODE_ALL); |
---|
| 753 | else if (!strcmp(parentingMode, "rotate-and-move")) |
---|
| 754 | return (PNODE_ROTATE_AND_MOVE); |
---|
| 755 | } |
---|