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