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