1 | /* |
---|
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 |
---|
13 | co-programmer: Benjamin Grauer |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PNODE |
---|
17 | |
---|
18 | #include "p_node.h" |
---|
19 | |
---|
20 | #include "load_param.h" |
---|
21 | #include "class_list.h" |
---|
22 | |
---|
23 | #include <algorithm> |
---|
24 | #include "compiler.h" |
---|
25 | #include "debug.h" |
---|
26 | |
---|
27 | #include "glincl.h" |
---|
28 | #include "color.h" |
---|
29 | |
---|
30 | #include "synchronizeable.h" |
---|
31 | |
---|
32 | #include "shell_command.h" |
---|
33 | SHELL_COMMAND(debugNode, PNode, debugNodeSC); |
---|
34 | |
---|
35 | using namespace std; |
---|
36 | |
---|
37 | |
---|
38 | /** |
---|
39 | * @brief standard constructor |
---|
40 | * @param parent the Parent of this Node. __NULL__ if __No Parent__ requested, PNode::getNullParent(), if connected to NullParent directly (default) |
---|
41 | * @param nodeFlags all flags to set. THIS_WILL_OVERWRITE Default_Values. |
---|
42 | */ |
---|
43 | PNode::PNode (PNode* parent, long nodeFlags) |
---|
44 | : Synchronizeable(), BaseObject() |
---|
45 | { |
---|
46 | this->setClassID(CL_PARENT_NODE, "PNode"); |
---|
47 | |
---|
48 | this->bRelCoorChanged = true; |
---|
49 | this->bRelDirChanged = true; |
---|
50 | this->parent = NULL; |
---|
51 | this->parentMode = nodeFlags; |
---|
52 | this->bActive = true; |
---|
53 | |
---|
54 | // smooth-movers |
---|
55 | this->toCoordinate = NULL; |
---|
56 | this->toDirection = NULL; |
---|
57 | this->bias = 1.0; |
---|
58 | |
---|
59 | if (parent != NULL) |
---|
60 | parent->addChild(this); |
---|
61 | } |
---|
62 | |
---|
63 | // NullParent Reference |
---|
64 | PNode* PNode::nullParent = NULL; |
---|
65 | |
---|
66 | /** |
---|
67 | * @brief standard deconstructor |
---|
68 | * |
---|
69 | * There are two general ways to delete a PNode |
---|
70 | * 1. delete instance; |
---|
71 | * -> result |
---|
72 | * delete this Node and all its children and children's children... |
---|
73 | * (danger if you still need the children's instance somewhere else!!) |
---|
74 | * |
---|
75 | * 2. instance->removeNode(); delete instance; |
---|
76 | * -> result: |
---|
77 | * moves its children to the NullParent |
---|
78 | * then deletes the Element. |
---|
79 | */ |
---|
80 | PNode::~PNode () |
---|
81 | { |
---|
82 | // remove the Node, delete it's children (if required). |
---|
83 | std::list<PNode*>::iterator tmp = this->children.begin(); |
---|
84 | std::list<PNode*>::iterator deleteNode; |
---|
85 | while(!this->children.empty()) |
---|
86 | while (tmp != this->children.end()) |
---|
87 | { |
---|
88 | deleteNode = tmp; |
---|
89 | tmp++; |
---|
90 | // printf("TEST::%s(%s) %s\n", (*deleteNode)->getName(), (*deleteNode)->getClassName(), this->getName()); |
---|
91 | if ((this->parentMode & PNODE_PROHIBIT_CHILD_DELETE) || |
---|
92 | ((*deleteNode)->parentMode & PNODE_PROHIBIT_DELETE_WITH_PARENT)) |
---|
93 | { |
---|
94 | if (this == PNode::nullParent && (*deleteNode)->parentMode & PNODE_REPARENT_TO_NULL) |
---|
95 | delete (*deleteNode); |
---|
96 | else |
---|
97 | (*deleteNode)->reparent(); |
---|
98 | } |
---|
99 | else |
---|
100 | delete (*deleteNode); |
---|
101 | } |
---|
102 | |
---|
103 | if (this->parent != NULL) |
---|
104 | { |
---|
105 | this->parent->eraseChild(this); |
---|
106 | this->parent = NULL; |
---|
107 | } |
---|
108 | |
---|
109 | // remove all other allocated memory. |
---|
110 | if (this->toCoordinate != NULL) |
---|
111 | delete this->toCoordinate; |
---|
112 | if (this->toDirection != NULL) |
---|
113 | delete this->toDirection; |
---|
114 | |
---|
115 | if (this == PNode::nullParent) |
---|
116 | PNode::nullParent = NULL; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | /** |
---|
121 | * @brief loads parameters of the PNode |
---|
122 | * @param root the XML-element to load the properties of |
---|
123 | */ |
---|
124 | void PNode::loadParams(const TiXmlElement* root) |
---|
125 | { |
---|
126 | BaseObject::loadParams(root); |
---|
127 | |
---|
128 | LoadParam(root, "rel-coor", this, PNode, setRelCoor) |
---|
129 | .describe("Sets The relative position of the Node to its parent."); |
---|
130 | |
---|
131 | LoadParam(root, "abs-coor", this, PNode, setAbsCoor) |
---|
132 | .describe("Sets The absolute Position of the Node."); |
---|
133 | |
---|
134 | LoadParam(root, "rel-dir", this, PNode, setRelDir) |
---|
135 | .describe("Sets The relative rotation of the Node to its parent."); |
---|
136 | |
---|
137 | LoadParam(root, "abs-dir", this, PNode, setAbsDir) |
---|
138 | .describe("Sets The absolute rotation of the Node."); |
---|
139 | |
---|
140 | LoadParam(root, "parent", this, PNode, setParent) |
---|
141 | .describe("the Name of the Parent to set for this PNode"); |
---|
142 | |
---|
143 | LoadParam(root, "parent-mode", this, PNode, setParentMode) |
---|
144 | .describe("the mode to connect this node to its parent ()"); |
---|
145 | |
---|
146 | // cycling properties |
---|
147 | if (root != NULL) |
---|
148 | { |
---|
149 | LOAD_PARAM_START_CYCLE(root, element); |
---|
150 | { |
---|
151 | LoadParam_CYCLE(element, "child", this, PNode, addChild) |
---|
152 | .describe("adds a new Child to the current Node."); |
---|
153 | |
---|
154 | } |
---|
155 | LOAD_PARAM_END_CYCLE(element); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | /** |
---|
161 | * init the pnode to a well definied state |
---|
162 | * |
---|
163 | * this function actualy only updates the PNode tree |
---|
164 | */ |
---|
165 | void PNode::init() |
---|
166 | { |
---|
167 | /* just update all aboslute positions via timestep 0.001ms */ |
---|
168 | this->updateNode(0.001f); |
---|
169 | this->updateNode(0.001f); |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | /** |
---|
174 | * @brief set relative coordinates |
---|
175 | * @param relCoord relative coordinates to its parent |
---|
176 | * |
---|
177 | * |
---|
178 | * it is very importand, that you use this function, if you want to update the |
---|
179 | * relCoordinates. If you don't use this, the PNode won't recognize, that something |
---|
180 | * has changed and won't update the children Nodes. |
---|
181 | */ |
---|
182 | void PNode::setRelCoor (const Vector& relCoord) |
---|
183 | { |
---|
184 | if (this->toCoordinate!= NULL) |
---|
185 | { |
---|
186 | delete this->toCoordinate; |
---|
187 | this->toCoordinate = NULL; |
---|
188 | } |
---|
189 | |
---|
190 | this->relCoordinate = relCoord; |
---|
191 | this->bRelCoorChanged = true; |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * @brief set relative coordinates |
---|
196 | * @param x x-relative coordinates to its parent |
---|
197 | * @param y y-relative coordinates to its parent |
---|
198 | * @param z z-relative coordinates to its parent |
---|
199 | * @see void PNode::setRelCoor (const Vector& relCoord) |
---|
200 | */ |
---|
201 | void PNode::setRelCoor (float x, float y, float z) |
---|
202 | { |
---|
203 | this->setRelCoor(Vector(x, y, z)); |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | * @brief sets a new relative position smoothely |
---|
208 | * @param relCoordSoft the new Position to iterate to |
---|
209 | * @param bias how fast to iterate to this position |
---|
210 | */ |
---|
211 | void PNode::setRelCoorSoft(const Vector& relCoordSoft, float bias) |
---|
212 | { |
---|
213 | if (likely(this->toCoordinate == NULL)) |
---|
214 | this->toCoordinate = new Vector(); |
---|
215 | |
---|
216 | *this->toCoordinate = relCoordSoft; |
---|
217 | this->bias = bias; |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | /** |
---|
222 | * @brief set relative coordinates smoothely |
---|
223 | * @param x x-relative coordinates to its parent |
---|
224 | * @param y y-relative coordinates to its parent |
---|
225 | * @param z z-relative coordinates to its parent |
---|
226 | * @see void PNode::setRelCoorSoft (const Vector&, float) |
---|
227 | */ |
---|
228 | void PNode::setRelCoorSoft (float x, float y, float z, float bias) |
---|
229 | { |
---|
230 | this->setRelCoorSoft(Vector(x, y, z), bias); |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | /** |
---|
235 | * @param absCoord set absolute coordinate |
---|
236 | */ |
---|
237 | void PNode::setAbsCoor (const Vector& absCoord) |
---|
238 | { |
---|
239 | if (this->toCoordinate!= NULL) |
---|
240 | { |
---|
241 | delete this->toCoordinate; |
---|
242 | this->toCoordinate = NULL; |
---|
243 | } |
---|
244 | |
---|
245 | if( likely(this->parentMode & PNODE_MOVEMENT)) |
---|
246 | { |
---|
247 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
248 | if (likely(this->parent != NULL)) |
---|
249 | this->relCoordinate = absCoord - parent->getAbsCoor (); |
---|
250 | else |
---|
251 | this->relCoordinate = absCoord; |
---|
252 | } |
---|
253 | if( this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
254 | { |
---|
255 | if (likely(this->parent != NULL)) |
---|
256 | this->relCoordinate = absCoord - parent->getAbsCoor (); |
---|
257 | else |
---|
258 | this->relCoordinate = absCoord; |
---|
259 | } |
---|
260 | |
---|
261 | this->bRelCoorChanged = true; |
---|
262 | // this->absCoordinate = absCoord; |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | /** |
---|
267 | * @param x x-coordinate. |
---|
268 | * @param y y-coordinate. |
---|
269 | * @param z z-coordinate. |
---|
270 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
---|
271 | */ |
---|
272 | void PNode::setAbsCoor(float x, float y, float z) |
---|
273 | { |
---|
274 | this->setAbsCoor(Vector(x, y, z)); |
---|
275 | } |
---|
276 | |
---|
277 | |
---|
278 | /** |
---|
279 | * @param absCoord set absolute coordinate |
---|
280 | * @todo check off |
---|
281 | */ |
---|
282 | void PNode::setAbsCoorSoft (const Vector& absCoordSoft, float bias) |
---|
283 | { |
---|
284 | if (this->toCoordinate == NULL) |
---|
285 | this->toCoordinate = new Vector; |
---|
286 | |
---|
287 | if( likely(this->parentMode & PNODE_MOVEMENT)) |
---|
288 | { |
---|
289 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
290 | if (likely(this->parent != NULL)) |
---|
291 | *this->toCoordinate = absCoordSoft - parent->getAbsCoor (); |
---|
292 | else |
---|
293 | *this->toCoordinate = absCoordSoft; |
---|
294 | } |
---|
295 | if( this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
296 | { |
---|
297 | if (likely(this->parent != NULL)) |
---|
298 | *this->toCoordinate = absCoordSoft - parent->getAbsCoor (); |
---|
299 | else |
---|
300 | *this->toCoordinate = absCoordSoft; |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | /** |
---|
306 | * @brief shift coordinate relative |
---|
307 | * @param shift shift vector |
---|
308 | * |
---|
309 | * this function shifts the current coordinates about the vector shift. this is |
---|
310 | * usefull because from some place else you can: |
---|
311 | * PNode* someNode = ...; |
---|
312 | * Vector objectMovement = calculateShift(); |
---|
313 | * someNode->shiftCoor(objectMovement); |
---|
314 | * |
---|
315 | * this is the internal method of: |
---|
316 | * PNode* someNode = ...; |
---|
317 | * Vector objectMovement = calculateShift(); |
---|
318 | * Vector currentCoor = someNode->getRelCoor(); |
---|
319 | * Vector newCoor = currentCoor + objectMovement; |
---|
320 | * someNode->setRelCoor(newCoor); |
---|
321 | * |
---|
322 | */ |
---|
323 | void PNode::shiftCoor (const Vector& shift) |
---|
324 | { |
---|
325 | this->relCoordinate += shift; |
---|
326 | this->bRelCoorChanged = true; |
---|
327 | } |
---|
328 | |
---|
329 | |
---|
330 | /** |
---|
331 | * @brief set relative direction |
---|
332 | * @param relDir to its parent |
---|
333 | */ |
---|
334 | void PNode::setRelDir (const Quaternion& relDir) |
---|
335 | { |
---|
336 | if (this->toDirection!= NULL) |
---|
337 | { |
---|
338 | delete this->toDirection; |
---|
339 | this->toDirection = NULL; |
---|
340 | } |
---|
341 | this->relDirection = relDir; |
---|
342 | |
---|
343 | this->bRelCoorChanged = true; |
---|
344 | } |
---|
345 | |
---|
346 | |
---|
347 | /** |
---|
348 | * @see void PNode::setRelDir (const Quaternion& relDir) |
---|
349 | * @param x the x direction |
---|
350 | * @param y the y direction |
---|
351 | * @param z the z direction |
---|
352 | * |
---|
353 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
354 | */ |
---|
355 | void PNode::setRelDir (float angle, float x, float y, float z) |
---|
356 | { |
---|
357 | this->setRelDir(Quaternion(angle, Vector(x,y,z))); |
---|
358 | } |
---|
359 | |
---|
360 | |
---|
361 | /** |
---|
362 | * @brief sets the Relative Direction of this node to its parent in a Smoothed way |
---|
363 | * @param relDirSoft the direction to iterate to smoothely. |
---|
364 | * @param bias how fast to iterate to the new Direction |
---|
365 | */ |
---|
366 | void PNode::setRelDirSoft(const Quaternion& relDirSoft, float bias) |
---|
367 | { |
---|
368 | if (likely(this->toDirection == NULL)) |
---|
369 | this->toDirection = new Quaternion(); |
---|
370 | |
---|
371 | *this->toDirection = relDirSoft; |
---|
372 | this->bias = bias; |
---|
373 | this->bRelDirChanged = true; |
---|
374 | } |
---|
375 | |
---|
376 | |
---|
377 | /** |
---|
378 | * @see void PNode::setRelDirSoft (const Quaternion& relDir) |
---|
379 | * @param x the x direction |
---|
380 | * @param y the y direction |
---|
381 | * @param z the z direction |
---|
382 | * |
---|
383 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
384 | */ |
---|
385 | void PNode::setRelDirSoft(float angle, float x, float y, float z, float bias) |
---|
386 | { |
---|
387 | this->setRelDirSoft(Quaternion(angle, Vector(x,y,z)), bias); |
---|
388 | } |
---|
389 | |
---|
390 | |
---|
391 | /** |
---|
392 | * @brief sets the absolute direction |
---|
393 | * @param absDir absolute coordinates |
---|
394 | */ |
---|
395 | void PNode::setAbsDir (const Quaternion& absDir) |
---|
396 | { |
---|
397 | if (this->toDirection!= NULL) |
---|
398 | { |
---|
399 | delete this->toDirection; |
---|
400 | this->toDirection = NULL; |
---|
401 | } |
---|
402 | |
---|
403 | if (likely(this->parent != NULL)) |
---|
404 | this->relDirection = absDir / this->parent->getAbsDir(); |
---|
405 | else |
---|
406 | this->relDirection = absDir; |
---|
407 | |
---|
408 | this->bRelDirChanged = true; |
---|
409 | } |
---|
410 | |
---|
411 | |
---|
412 | /** |
---|
413 | * @see void PNode::setAbsDir (const Quaternion& relDir) |
---|
414 | * @param x the x direction |
---|
415 | * @param y the y direction |
---|
416 | * @param z the z direction |
---|
417 | * |
---|
418 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
419 | */ |
---|
420 | void PNode::setAbsDir (float angle, float x, float y, float z) |
---|
421 | { |
---|
422 | this->setAbsDir(Quaternion(angle, Vector(x,y,z))); |
---|
423 | } |
---|
424 | |
---|
425 | |
---|
426 | /** |
---|
427 | * @brief sets the absolute direction |
---|
428 | * @param absDir absolute coordinates |
---|
429 | * @param bias how fast to iterator to the new Position |
---|
430 | */ |
---|
431 | void PNode::setAbsDirSoft (const Quaternion& absDirSoft, float bias) |
---|
432 | { |
---|
433 | if (this->toDirection == NULL) |
---|
434 | this->toDirection = new Quaternion(); |
---|
435 | |
---|
436 | if (likely(this->parent != NULL)) |
---|
437 | *this->toDirection = absDirSoft / this->parent->getAbsDir(); |
---|
438 | else |
---|
439 | *this->toDirection = absDirSoft; |
---|
440 | |
---|
441 | this->bias = bias; |
---|
442 | this->bRelDirChanged = true; |
---|
443 | } |
---|
444 | |
---|
445 | |
---|
446 | /** |
---|
447 | * @see void PNode::setAbsDir (const Quaternion& relDir) |
---|
448 | * @param x the x direction |
---|
449 | * @param y the y direction |
---|
450 | * @param z the z direction |
---|
451 | * |
---|
452 | * main difference is, that here you give a directional vector, that will be translated into a Quaternion |
---|
453 | */ |
---|
454 | void PNode::setAbsDirSoft (float angle, float x, float y, float z, float bias) |
---|
455 | { |
---|
456 | this->setAbsDirSoft(Quaternion(angle, Vector(x,y,z)), bias); |
---|
457 | } |
---|
458 | |
---|
459 | |
---|
460 | /** |
---|
461 | * @brief shift Direction |
---|
462 | * @param shift the direction around which to shift. |
---|
463 | */ |
---|
464 | void PNode::shiftDir (const Quaternion& shift) |
---|
465 | { |
---|
466 | this->relDirection = this->relDirection * shift; |
---|
467 | this->bRelDirChanged = true; |
---|
468 | } |
---|
469 | |
---|
470 | |
---|
471 | /** |
---|
472 | * @brief adds a child and makes this node to a parent |
---|
473 | * @param child child reference |
---|
474 | * use this to add a child to this node. |
---|
475 | */ |
---|
476 | void PNode::addChild (PNode* child) |
---|
477 | { |
---|
478 | if( likely(child->parent != NULL)) |
---|
479 | child->parent->eraseChild(child); |
---|
480 | if (this->checkIntegrity(child)) |
---|
481 | { |
---|
482 | child->parent = this; |
---|
483 | if (unlikely(this != NULL)) |
---|
484 | this->children.push_back(child); |
---|
485 | child->parentCoorChanged(); |
---|
486 | |
---|
487 | // if(this->getUniqueID() == NET_UID_UNASSIGNED) |
---|
488 | // { |
---|
489 | // PRINTF(1)("Adding to an UNASSIGNED PNode - looking for next assigned Node\n"); |
---|
490 | // PNode* node = this->seekNextAssignedPNode(this); |
---|
491 | // if( node == NULL) |
---|
492 | // PRINTF(1)(" Got NULL - Is this the NULLParent - uid %i\n", this->getUniqueID()); |
---|
493 | // else |
---|
494 | // PRINTF(1)(" Found next assigned node: %i\n", node->getUniqueID()); |
---|
495 | // } |
---|
496 | } |
---|
497 | else |
---|
498 | { |
---|
499 | PRINTF(1)("Tried to reparent to own child '%s::%s' to '%s::%s'.\n", |
---|
500 | this->getClassName(), this->getName(), child->getClassName(), child->getName()); |
---|
501 | child->parent = NULL; |
---|
502 | child->parentCoorChanged(); |
---|
503 | } |
---|
504 | } |
---|
505 | |
---|
506 | |
---|
507 | PNode* PNode::seekNextAssignedPNode(PNode* node) const |
---|
508 | { |
---|
509 | PNode* tmpNode = node->parent; |
---|
510 | printf("entering seek PNode loop for name: %s, uid: %i\n", node->getName(), node->getUniqueID()); |
---|
511 | if(tmpNode) |
---|
512 | printf(" @node name: %s, uid: %d\n", tmpNode->getName(), tmpNode->getUniqueID()); |
---|
513 | while( tmpNode != NULL && tmpNode->getUniqueID() == NET_UID_UNASSIGNED) |
---|
514 | { |
---|
515 | printf(" @node name: %s, uid: %d\n", tmpNode->getName(), tmpNode->getUniqueID()); |
---|
516 | tmpNode = tmpNode->parent; |
---|
517 | } |
---|
518 | printf("leaving PNode loop\n\n"); |
---|
519 | |
---|
520 | return tmpNode; |
---|
521 | } |
---|
522 | |
---|
523 | |
---|
524 | /** |
---|
525 | * @see PNode::addChild(PNode* child); |
---|
526 | * @param childName the name of the child to add to this PNode |
---|
527 | */ |
---|
528 | void PNode::addChild (const char* childName) |
---|
529 | { |
---|
530 | PNode* childNode = dynamic_cast<PNode*>(ClassList::getObject(childName, CL_PARENT_NODE)); |
---|
531 | // PRINTF(0)("Adding the Child: %s to: %s\n", childName, this->getName()); |
---|
532 | // assert( childNode != NULL ); |
---|
533 | if (childNode != NULL) |
---|
534 | { |
---|
535 | this->addChild(childNode); |
---|
536 | } |
---|
537 | } |
---|
538 | |
---|
539 | |
---|
540 | /** |
---|
541 | * @brief removes a child from the node |
---|
542 | * @param child the child to remove from this pNode. |
---|
543 | * |
---|
544 | * Children from pNode will not be lost, they are Reparented by the rules of the ParentMode |
---|
545 | */ |
---|
546 | void PNode::removeChild (PNode* child) |
---|
547 | { |
---|
548 | if (child != NULL) |
---|
549 | child->removeNode(); |
---|
550 | } |
---|
551 | |
---|
552 | |
---|
553 | /** |
---|
554 | * !! PRIVATE FUNCTION |
---|
555 | * @brief reparents a node (happens on Parents Node delete or remove and Flags are set.) |
---|
556 | */ |
---|
557 | void PNode::reparent() |
---|
558 | { |
---|
559 | if (this->parentMode & PNODE_REPARENT_TO_NULL) |
---|
560 | this->setParent((PNode*)NULL); |
---|
561 | else if (this->parentMode & PNODE_REPARENT_TO_PARENTS_PARENT && this->parent != NULL) |
---|
562 | this->setParent(this->parent->getParent()); |
---|
563 | else |
---|
564 | this->setParent(PNode::getNullParent()); |
---|
565 | } |
---|
566 | |
---|
567 | /** |
---|
568 | * ereases child from the nodes children |
---|
569 | * @param chuld the child to remove |
---|
570 | */ |
---|
571 | void PNode::eraseChild(PNode* child) |
---|
572 | { |
---|
573 | std::list<PNode*>::iterator childRemover = std::find(this->children.begin(), this->children.end(), child); |
---|
574 | if(childRemover != this->children.end()) |
---|
575 | this->children.erase(childRemover); |
---|
576 | } |
---|
577 | |
---|
578 | |
---|
579 | /** |
---|
580 | * @brief remove this pnode from the tree and adds all following to NullParent |
---|
581 | * |
---|
582 | * this can be the case, if an entity in the world is being destroyed. |
---|
583 | */ |
---|
584 | void PNode::removeNode() |
---|
585 | { |
---|
586 | list<PNode*>::iterator child = this->children.begin(); |
---|
587 | list<PNode*>::iterator reparenter; |
---|
588 | while (child != this->children.end()) |
---|
589 | { |
---|
590 | reparenter = child; |
---|
591 | child++; |
---|
592 | if (this->parentMode & PNODE_REPARENT_CHILDREN_ON_REMOVE || |
---|
593 | (*reparenter)->parentMode & PNODE_REPARENT_ON_PARENTS_REMOVE) |
---|
594 | { |
---|
595 | printf("TEST----------------%s ---- %s\n", this->getClassName(), (*reparenter)->getClassName()); |
---|
596 | (*reparenter)->reparent(); |
---|
597 | printf("REPARENTED TO: %s::%s\n",(*reparenter)->getParent()->getClassName(),(*reparenter)->getParent()->getName()); |
---|
598 | } |
---|
599 | } |
---|
600 | if (this->parent != NULL) |
---|
601 | { |
---|
602 | this->parent->eraseChild(this); |
---|
603 | this->parent = NULL; |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | |
---|
608 | /** |
---|
609 | * @see PNode::setParent(PNode* parent); |
---|
610 | * @param parentName the name of the Parent to set to this PNode |
---|
611 | */ |
---|
612 | void PNode::setParent (const char* parentName) |
---|
613 | { |
---|
614 | PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE)); |
---|
615 | if (parentNode != NULL) |
---|
616 | parentNode->addChild(this); |
---|
617 | else |
---|
618 | PRINTF(2)("Not Found PNode's (%s::%s) new Parent by Name: %s\n", |
---|
619 | this->getClassName(), this->getName(), parentName); |
---|
620 | } |
---|
621 | |
---|
622 | |
---|
623 | /** |
---|
624 | * @brief does the reparenting in a very smooth way |
---|
625 | * @param parentNode the new Node to connect this node to. |
---|
626 | * @param bias the speed to iterate to this new Positions |
---|
627 | */ |
---|
628 | void PNode::setParentSoft(PNode* parentNode, float bias) |
---|
629 | { |
---|
630 | // return if the new parent and the old one match |
---|
631 | if (this->parent == parentNode ) |
---|
632 | return; |
---|
633 | if (parentNode == NULL) |
---|
634 | parentNode = PNode::getNullParent(); |
---|
635 | |
---|
636 | // store the Valures to iterate to. |
---|
637 | if (likely(this->toCoordinate == NULL)) |
---|
638 | { |
---|
639 | this->toCoordinate = new Vector(); |
---|
640 | *this->toCoordinate = this->getRelCoor(); |
---|
641 | } |
---|
642 | if (likely(this->toDirection == NULL)) |
---|
643 | { |
---|
644 | this->toDirection = new Quaternion(); |
---|
645 | *this->toDirection = this->getRelDir(); |
---|
646 | } |
---|
647 | this->bias = bias; |
---|
648 | |
---|
649 | Vector tmpV = this->getAbsCoor(); |
---|
650 | Quaternion tmpQ = this->getAbsDir(); |
---|
651 | |
---|
652 | parentNode->addChild(this); |
---|
653 | |
---|
654 | if (this->parentMode & PNODE_ROTATE_MOVEMENT && this->parent != NULL) |
---|
655 | this->relCoordinate = this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor()); |
---|
656 | else |
---|
657 | this->relCoordinate = tmpV - parentNode->getAbsCoor(); |
---|
658 | |
---|
659 | this->relDirection = tmpQ / parentNode->getAbsDir(); |
---|
660 | } |
---|
661 | |
---|
662 | |
---|
663 | /** |
---|
664 | * @brief does the reparenting in a very smooth way |
---|
665 | * @param parentName the name of the Parent to reconnect to |
---|
666 | * @param bias the speed to iterate to this new Positions |
---|
667 | */ |
---|
668 | void PNode::setParentSoft(const char* parentName, float bias) |
---|
669 | { |
---|
670 | PNode* parentNode = dynamic_cast<PNode*>(ClassList::getObject(parentName, CL_PARENT_NODE)); |
---|
671 | if (parentNode != NULL) |
---|
672 | this->setParentSoft(parentNode, bias); |
---|
673 | } |
---|
674 | |
---|
675 | /** |
---|
676 | * @param parentMode sets the parentingMode of this Node |
---|
677 | */ |
---|
678 | void PNode::setParentMode(PARENT_MODE parentMode) |
---|
679 | { |
---|
680 | this->parentMode = ((this->parentMode & 0xfff0) | parentMode); |
---|
681 | } |
---|
682 | |
---|
683 | /** |
---|
684 | * @brief sets the mode of this parent manually |
---|
685 | * @param parentMode a String representing this parentingMode |
---|
686 | */ |
---|
687 | void PNode::setParentMode (const char* parentingMode) |
---|
688 | { |
---|
689 | this->setParentMode(PNode::charToParentingMode(parentingMode)); |
---|
690 | } |
---|
691 | |
---|
692 | /** |
---|
693 | * @brief adds special mode Flags to this PNode |
---|
694 | * @see PARENT_MODE |
---|
695 | * @param nodeFlags a compsition of PARENT_MODE-flags, split by the '|' (or) operator. |
---|
696 | */ |
---|
697 | void PNode::addNodeFlags(unsigned short nodeFlags) |
---|
698 | { |
---|
699 | this->parentMode |= nodeFlags; |
---|
700 | } |
---|
701 | |
---|
702 | /** |
---|
703 | * @brief removes special mode Flags to this PNode |
---|
704 | * @see PARENT_MODE |
---|
705 | * @param nodeFlags a compsition of PARENT_MODE-flags, split by the '|' (or) operator. |
---|
706 | */ |
---|
707 | void PNode::removeNodeFlags(unsigned short nodeFlags) |
---|
708 | { |
---|
709 | this->parentMode &= !nodeFlags; |
---|
710 | } |
---|
711 | |
---|
712 | /** |
---|
713 | * @returns the NullParent (and if needed creates it) |
---|
714 | */ |
---|
715 | PNode* PNode::createNullParent() |
---|
716 | { |
---|
717 | if (likely(PNode::nullParent == NULL)) |
---|
718 | { |
---|
719 | PNode::nullParent = new PNode(NULL, PNODE_PARENT_MODE_DEFAULT | PNODE_REPARENT_TO_NULL); |
---|
720 | PNode::nullParent->setClassID(CL_NULL_PARENT, "NullParent"); |
---|
721 | PNode::nullParent->setName("NullParent"); |
---|
722 | PNode::nullParent->setSynchronized(true); |
---|
723 | } |
---|
724 | return PNode::nullParent; |
---|
725 | } |
---|
726 | |
---|
727 | |
---|
728 | /** |
---|
729 | * !! PRIVATE FUNCTION |
---|
730 | * @brief checks the upward integrity (e.g if PNode is somewhere up the Node tree.) |
---|
731 | * @param checkParent the Parent to check. |
---|
732 | * @returns true if the integrity-check succeeds, false otherwise. |
---|
733 | * |
---|
734 | * If there is a second occurence of checkParent before NULL, then a loop could get |
---|
735 | * into the Tree, and we do not want this. |
---|
736 | */ |
---|
737 | bool PNode::checkIntegrity(const PNode* checkParent) const |
---|
738 | { |
---|
739 | const PNode* parent = this; |
---|
740 | while ( (parent = parent->getParent()) != NULL) |
---|
741 | if (unlikely(parent == checkParent)) |
---|
742 | return false; |
---|
743 | return true; |
---|
744 | } |
---|
745 | |
---|
746 | |
---|
747 | /** |
---|
748 | * @brief updates the absCoordinate/absDirection |
---|
749 | * @param dt The time passed since the last update |
---|
750 | * |
---|
751 | * this is used to go through the parent-tree to update all the absolute coordinates |
---|
752 | * and directions. this update should be done by the engine, so you don't have to |
---|
753 | * worry, normaly... |
---|
754 | */ |
---|
755 | void PNode::updateNode (float dt) |
---|
756 | { |
---|
757 | if (!(this->parentMode & PNODE_STATIC_NODE)) |
---|
758 | { |
---|
759 | if( likely(this->parent != NULL)) |
---|
760 | { |
---|
761 | // movement for nodes with smoothMove enabled |
---|
762 | if (unlikely(this->toCoordinate != NULL)) |
---|
763 | { |
---|
764 | float shiftLen = fabsf(dt)*bias; |
---|
765 | if (shiftLen >= 1.0) |
---|
766 | shiftLen = 1.0; |
---|
767 | Vector moveVect = (*this->toCoordinate - this->relCoordinate) * shiftLen; |
---|
768 | if (likely(moveVect.len() >= PNODE_ITERATION_DELTA)) |
---|
769 | { |
---|
770 | this->shiftCoor(moveVect); |
---|
771 | } |
---|
772 | else |
---|
773 | { |
---|
774 | delete this->toCoordinate; |
---|
775 | this->toCoordinate = NULL; |
---|
776 | PRINTF(5)("SmoothMove of %s finished\n", this->getName()); |
---|
777 | } |
---|
778 | } |
---|
779 | if (unlikely(this->toDirection != NULL)) |
---|
780 | { |
---|
781 | float shiftLen = fabsf(dt)*bias; |
---|
782 | if (shiftLen >= 1.0) |
---|
783 | shiftLen = 1.0; |
---|
784 | //printf("%s::%s %f\n", this->getClassName(), this->getName(), this->toStep ); |
---|
785 | Quaternion rotQuat = Quaternion::quatSlerp(this->relDirection,*this->toDirection, shiftLen); |
---|
786 | if (this->relDirection.distance(rotQuat) > PNODE_ITERATION_DELTA) |
---|
787 | { |
---|
788 | this->relDirection = rotQuat; |
---|
789 | this->bRelDirChanged; |
---|
790 | } |
---|
791 | else |
---|
792 | { |
---|
793 | delete this->toDirection; |
---|
794 | this->toDirection = NULL; |
---|
795 | PRINTF(5)("SmoothRotate of %s finished\n", this->getName()); |
---|
796 | this->bRelDirChanged; |
---|
797 | } |
---|
798 | } |
---|
799 | |
---|
800 | // MAIN UPDATE ///////////////////////////////////// |
---|
801 | this->lastAbsCoordinate = this->absCoordinate; |
---|
802 | |
---|
803 | PRINTF(5)("PNode::update - '%s::%s' - (%f, %f, %f)\n", this->getClassName(), this->getName(), |
---|
804 | this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
805 | |
---|
806 | |
---|
807 | if(this->bRelDirChanged && this->parentMode & PNODE_LOCAL_ROTATE ) |
---|
808 | { |
---|
809 | /* update the current absDirection - remember * means rotation around sth.*/ |
---|
810 | this->prevRelCoordinate = this->relCoordinate; |
---|
811 | this->absDirection = parent->getAbsDir() * this->relDirection; |
---|
812 | } |
---|
813 | |
---|
814 | if(likely(this->bRelCoorChanged && this->parentMode & PNODE_MOVEMENT)) |
---|
815 | { |
---|
816 | /* update the current absCoordinate */ |
---|
817 | this->prevRelCoordinate = this->relCoordinate; |
---|
818 | this->absCoordinate = this->parent->getAbsCoor() + this->relCoordinate; |
---|
819 | } |
---|
820 | else if( this->parentMode & PNODE_ROTATE_MOVEMENT && (this->bRelCoorChanged || this->bRelDirChanged)) |
---|
821 | { |
---|
822 | /* update the current absCoordinate */ |
---|
823 | this->prevRelCoordinate = this->relCoordinate; |
---|
824 | this->absCoordinate = this->parent->getAbsCoor() + parent->getAbsDir().apply(this->relCoordinate); |
---|
825 | } |
---|
826 | ///////////////////////////////////////////////// |
---|
827 | } |
---|
828 | |
---|
829 | else // Nodes without a Parent are handled faster :: MOST LIKELY THE NULLPARENT |
---|
830 | { |
---|
831 | PRINTF(4)("update ParentLess Node (%s::%s) - (%f, %f, %f)\n", this->getClassName(), this->getName(), |
---|
832 | this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
833 | if (this->bRelCoorChanged) |
---|
834 | { |
---|
835 | this->prevRelCoordinate = this->relCoordinate; |
---|
836 | this->absCoordinate = this->relCoordinate; |
---|
837 | } |
---|
838 | if (this->bRelDirChanged) |
---|
839 | { |
---|
840 | this->prevRelDirection = this->relDirection; |
---|
841 | this->absDirection = this->getAbsDir () * this->relDirection; |
---|
842 | } |
---|
843 | } |
---|
844 | } |
---|
845 | |
---|
846 | if(!this->children.empty() && (this->bActive || this->parentMode & PNODE_UPDATE_CHILDREN_IF_INACTIVE )) |
---|
847 | { |
---|
848 | list<PNode*>::iterator child; |
---|
849 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
850 | { |
---|
851 | /* if this node has changed, make sure, that all children are updated also */ |
---|
852 | if( likely(this->bRelCoorChanged)) |
---|
853 | (*child)->parentCoorChanged (); |
---|
854 | if( likely(this->bRelDirChanged)) |
---|
855 | (*child)->parentDirChanged (); |
---|
856 | |
---|
857 | (*child)->updateNode(dt); |
---|
858 | } |
---|
859 | } |
---|
860 | this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt; |
---|
861 | this->bRelCoorChanged = false; |
---|
862 | this->bRelDirChanged = false; |
---|
863 | } |
---|
864 | |
---|
865 | |
---|
866 | |
---|
867 | |
---|
868 | |
---|
869 | /************* |
---|
870 | * DEBUGGING * |
---|
871 | *************/ |
---|
872 | /** |
---|
873 | * @brief counts total amount the children walking through the entire tree. |
---|
874 | * @param nodes the counter |
---|
875 | */ |
---|
876 | void PNode::countChildNodes(int& nodes) const |
---|
877 | { |
---|
878 | nodes++; |
---|
879 | list<PNode*>::const_iterator child; |
---|
880 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
881 | (*child)->countChildNodes(nodes); |
---|
882 | } |
---|
883 | |
---|
884 | |
---|
885 | /** |
---|
886 | * @brief displays some information about this pNode |
---|
887 | * @param depth The deph into which to debug the children of this PNode to. |
---|
888 | * (0: all children will be debugged, 1: only this PNode, 2: this and direct children, ...) |
---|
889 | * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output). |
---|
890 | */ |
---|
891 | void PNode::debugNode(unsigned int depth, unsigned int level) const |
---|
892 | { |
---|
893 | for (unsigned int i = 0; i < level; i++) |
---|
894 | PRINT(0)(" |"); |
---|
895 | if (this->children.size() > 0) |
---|
896 | PRINT(0)(" +"); |
---|
897 | else |
---|
898 | PRINT(0)(" -"); |
---|
899 | |
---|
900 | int childNodeCount = 0; |
---|
901 | this->countChildNodes(childNodeCount); |
---|
902 | |
---|
903 | 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", |
---|
904 | this->getClassName(), |
---|
905 | this->getName(), |
---|
906 | this->absCoordinate.x, |
---|
907 | this->absCoordinate.y, |
---|
908 | this->absCoordinate.z, |
---|
909 | this->relCoordinate.x, |
---|
910 | this->relCoordinate.y, |
---|
911 | this->relCoordinate.z, |
---|
912 | this->getAbsDirV().x, |
---|
913 | this->getAbsDirV().y, |
---|
914 | this->getAbsDirV().z, |
---|
915 | this->parentingModeToChar(parentMode), |
---|
916 | childNodeCount); |
---|
917 | if (depth >= 2 || depth == 0) |
---|
918 | { |
---|
919 | list<PNode*>::const_iterator child; |
---|
920 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
921 | { |
---|
922 | if (depth == 0) |
---|
923 | (*child)->debugNode(0, level + 1); |
---|
924 | else |
---|
925 | (*child)->debugNode(depth - 1, level +1); |
---|
926 | } |
---|
927 | } |
---|
928 | } |
---|
929 | |
---|
930 | /** |
---|
931 | * @brief displays the PNode at its position with its rotation as a cube. |
---|
932 | * @param depth The deph into which to debug the children of this PNode to. |
---|
933 | * (0: all children will be displayed, 1: only this PNode, 2: this and direct children, ...) |
---|
934 | * @param size the Size of the Box to draw. |
---|
935 | * @param color the color of the Box to display. |
---|
936 | * @param level !! INTERNAL !! The n-th level of the Node we draw (this is internal and only for nice output). |
---|
937 | */ |
---|
938 | void PNode::debugDraw(unsigned int depth, float size, const Vector& color, unsigned int level) const |
---|
939 | { |
---|
940 | // if this is the first Element we draw |
---|
941 | if (level == 0) |
---|
942 | { |
---|
943 | glPushAttrib(GL_ENABLE_BIT); // save the Enable-attributes |
---|
944 | glMatrixMode(GL_MODELVIEW); // goto the ModelView Matrix |
---|
945 | |
---|
946 | glDisable(GL_LIGHTING); // disable lighting (we do not need them for just lighting) |
---|
947 | glDisable(GL_BLEND); // '' |
---|
948 | glDisable(GL_TEXTURE_2D); // '' |
---|
949 | glDisable(GL_DEPTH_TEST); // '' |
---|
950 | } |
---|
951 | |
---|
952 | glPushMatrix(); // repush the Matrix-stack |
---|
953 | /* translate */ |
---|
954 | glTranslatef (this->getAbsCoor ().x, |
---|
955 | this->getAbsCoor ().y, |
---|
956 | this->getAbsCoor ().z); |
---|
957 | // this->getAbsDir ().matrix (matrix); |
---|
958 | |
---|
959 | /* rotate */ |
---|
960 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
961 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
962 | /* set the new Color */ |
---|
963 | glColor3f(color.x, color.y, color.z); |
---|
964 | { /* draw a cube of size size */ |
---|
965 | glBegin(GL_LINE_STRIP); |
---|
966 | glVertex3f(-.5*size, -.5*size, -.5*size); |
---|
967 | glVertex3f(+.5*size, -.5*size, -.5*size); |
---|
968 | glVertex3f(+.5*size, -.5*size, +.5*size); |
---|
969 | glVertex3f(-.5*size, -.5*size, +.5*size); |
---|
970 | glVertex3f(-.5*size, -.5*size, -.5*size); |
---|
971 | glEnd(); |
---|
972 | glBegin(GL_LINE_STRIP); |
---|
973 | glVertex3f(-.5*size, +.5*size, -.5*size); |
---|
974 | glVertex3f(+.5*size, +.5*size, -.5*size); |
---|
975 | glVertex3f(+.5*size, +.5*size, +.5*size); |
---|
976 | glVertex3f(-.5*size, +.5*size, +.5*size); |
---|
977 | glVertex3f(-.5*size, +.5*size, -.5*size); |
---|
978 | glEnd(); |
---|
979 | |
---|
980 | glBegin(GL_LINES); |
---|
981 | glVertex3f(-.5*size, -.5*size, -.5*size); |
---|
982 | glVertex3f(-.5*size, +.5*size, -.5*size); |
---|
983 | glVertex3f(+.5*size, -.5*size, -.5*size); |
---|
984 | glVertex3f(+.5*size, +.5*size, -.5*size); |
---|
985 | glVertex3f(+.5*size, -.5*size, +.5*size); |
---|
986 | glVertex3f(+.5*size, +.5*size, +.5*size); |
---|
987 | glVertex3f(-.5*size, -.5*size, +.5*size); |
---|
988 | glVertex3f(-.5*size, +.5*size, +.5*size); |
---|
989 | glEnd(); |
---|
990 | } |
---|
991 | glPopMatrix(); |
---|
992 | |
---|
993 | if (depth >= 2 || depth == 0) |
---|
994 | { |
---|
995 | /* rotate the current color in HSV space around 20 degree */ |
---|
996 | Vector childColor = Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0)); |
---|
997 | list<PNode*>::const_iterator child; |
---|
998 | for (child = this->children.begin(); child != this->children.end(); child ++) |
---|
999 | { |
---|
1000 | // drawing the Dependency graph |
---|
1001 | if (this != PNode::getNullParent()) |
---|
1002 | { |
---|
1003 | glBegin(GL_LINES); |
---|
1004 | glColor3f(color.x, color.y, color.z); |
---|
1005 | glVertex3f(this->getAbsCoor ().x, |
---|
1006 | this->getAbsCoor ().y, |
---|
1007 | this->getAbsCoor ().z); |
---|
1008 | glColor3f(childColor.x, childColor.y, childColor.z); |
---|
1009 | glVertex3f((*child)->getAbsCoor ().x, |
---|
1010 | (*child)->getAbsCoor ().y, |
---|
1011 | (*child)->getAbsCoor ().z); |
---|
1012 | glEnd(); |
---|
1013 | } |
---|
1014 | |
---|
1015 | /* if we want to draw the children too */ |
---|
1016 | if (depth == 0) /* -> all of them */ |
---|
1017 | (*child)->debugDraw(0, size, childColor, level+1); |
---|
1018 | else /* -> only the Next one */ |
---|
1019 | (*child)->debugDraw(depth - 1, size, childColor, level +1); |
---|
1020 | } |
---|
1021 | } |
---|
1022 | if (level == 0) |
---|
1023 | glPopAttrib(); /* pop the saved attributes back out */ |
---|
1024 | } |
---|
1025 | |
---|
1026 | |
---|
1027 | |
---|
1028 | ///////////////////// |
---|
1029 | // HELPER_FUCTIONS // |
---|
1030 | ///////////////////// |
---|
1031 | |
---|
1032 | /** |
---|
1033 | * @brief converts a parentingMode into a string that is the name of it |
---|
1034 | * @param parentingMode the ParentingMode to convert |
---|
1035 | * @return the converted string |
---|
1036 | */ |
---|
1037 | const char* PNode::parentingModeToChar(int parentingMode) |
---|
1038 | { |
---|
1039 | if (parentingMode == PNODE_LOCAL_ROTATE) |
---|
1040 | return "local-rotate"; |
---|
1041 | else if (parentingMode == PNODE_ROTATE_MOVEMENT) |
---|
1042 | return "rotate-movement"; |
---|
1043 | else if (parentingMode == PNODE_MOVEMENT) |
---|
1044 | return "movement"; |
---|
1045 | else if (parentingMode == PNODE_ALL) |
---|
1046 | return "all"; |
---|
1047 | else if (parentingMode == PNODE_ROTATE_AND_MOVE) |
---|
1048 | return "rotate-and-move"; |
---|
1049 | } |
---|
1050 | |
---|
1051 | /** |
---|
1052 | * @brief converts a parenting-mode-string into a int |
---|
1053 | * @param parentingMode the string naming the parentingMode |
---|
1054 | * @return the int corresponding to the named parentingMode |
---|
1055 | */ |
---|
1056 | PARENT_MODE PNode::charToParentingMode(const char* parentingMode) |
---|
1057 | { |
---|
1058 | if (!strcmp(parentingMode, "local-rotate")) |
---|
1059 | return (PNODE_LOCAL_ROTATE); |
---|
1060 | else if (!strcmp(parentingMode, "rotate-movement")) |
---|
1061 | return (PNODE_ROTATE_MOVEMENT); |
---|
1062 | else if (!strcmp(parentingMode, "movement")) |
---|
1063 | return (PNODE_MOVEMENT); |
---|
1064 | else if (!strcmp(parentingMode, "all")) |
---|
1065 | return (PNODE_ALL); |
---|
1066 | else if (!strcmp(parentingMode, "rotate-and-move")) |
---|
1067 | return (PNODE_ROTATE_AND_MOVE); |
---|
1068 | } |
---|
1069 | |
---|
1070 | /** |
---|
1071 | * Writes data from network containing information about the state |
---|
1072 | * @param data pointer to data |
---|
1073 | * @param length length of data |
---|
1074 | * @param sender hostID of sender |
---|
1075 | */ |
---|
1076 | int PNode::writeState( const byte * data, int length, int sender ) |
---|
1077 | { |
---|
1078 | SYNCHELP_READ_BEGIN(); |
---|
1079 | |
---|
1080 | SYNCHELP_READ_FKT( BaseObject::writeState, NWT_PN_BO_WRITESTATE ); |
---|
1081 | |
---|
1082 | // char * parentName = NULL; |
---|
1083 | // SYNCHELP_READ_STRINGM( parentName ); |
---|
1084 | // |
---|
1085 | // if ( strcmp(parentName, "")==0 ) |
---|
1086 | // { |
---|
1087 | // setParent( (char*)NULL ); |
---|
1088 | // } |
---|
1089 | // else |
---|
1090 | // { |
---|
1091 | // setParent( parentName ); |
---|
1092 | // } |
---|
1093 | // |
---|
1094 | // delete[] parentName; |
---|
1095 | |
---|
1096 | int parentMode; |
---|
1097 | SYNCHELP_READ_INT( parentMode, NWT_PN_PARENTMODE ); |
---|
1098 | this->setParentMode((PARENT_MODE)parentMode); |
---|
1099 | |
---|
1100 | float f1, f2, f3, f4; |
---|
1101 | |
---|
1102 | SYNCHELP_READ_FLOAT( f1, NWT_PN_COORX ); |
---|
1103 | SYNCHELP_READ_FLOAT( f2, NWT_PN_COORY ); |
---|
1104 | SYNCHELP_READ_FLOAT( f3, NWT_PN_COORZ ); |
---|
1105 | this->setRelCoor( f1, f2, f3 ); |
---|
1106 | |
---|
1107 | |
---|
1108 | SYNCHELP_READ_FLOAT( f1, NWT_PN_ROTV ); |
---|
1109 | SYNCHELP_READ_FLOAT( f2, NWT_PN_ROTX ); |
---|
1110 | SYNCHELP_READ_FLOAT( f3, NWT_PN_ROTY ); |
---|
1111 | SYNCHELP_READ_FLOAT( f4, NWT_PN_ROTZ ); |
---|
1112 | this->setRelDir( Quaternion( Vector(f2, f3, f4), f1 ) ); |
---|
1113 | |
---|
1114 | // int n; |
---|
1115 | // char * childName; |
---|
1116 | // |
---|
1117 | // PRINTF(0)("JKLO %d %d %d %d\n", data[__synchelp_read_i], data[__synchelp_read_i+1], data[__synchelp_read_i+2], data[__synchelp_read_i+3]); |
---|
1118 | // SYNCHELP_READ_INT( n ); |
---|
1119 | // PRINTF(0)("read %s:n=%d\n", this->getName(), n); |
---|
1120 | // |
---|
1121 | // for (int i = 0; i<n; i++) |
---|
1122 | // { |
---|
1123 | // SYNCHELP_READ_STRINGM( childName ); |
---|
1124 | // PRINTF(0)("RCVD CHILD = %s\n", childName); |
---|
1125 | // addChild( childName ); |
---|
1126 | // delete childName; |
---|
1127 | // childName = NULL; |
---|
1128 | // } |
---|
1129 | |
---|
1130 | return SYNCHELP_READ_N; |
---|
1131 | } |
---|
1132 | |
---|
1133 | /** |
---|
1134 | * data copied in data will bee sent to another host |
---|
1135 | * @param data pointer to data |
---|
1136 | * @param maxLength max length of data |
---|
1137 | * @return the number of bytes writen |
---|
1138 | */ |
---|
1139 | int PNode::readState( byte * data, int maxLength ) |
---|
1140 | { |
---|
1141 | SYNCHELP_WRITE_BEGIN(); |
---|
1142 | |
---|
1143 | SYNCHELP_WRITE_FKT( BaseObject::readState, NWT_PN_BO_WRITESTATE ); |
---|
1144 | |
---|
1145 | // if ( this->parent ) |
---|
1146 | // { |
---|
1147 | // SYNCHELP_WRITE_STRING( parent->getName() ); |
---|
1148 | // } |
---|
1149 | // else |
---|
1150 | // { |
---|
1151 | // SYNCHELP_WRITE_STRING( "" ); |
---|
1152 | // } |
---|
1153 | |
---|
1154 | SYNCHELP_WRITE_INT( this->parentMode, NWT_PN_PARENTMODE ); |
---|
1155 | |
---|
1156 | SYNCHELP_WRITE_FLOAT( this->relCoordinate.x, NWT_PN_COORX ); |
---|
1157 | SYNCHELP_WRITE_FLOAT( this->relCoordinate.y, NWT_PN_COORY ); |
---|
1158 | SYNCHELP_WRITE_FLOAT( this->relCoordinate.z, NWT_PN_COORZ ); |
---|
1159 | |
---|
1160 | SYNCHELP_WRITE_FLOAT( this->relDirection.w, NWT_PN_ROTV ); |
---|
1161 | SYNCHELP_WRITE_FLOAT( this->relDirection.v.x, NWT_PN_ROTX ); |
---|
1162 | SYNCHELP_WRITE_FLOAT( this->relDirection.v.y, NWT_PN_ROTY ); |
---|
1163 | SYNCHELP_WRITE_FLOAT( this->relDirection.v.z, NWT_PN_ROTZ ); |
---|
1164 | |
---|
1165 | // int n = children.size(); |
---|
1166 | // //check if camera is in children |
---|
1167 | // for (std::list<PNode*>::const_iterator it = children.begin(); it!=children.end(); it++) |
---|
1168 | // { |
---|
1169 | // if ( (*it)->isA(CL_CAMERA) ) |
---|
1170 | // n--; |
---|
1171 | // } |
---|
1172 | // PRINTF(0)("write %s:n=%d\n", this->getName(), n); |
---|
1173 | // SYNCHELP_WRITE_INT( n ); |
---|
1174 | // PRINTF(0)("ASDF %d %d %d %d\n", data[__synchelp_write_i-4], data[__synchelp_write_i-3], data[__synchelp_write_i-2], data[__synchelp_write_i-1]); |
---|
1175 | // |
---|
1176 | // |
---|
1177 | // for (std::list<PNode*>::const_iterator it = children.begin(); it!=children.end(); it++) |
---|
1178 | // { |
---|
1179 | // //dont add camera because there is only one camera attached to local player |
---|
1180 | // if ( !(*it)->isA(CL_CAMERA) ) |
---|
1181 | // { |
---|
1182 | // PRINTF(0)("SENDING CHILD: %s\n", (*it)->getName()); |
---|
1183 | // SYNCHELP_WRITE_STRING( (*it)->getName() ); |
---|
1184 | // } |
---|
1185 | // } |
---|
1186 | |
---|
1187 | return SYNCHELP_WRITE_N; |
---|
1188 | } |
---|
1189 | |
---|
1190 | #define __FLAG_COOR 1 |
---|
1191 | #define __FLAG_ROT 2 |
---|
1192 | |
---|
1193 | #define __OFFSET_POS 1 |
---|
1194 | #define __OFFSET_ROT 0.05 |
---|
1195 | |
---|
1196 | /** |
---|
1197 | * Writes data from network containing information about the state which has changed |
---|
1198 | * @param data pointer to data |
---|
1199 | * @param length length of data |
---|
1200 | * @param sender hostID of sender |
---|
1201 | */ |
---|
1202 | int PNode::writeSync( const byte * data, int length, int sender ) |
---|
1203 | { |
---|
1204 | SYNCHELP_READ_BEGIN(); |
---|
1205 | |
---|
1206 | if ( this->getHostID()==this->getOwner() ) |
---|
1207 | { |
---|
1208 | return SYNCHELP_READ_N; |
---|
1209 | } |
---|
1210 | |
---|
1211 | byte flags = 0; |
---|
1212 | SYNCHELP_READ_BYTE( flags, NWT_PN_FLAGS ); |
---|
1213 | //PRINTF(0)("%s::FLAGS = %d\n", this->getName(), flags); |
---|
1214 | |
---|
1215 | float f1, f2, f3, f4; |
---|
1216 | |
---|
1217 | if ( flags & __FLAG_COOR ) |
---|
1218 | { |
---|
1219 | SYNCHELP_READ_FLOAT( f1, NWT_PN_SCOORX ); |
---|
1220 | SYNCHELP_READ_FLOAT( f2, NWT_PN_SCOORY ); |
---|
1221 | SYNCHELP_READ_FLOAT( f3, NWT_PN_SCOORZ ); |
---|
1222 | PRINTF(0)("RCVD COOR: %f %f %f\n", f1, f2, f3); |
---|
1223 | this->setRelCoor( f1, f2, f3 ); |
---|
1224 | } |
---|
1225 | |
---|
1226 | if ( flags & __FLAG_ROT ) |
---|
1227 | { |
---|
1228 | SYNCHELP_READ_FLOAT( f1, NWT_PN_SROTV ); |
---|
1229 | SYNCHELP_READ_FLOAT( f2, NWT_PN_SROTX ); |
---|
1230 | SYNCHELP_READ_FLOAT( f3, NWT_PN_SROTY ); |
---|
1231 | SYNCHELP_READ_FLOAT( f4, NWT_PN_SROTZ ); |
---|
1232 | PRINTF(0)("RCVD QUAT: %f %f %f %f\n", f1, f2, f3, f4); |
---|
1233 | //this->setRelDir( Quaternion( Vector(f2, f3, f4), f1 ) ); |
---|
1234 | Quaternion q; |
---|
1235 | q.w = f1; |
---|
1236 | q.v.x = f2; |
---|
1237 | q.v.y = f3; |
---|
1238 | q.v.z = f4; |
---|
1239 | this->setAbsDir( q ); |
---|
1240 | } |
---|
1241 | |
---|
1242 | return SYNCHELP_READ_N; |
---|
1243 | } |
---|
1244 | |
---|
1245 | /** |
---|
1246 | * data copied in data will bee sent to another host |
---|
1247 | * @param data pointer to data |
---|
1248 | * @param maxLength max length of data |
---|
1249 | * @return the number of bytes writen |
---|
1250 | */ |
---|
1251 | int PNode::readSync( byte * data, int maxLength ) |
---|
1252 | { |
---|
1253 | //WARNING: if you change this file make sure you also change needsReadSync |
---|
1254 | SYNCHELP_WRITE_BEGIN(); |
---|
1255 | |
---|
1256 | if ( this->getHostID()!=0 && this->getHostID()!=this->getOwner() ) |
---|
1257 | { |
---|
1258 | return SYNCHELP_WRITE_N; |
---|
1259 | } |
---|
1260 | |
---|
1261 | byte flags = 0; |
---|
1262 | if ( fabs( coorx - relCoordinate.x ) > __OFFSET_POS*0.05*this->velocity.len() || |
---|
1263 | fabs( coory - relCoordinate.y ) > __OFFSET_POS*0.05*this->velocity.len() || |
---|
1264 | fabs( coorz - relCoordinate.z ) > __OFFSET_POS*0.05*this->velocity.len() ) |
---|
1265 | flags |= __FLAG_COOR; |
---|
1266 | |
---|
1267 | if ( fabs( rotw - absDirection.w ) > __OFFSET_ROT || |
---|
1268 | fabs( rotx - absDirection.v.x ) > __OFFSET_ROT || |
---|
1269 | fabs( roty - absDirection.v.y ) > __OFFSET_ROT || |
---|
1270 | fabs( rotz - absDirection.v.z ) > __OFFSET_ROT ) |
---|
1271 | flags |= __FLAG_ROT; |
---|
1272 | |
---|
1273 | |
---|
1274 | SYNCHELP_WRITE_BYTE( flags, NWT_PN_FLAGS ); |
---|
1275 | PRINTF(0)("FLAGS = %d\n", flags); |
---|
1276 | |
---|
1277 | if ( flags & __FLAG_COOR ) |
---|
1278 | { |
---|
1279 | |
---|
1280 | PRINTF(0)("SEND COOR: %f %f %f\n", this->relCoordinate.x, this->relCoordinate.y, this->relCoordinate.z); |
---|
1281 | |
---|
1282 | SYNCHELP_WRITE_FLOAT( this->relCoordinate.x, NWT_PN_SCOORX ); |
---|
1283 | SYNCHELP_WRITE_FLOAT( this->relCoordinate.y, NWT_PN_SCOORY ); |
---|
1284 | SYNCHELP_WRITE_FLOAT( this->relCoordinate.z, NWT_PN_SCOORZ ); |
---|
1285 | |
---|
1286 | coorx = relCoordinate.x; |
---|
1287 | coory = relCoordinate.y; |
---|
1288 | coorz = relCoordinate.z; |
---|
1289 | } |
---|
1290 | |
---|
1291 | if ( flags & __FLAG_ROT ) |
---|
1292 | { |
---|
1293 | |
---|
1294 | PRINTF(0)("SEND QUAT: %f %f %f %f\n", this->absDirection.w, this->absDirection.v.x, this->absDirection.v.y, this->absDirection.v.z); |
---|
1295 | |
---|
1296 | SYNCHELP_WRITE_FLOAT( this->absDirection.w, NWT_PN_SROTV ); |
---|
1297 | SYNCHELP_WRITE_FLOAT( this->absDirection.v.x, NWT_PN_SROTX ); |
---|
1298 | SYNCHELP_WRITE_FLOAT( this->absDirection.v.y, NWT_PN_SROTY ); |
---|
1299 | SYNCHELP_WRITE_FLOAT( this->absDirection.v.z, NWT_PN_SROTZ ); |
---|
1300 | |
---|
1301 | rotw = absDirection.w; |
---|
1302 | rotx = absDirection.v.x; |
---|
1303 | roty = absDirection.v.y; |
---|
1304 | rotz = absDirection.v.z; |
---|
1305 | } |
---|
1306 | |
---|
1307 | return SYNCHELP_WRITE_N; |
---|
1308 | } |
---|
1309 | |
---|
1310 | bool PNode::needsReadSync( ) |
---|
1311 | { |
---|
1312 | if ( fabs( coorx - relCoordinate.x ) > __OFFSET_POS*0.05*this->velocity.len() || |
---|
1313 | fabs( coory - relCoordinate.y ) > __OFFSET_POS*0.05*this->velocity.len() || |
---|
1314 | fabs( coorz - relCoordinate.z ) > __OFFSET_POS*0.05*this->velocity.len() ) |
---|
1315 | return true; |
---|
1316 | |
---|
1317 | if ( fabs( rotw - absDirection.w ) > __OFFSET_ROT || |
---|
1318 | fabs( rotx - absDirection.v.x ) > __OFFSET_ROT || |
---|
1319 | fabs( roty - absDirection.v.y ) > __OFFSET_ROT || |
---|
1320 | fabs( rotz - absDirection.v.z ) > __OFFSET_ROT ) |
---|
1321 | return true; |
---|
1322 | |
---|
1323 | return false; |
---|
1324 | } |
---|