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 | #include "stdincl.h" |
---|
23 | #include "synchronizeable.h" |
---|
24 | |
---|
25 | #include "vector.h" |
---|
26 | #include "quaternion.h" |
---|
27 | #include <list> |
---|
28 | |
---|
29 | // FORWARD DECLARATION |
---|
30 | class TiXmlElement; |
---|
31 | |
---|
32 | #define PNODE_ITERATION_DELTA .001 |
---|
33 | |
---|
34 | //! Parental linkage modes |
---|
35 | typedef enum |
---|
36 | { |
---|
37 | // PARENTAL FOLLOWING |
---|
38 | PNODE_LOCAL_ROTATE = 0x0001, //!< Rotates all the children around their centers. |
---|
39 | PNODE_ROTATE_MOVEMENT = 0x0002, //!< Moves all the children around the center of their parent, without the rotation around their own centers. |
---|
40 | |
---|
41 | PNODE_MOVEMENT = 0x0004, //!< Moves all children along with the parent. |
---|
42 | // special linkage modes |
---|
43 | PNODE_ALL = 0x0003, //!< Moves all children around the center of their parent, and also rotates their centers |
---|
44 | 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. |
---|
45 | |
---|
46 | |
---|
47 | // REPARENTING |
---|
48 | PNODE_REPARENT_TO_NULL = 0x0010, //!< Reparents to the Null, if the Parent is Removed. Meaning the Node wont have a parent anymore. |
---|
49 | PNODE_REPARENT_TO_PARENTS_PARENT = 0x0020, //!< Reparents the Node to the parents (old) parent it the parent gets removed. |
---|
50 | ///////////////////////////////////////////// // ELSE: Reparents to the NullParent. |
---|
51 | PNODE_REPARENT_DELETE_CHILDREN = 0x0040, //!< Deletes the Children of the node when This Node is Removed. (Use with care). |
---|
52 | /// FIXME |
---|
53 | PNODE_REPARENT_KEEP_POSITION = 0x0080, //!< Tries to keep the Position if the Node is reparented. |
---|
54 | |
---|
55 | |
---|
56 | // DELETION |
---|
57 | PNODE_PROHIBIT_CHILD_DELETE = 0x0100, //!< Prohibits the Children from being deleted if this Node gets deleted. |
---|
58 | 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 |
---|
59 | PNODE_REPARENT_CHILDREN_ON_REMOVE = 0x0400, //!< Reparents the Children of the Node if the Node gets Removed. |
---|
60 | PNODE_REPARENT_ON_PARENTS_REMOVE = 0x0800, //!< The Node gets Reparented if its Parent gets removed. Child will be reparented according to the Reparenting-Rules. |
---|
61 | |
---|
62 | // VISIBILITY/ACTIVITY |
---|
63 | PNODE_HIDE_CHILDREN_IF_HIDDEN = 0x1000, //!< Prohibits the Children from being drawn if this node isn't visible. (used for Draw)) |
---|
64 | PNODE_HIDE_IF_PARENT_HIDDEN = 0x2000, //!< Prohibits the node from being drawn if the Parent is invisible. |
---|
65 | 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.) |
---|
66 | PNODE_STATIC_NODE = 0x8000, //!< Used for nodes that do not have any moving children, and that do not move. |
---|
67 | |
---|
68 | } PARENT_MODE; |
---|
69 | |
---|
70 | //! The default mode of the translation-binding. |
---|
71 | #define PNODE_PARENT_MODE_DEFAULT PNODE_ALL | \ |
---|
72 | PNODE_REPARENT_KEEP_POSITION |
---|
73 | |
---|
74 | |
---|
75 | //! Patent Node is a Engine to calculate the position of an Object in respect to the position of its parent. |
---|
76 | class PNode : virtual public BaseObject, virtual public Synchronizeable { |
---|
77 | public: |
---|
78 | PNode (PNode* parent = PNode::getNullParent(), long nodeFlags = PNODE_PARENT_MODE_DEFAULT); |
---|
79 | virtual ~PNode (); |
---|
80 | |
---|
81 | virtual void loadParams(const TiXmlElement* root); |
---|
82 | |
---|
83 | void init(); |
---|
84 | |
---|
85 | // ACTIVATION // |
---|
86 | inline void activateNode() { this->bActive = this->bRelCoorChanged = this->bRelDirChanged = true; }; |
---|
87 | inline void deactivateNode() { this->bActive = false; }; |
---|
88 | inline bool getNodeActiveState() { return this->bActive; }; |
---|
89 | |
---|
90 | // POSITION // |
---|
91 | void setRelCoor (const Vector& relCoord); |
---|
92 | void setRelCoor (float x, float y, float z); |
---|
93 | void setRelCoorSoft(const Vector& relCoordSoft, float bias = 1.0); |
---|
94 | void setRelCoorSoft(float x, float y, float z, float bias = 1.0); |
---|
95 | /** @returns the relative position */ |
---|
96 | inline const Vector& getRelCoor () const { return this->prevRelCoordinate; }; |
---|
97 | /** @returns the Relative Coordinate Destination */ |
---|
98 | inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)? *this->toCoordinate : this->relCoordinate; }; |
---|
99 | void setAbsCoor (const Vector& absCoord); |
---|
100 | void setAbsCoor (float x, float y, float z); |
---|
101 | void setAbsCoorSoft(const Vector& absCoordSoft, float bias = 1.0); |
---|
102 | void setAbsCoorSoft(float x, float y, float z, float bias = 1.0); |
---|
103 | /** @returns the absolute position */ |
---|
104 | inline const Vector& getAbsCoor () const { return this->absCoordinate; }; |
---|
105 | /** @returns the absolute X coordinate. */ |
---|
106 | inline float getAbsCoorX() { return this->absCoordinate.x; }; |
---|
107 | /** @returns the absolute Y Coordinate */ |
---|
108 | inline float getAbsCoorY() { return this->absCoordinate.y; }; |
---|
109 | /** @returns the absolute Z Coordinate */ |
---|
110 | inline float getAbsCoorZ() { return this->absCoordinate.z; }; |
---|
111 | |
---|
112 | void shiftCoor (const Vector& shift); |
---|
113 | void shiftCoor (float x, float y, float z) { this->shiftCoor(Vector(x, y, z)); }; |
---|
114 | |
---|
115 | // SPEED // |
---|
116 | /** @returns the Speed of the Node */ |
---|
117 | inline float getSpeed() const { return this->velocity.len(); }; |
---|
118 | /** @returns the Velocity of the Node */ |
---|
119 | inline const Vector& getVelocity() const { return this->velocity; }; |
---|
120 | |
---|
121 | |
---|
122 | // ROTATION // |
---|
123 | void setRelDir (const Quaternion& relDir); |
---|
124 | void setRelDir (float angle, float x, float y, float z); |
---|
125 | void setRelDirSoft(const Quaternion& relDirSoft, float bias = 1.0); |
---|
126 | void setRelDirSoft(float angle, float x, float y, float z, float bias = 1.0); |
---|
127 | /** @returns the relative Direction */ |
---|
128 | inline const Quaternion& getRelDir () const { return this->prevRelDirection; }; |
---|
129 | /** @returns the Relative Directional Destination */ |
---|
130 | inline const Quaternion& getRelDirSoft2D() const { return (this->toDirection)? *this->toDirection : this->relDirection; }; |
---|
131 | /** @returns a Vector pointing into the relative Direction */ |
---|
132 | inline Vector getRelDirV() const { return this->prevRelDirection.apply(Vector(0,1,0)); }; |
---|
133 | void setAbsDir (const Quaternion& absDir); |
---|
134 | void setAbsDir (float angle, float x, float y, float z); |
---|
135 | void setAbsDirSoft(const Quaternion& absDirSoft, float bias = 1.0); |
---|
136 | void setAbsDirSoft(float angle, float x, float y, float z, float bias = 1.0); |
---|
137 | void shiftDir (const Quaternion& shift); |
---|
138 | /** @returns the absolute Direction */ |
---|
139 | inline const Quaternion& getAbsDir () const { return this->absDirection; }; |
---|
140 | /** @returns a Vector pointing into the absolute Direction */ |
---|
141 | inline Vector getAbsDirV() const { return this->absDirection.apply(Vector(0,1,0)); }; |
---|
142 | /** @returns A Vector pointing into the forward direction (X) of the Node */ |
---|
143 | inline Vector getAbsDirX() const { return this->absDirection.apply(Vector(1,0,0)); }; |
---|
144 | /** @returns A Vector pointing into the upward direction (Y) of the Node */ |
---|
145 | inline Vector getAbsDirY() const { return this->absDirection.apply(Vector(0,1,0)); }; |
---|
146 | /** @returns A Vector pointing into the right direction (Z) of the Node */ |
---|
147 | inline Vector getAbsDirZ() const { return this->absDirection.apply(Vector(0,0,1)); }; |
---|
148 | |
---|
149 | |
---|
150 | // PARENTING // |
---|
151 | void addChild (PNode* child); |
---|
152 | void addChild (const std::string& childName); |
---|
153 | void removeChild (PNode* child); |
---|
154 | void removeNode(); |
---|
155 | |
---|
156 | PNode* seekNextAssignedPNode(PNode* node) const; |
---|
157 | |
---|
158 | /** @param parent the new parent of this node */ |
---|
159 | inline void setParent (PNode* parent) { parent->addChild(this); }; |
---|
160 | void setParent (const std::string& parentName); |
---|
161 | /** @returns the parent of this PNode */ |
---|
162 | inline PNode* getParent () const { return this->parent; }; |
---|
163 | /** @returns the List of Children of this PNode */ |
---|
164 | const std::list<PNode*>& getNodesChildren() const { return this->children; }; |
---|
165 | |
---|
166 | void setParentSoft(PNode* parentNode, float bias = 1.0); |
---|
167 | void setParentSoft(const std::string& parentName, float bias = 1.0); |
---|
168 | |
---|
169 | // PARENTING_MODE AND OTHER FLAGS // |
---|
170 | void setParentMode (PARENT_MODE parentMode); |
---|
171 | void setParentMode (const std::string& parentingMode); |
---|
172 | /** @returns the Parenting mode of this node */ |
---|
173 | int getParentMode() const { return 0x000f & this->parentMode; }; |
---|
174 | |
---|
175 | void addNodeFlags(unsigned short nodeFlags); |
---|
176 | void removeNodeFlags(unsigned short nodeFlags); |
---|
177 | |
---|
178 | // NULL_PARENT // |
---|
179 | /** @returns the NullParent, the (main) ROOT of the PNode Tree. If it does not yet exist, it will be created. */ |
---|
180 | static PNode* getNullParent() { return (PNode::nullParent != NULL)? PNode::nullParent : PNode::createNullParent(); }; |
---|
181 | |
---|
182 | // UPDATING // |
---|
183 | void updateNode (float dt); |
---|
184 | |
---|
185 | // DEBUG // |
---|
186 | void countChildNodes(int& nodes) const; |
---|
187 | void debugNode (unsigned int depth = 1, unsigned int level = 0) const; |
---|
188 | void debugDraw(unsigned int depth = 1, float size = 1.0, const Vector& color = Vector(1, 0, 0), unsigned int level = 0) const; |
---|
189 | |
---|
190 | // HELPER_FUNCTIONS // |
---|
191 | static const char* parentingModeToString(int parentingMode); |
---|
192 | static PARENT_MODE stringToParentingMode(const std::string& parentingMode); |
---|
193 | float distance(const PNode* node) const { return (this->getAbsCoor() - node->getAbsCoor()).len(); }; |
---|
194 | |
---|
195 | private: |
---|
196 | /** tells the child that the parent's Coordinate has changed */ |
---|
197 | inline void parentCoorChanged () { this->bRelCoorChanged = true; } |
---|
198 | /** tells the child that the parent's Direction has changed */ |
---|
199 | inline void parentDirChanged () { this->bRelDirChanged = true; } |
---|
200 | public: |
---|
201 | /** @returns the last calculated coordinate */ |
---|
202 | inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; } |
---|
203 | private: |
---|
204 | static PNode* createNullParent(); |
---|
205 | void reparent(); |
---|
206 | bool checkIntegrity(const PNode* checkParent) const; |
---|
207 | void eraseChild(PNode* child); |
---|
208 | |
---|
209 | private: |
---|
210 | bool bRelCoorChanged; //!< If Relative Coordinate has changed since last time we checked |
---|
211 | bool bRelDirChanged; //!< If Relative Direction has changed since last time we checked |
---|
212 | |
---|
213 | Vector relCoordinate; //!< coordinates relative to the parent |
---|
214 | Vector absCoordinate; //!< absolute coordinates in the world ( from (0,0,0) ) |
---|
215 | Quaternion relDirection; //!< direction relative to the parent |
---|
216 | Quaternion absDirection; //!< absolute direvtion in the world ( from (0,0,1) ) |
---|
217 | |
---|
218 | Vector prevRelCoordinate; //!< The last Relative Coordinate from the last update-Cycle. |
---|
219 | Vector lastAbsCoordinate; //!< this is used for speedcalculation, it stores the last coordinate |
---|
220 | Quaternion prevRelDirection; //!< The last Relative Direciton from the last update-Cycle. |
---|
221 | // Quaternion lastAbsDirection; |
---|
222 | |
---|
223 | Vector velocity; //!< Saves the velocity. |
---|
224 | |
---|
225 | Vector* toCoordinate; //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft) |
---|
226 | Quaternion* toDirection; //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft) |
---|
227 | float bias; //!< how fast to iterate to the given position (default is 1) |
---|
228 | |
---|
229 | PNode* parent; //!< a pointer to the parent node. |
---|
230 | std::list<PNode*> children; //!< list of the children of this PNode. |
---|
231 | |
---|
232 | bool bActive; //!< If the Node is Active (for cutting off the rest of the tree in update). |
---|
233 | unsigned short parentMode; //!< the mode of the binding |
---|
234 | |
---|
235 | |
---|
236 | static PNode* nullParent; //!< The ROOT of the main PNode Tree. |
---|
237 | |
---|
238 | private: |
---|
239 | float coorx; |
---|
240 | float coory; |
---|
241 | float coorz; |
---|
242 | |
---|
243 | float rotw; |
---|
244 | float rotx; |
---|
245 | float roty; |
---|
246 | float rotz; |
---|
247 | |
---|
248 | private: |
---|
249 | int relCoordinate_handle; |
---|
250 | int relDirection_handle; |
---|
251 | Vector relCoordinate_write; |
---|
252 | Quaternion relDirection_write; |
---|
253 | |
---|
254 | public: |
---|
255 | virtual void varChangeHandler( std::list<int> & id ); |
---|
256 | }; |
---|
257 | |
---|
258 | #endif /* _P_NODE_H */ |
---|