Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/coord/p_node.h @ 6048

Last change on this file since 6048 was 6048, checked in by bensch, 18 years ago

orxonox/trunk: some fixes, to PNode and world.

File size: 10.1 KB
Line 
1/*!
2 * @file p_node.h
3 * @brief Definition of THE Parenting Node
4 *
5 *  parenting is how coordinates are handled in orxonox, meaning, that all coordinates
6 *  are representet relative to another parent node. this nodes build a parenting
7 *  tree of one-sided references (from up to down referenced).
8 *  Every node manages itself a list of childrens (of whos it is parent - easy...).
9 *
10 *  absCoordinate, absDirection have to be recalculated as soon as there was a change in
11 *  place or ortientation. this is only the case if
12 *  o bRelCoorChanged is true (so moved)
13 *  o bRelDirChanged is true (so changed)
14 *  this conditions make it cheaper to recalculate the tree (reduces redundant work).
15 */
16
17
18#ifndef _P_NODE_H
19#define _P_NODE_H
20
21#include "base_object.h"
22
23#include "vector.h"
24#include <list>
25
26// FORWARD DECLARATION
27class TiXmlElement;
28
29#define PNODE_ITERATION_DELTA    .001
30//! Parental linkage modes
31typedef enum
32{
33  // PARENTAL FOLLOWING
34  PNODE_LOCAL_ROTATE                   = 0x0001,    //!< Rotates all the children around their centers.
35  PNODE_ROTATE_MOVEMENT                = 0x0002,    //!< Moves all the children around the center of their parent, without the rotation around their own centers.
36
37  PNODE_MOVEMENT                       = 0x0004,    //!< Moves all children along with the parent.
38  // special linkage modes
39  PNODE_ALL                            = 0x0003,    //!< Moves all children around the center of their parent, and also rotates their centers
40  PNODE_ROTATE_AND_MOVE                = 0x0005,    //!< Rotates all children around their axis, and moves them as the Parent Moves, but does not rotate around the center of their parent.
41
42
43  // REPARENTING
44  PNODE_REPARENT_TO_NULLPARENT         = 0x0010,    //!< Reparents to the NullParent, if the Parent is Removed.
45  PNODE_REPARENT_TO_PARENTS_PARENT     = 0x0020,    //!< Reparents the Node to the parents (old) parent it the parent gets removed.
46  PNODE_REPARENT_DELETE_CHILDREN       = 0x0040,    //!< Deletes the Children of the node when This Node is Removed. (Use with care).
47  PNODE_REPARENT_KEEP_POSITION         = 0x0080,    //!< Tries to keep the Position if the Node is reparented.
48
49
50  // DELETION
51  PNODE_PROHIBIT_CHILD_DELETE          = 0x0100,    //!< Prohibits the Children from being deleted if this Node gets deleted.
52  PNODE_PROHIBIT_DELETE_WITH_PARENT    = 0x0200,    //!< Prohibits the Node to be deleted if the Parent is. Child will be reparented according to the Repaenting-Rules
53  PNODE_REPARENT_CHILDREN_ON_DELETE    = 0x0400,    //!< Reparents the Children of the Node to
54  PNODE_REPARANT_CHILDREN_ON_REMOVE    = 0x0800,    //!< Reparents the Children of the Node if the Node gets Removed.
55
56  // VISIBILITY/ACTIVITY
57  PNODE_HIDE_CHILDREN_IF_HIDDEN        = 0x1000,    //!< Prohibits the Children from being drawn if this node isn't visible. (used for Draw))
58  PNODE_HIDE_IF_PARENT_HIDDEN          = 0x2000,    //!< Prohibits the node from being drawn if the Parent is invisible.
59  PNODE_UPDATE_CHILDREN_IF_INACTIVE    = 0x4000,    //!< Updates the Children of this Node even if the Parent is Inactive (note if this's parent is inactive children won't be updated.)
60  PNODE_STATIC_NODE                    = 0x8000,    //!< Used for nodes that do not have any moving children, and that do not move.
61
62} PARENT_MODE;
63
64//! The default mode of the translation-binding.
65#define PNODE_PARENT_MODE_DEFAULT PNODE_ALL | \
66                                  PNODE_REPARENT_KEEP_POSITION
67
68
69//! Patent Node is a Engine to calculate the position of an Object in respect to the position of its parent.
70class PNode : virtual public BaseObject {
71
72 public:
73  PNode ();
74  PNode(const TiXmlElement* root);
75  PNode (const Vector& absCoor, PNode* pNode);
76  virtual ~PNode ();
77
78  void loadParams(const TiXmlElement* root);
79
80  void setRelCoor (const Vector& relCoord);
81  void setRelCoor (float x, float y, float z);
82  void setRelCoorSoft(const Vector& relCoordSoft, float bias = 1.0);
83  void setRelCoorSoft(float x, float y, float z, float bias = 1.0);
84  /** @returns the relative position */
85  inline const Vector& getRelCoor () const { return this->prevRelCoordinate; };
86  /** @returns the Relative Coordinate Destination */
87  inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)? *this->toCoordinate : this->relCoordinate; };
88  void setAbsCoor (const Vector& absCoord);
89  void setAbsCoor (float x, float y, float z);
90  void setAbsCoorSoft(const Vector& absCoordSoft, float bias = 1.0);
91  void setAbsCoorSoft(float x, float y, float z, float bias = 1.0);
92  /** @returns the absolute position */
93  inline const Vector& getAbsCoor () const { return this->absCoordinate; };
94  void shiftCoor (const Vector& shift);
95  void shiftCoor (float x, float y, float z) { this->shiftCoor(Vector(x, y, z)); };
96
97  void setRelDir (const Quaternion& relDir);
98  void setRelDir (float x, float y, float z);
99  void setRelDirSoft(const Quaternion& relDirSoft, float bias = 1.0);
100  void setRelDirSoft(float x, float y, float z, float bias = 1.0);
101  /** @returns the relative Direction */
102  inline const Quaternion& getRelDir () const { return this->prevRelDirection; };
103  /** @returns the Relative Directional Destination */
104  inline const Quaternion& getRelDirSoft2D() const { return (this->toDirection)? *this->toDirection : this->relDirection; };
105  /** @returns a Vector pointing into the relative Direction */
106  inline Vector getRelDirV() const { return this->prevRelDirection.apply(Vector(0,1,0)); };
107  void setAbsDir (const Quaternion& absDir);
108  void setAbsDir (float x, float y, float z);
109  void setAbsDirSoft(const Quaternion& absDirSoft, float bias = 1.0);
110  void setAbsDirSoft(float x, float y, float z, float bias = 1.0);
111  void shiftDir (const Quaternion& shift);
112  /** @returns the absolute Direction */
113  inline const Quaternion& getAbsDir () const { return this->absDirection; };
114  /** @returns a Vector pointing into the absolute Direction */
115  inline Vector getAbsDirV() const { return this->absDirection.apply(Vector(0,1,0)); };
116  /** @returns A Vector pointing into the forward direction (X) of the Node */
117  inline Vector getAbsDirX() const { return this->absDirection.apply(Vector(1,0,0)); };
118  /** @returns A Vector pointing into the upward direction (Y) of the Node */
119  inline Vector getAbsDirY() const { return this->absDirection.apply(Vector(0,1,0)); };
120  /** @returns A Vector pointing into the right direction (Z) of the Node */
121  inline Vector getAbsDirZ() const { return this->absDirection.apply(Vector(0,0,1)); };
122
123  /** @returns the Speed of the Node */
124  inline float getSpeed() const { return this->velocity.len(); };
125  /** @returns the Velocity of the Node */
126  inline const Vector& getVelocity() const { return this->velocity; };
127
128
129  void addChild (PNode* child);
130  void addChild (const char* childName);
131  void removeChild (PNode* child);
132  void removeNode();
133
134  /** @param the new parent of this node */
135  inline void setParent (PNode* parent) { parent->addChild(this); };
136  void setParent (const char* parentName);
137  /** @returns the parent of this PNode */
138  inline PNode* getParent () const { return this->parent; };
139  /** @returns the List of Children of this PNode */
140 // inline const tList<PNode>* getChildren() const { return this->children; };
141
142  void setParentSoft(PNode* parentNode, float bias = 1.0);
143  void setParentSoft(const char* parentName, float bias = 1.0);
144
145  /** @param parentMode sets the parentingMode of this Node */
146  void setParentMode (PARENT_MODE parentMode) { this->parentMode = parentMode; };
147  void setParentMode (const char* parentingMode);
148  /** @returns the Parenting mode of this node */
149  int getParentMode() const { return this->parentMode; };
150
151  void updateNode (float dt);
152
153  void countChildNodes(int& nodes) const;
154  void debugNode (unsigned int depth = 1, unsigned int level = 0) const;
155  void debugDraw(unsigned int depth = 1, float size = 1.0, const Vector& color = Vector(1, 0, 0), unsigned int level = 0) const;
156
157
158  // helper functions //
159  static const char* parentingModeToChar(int parentingMode);
160  static PARENT_MODE charToParentingMode(const char* parentingMode);
161
162
163 private:
164  void init(PNode* parent);
165  /** tells the child that the parent's Coordinate has changed */
166  inline void parentCoorChanged () { this->bRelCoorChanged = true; }
167  /** tells the child that the parent's Direction has changed */
168  inline void parentDirChanged () { this->bRelDirChanged = true; }
169  /** @returns the last calculated coordinate */
170  inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; }
171
172  void reparent();
173
174 private:
175  bool               bRelCoorChanged;    //!< If Relative Coordinate has changed since last time we checked
176  bool               bRelDirChanged;     //!< If Relative Direction has changed since last time we checked
177
178  Vector             relCoordinate;      //!< coordinates relative to the parent
179  Vector             absCoordinate;      //!< absolute coordinates in the world ( from (0,0,0) )
180  Quaternion         relDirection;       //!< direction relative to the parent
181  Quaternion         absDirection;       //!< absolute direvtion in the world ( from (0,0,1) )
182
183  Vector             prevRelCoordinate;  //!< The last Relative Coordinate from the last update-Cycle.
184  Vector             lastAbsCoordinate;  //!< this is used for speedcalculation, it stores the last coordinate
185  Quaternion         prevRelDirection;   //!< The last Relative Direciton from the last update-Cycle.
186//  Quaternion         lastAbsDirection;
187
188  Vector             velocity;           //!< Saves the velocity.
189
190  Vector*            toCoordinate;       //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft)
191  Quaternion*        toDirection;        //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft)
192  float              bias;               //!< how fast to iterate to the given position (default is 1)
193
194  PNode*             parent;             //!< a pointer to the parent node.
195  std::list<PNode*>  children;           //!< list of the children of this PNode.
196
197  bool               bActive;            //!< If the Node is Active (for cutting off the rest of the tree in update).
198  unsigned short     parentMode;         //!< the mode of the binding
199};
200
201#endif /* _P_NODE_H */
Note: See TracBrowser for help on using the repository browser.