1 | /*! |
---|
2 | * @file p_node.h |
---|
3 | * @brief Definition of a 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 bDirChanged is true (so changed) AND timeStamp != now |
---|
13 | * o bCoorChanged is true (so moved) AND timeStamp != now |
---|
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 "vector.h" |
---|
23 | |
---|
24 | // FORWARD DEFINITION \\ |
---|
25 | class TiXmlElement; |
---|
26 | template<class T> class tList; |
---|
27 | |
---|
28 | #define PNODE_ITERATION_DELTA .001 |
---|
29 | //! Parental linkage modes |
---|
30 | typedef enum |
---|
31 | { |
---|
32 | PNODE_LOCAL_ROTATE = 1, //!< Rotates all the children around their centers. |
---|
33 | PNODE_ROTATE_MOVEMENT = 2, //!< Moves all the children around the center of their parent, without the rotation around their own centers. |
---|
34 | |
---|
35 | PNODE_MOVEMENT = 4, //!< Moves all children along with the parent. |
---|
36 | // special linkage modes |
---|
37 | PNODE_ALL = 3, //!< Moves all children around the center of their parent, and also rotates their centers |
---|
38 | PNODE_ROTATE_AND_MOVE = 5 //!< Rotates all children around their axis, and moves them as the Parent Moves, but does not rotate around the center of their parent. |
---|
39 | |
---|
40 | } PARENT_MODE; |
---|
41 | |
---|
42 | //! The default mode of the translation-binding. |
---|
43 | #define PNODE_PARENT_MODE_DEFAULT PNODE_ALL |
---|
44 | |
---|
45 | |
---|
46 | //! Patent Node is a Engine to calculate the position of an Object in respect to the position of its parent. |
---|
47 | class PNode : virtual public BaseObject { |
---|
48 | |
---|
49 | public: |
---|
50 | PNode (); |
---|
51 | PNode(const TiXmlElement* root); |
---|
52 | PNode (const Vector& absCoor, PNode* pNode); |
---|
53 | virtual ~PNode (); |
---|
54 | |
---|
55 | void loadParams(const TiXmlElement* root); |
---|
56 | |
---|
57 | void setRelCoor (const Vector& relCoord); |
---|
58 | void setRelCoor (float x, float y, float z); |
---|
59 | void setRelCoorSoft(const Vector& relCoordSoft, float bias = 1.0); |
---|
60 | void setRelCoorSoft(float x, float y, float z, float bias = 1.0); |
---|
61 | /** @returns the relative position */ |
---|
62 | inline const Vector& getRelCoor () const { return this->prevRelCoordinate; }; |
---|
63 | /** @returns the Relative Coordinate Destination */ |
---|
64 | inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)?*this->toCoordinate:this->relCoordinate; }; |
---|
65 | void setAbsCoor (const Vector& absCoord); |
---|
66 | void setAbsCoor (float x, float y, float z); |
---|
67 | /** @returns the absolute position */ |
---|
68 | inline const Vector& getAbsCoor () const { return this->absCoordinate; }; |
---|
69 | void shiftCoor (const Vector& shift); |
---|
70 | |
---|
71 | void setRelDir (const Quaternion& relDir); |
---|
72 | void setRelDir (float x, float y, float z); |
---|
73 | void setRelDirSoft(const Quaternion& relDirSoft, float bias = 1.0); |
---|
74 | void setRelDirSoft(float x, float y, float z, float bias = 1.0); |
---|
75 | /** @returns the relative Direction */ |
---|
76 | inline const Quaternion& getRelDir () const { return this->prevRelDirection; }; |
---|
77 | /** @returns the Relative Directional Destination */ |
---|
78 | inline const Quaternion& getRelDirSoft2D() const { return (this->toDirection)?*this->toDirection:this->relDirection; }; |
---|
79 | /** @returns a Vector pointing into the relative Direction */ |
---|
80 | inline Vector getRelDirV() const { return this->prevRelDirection.apply(Vector(0,1,0)); }; |
---|
81 | void setAbsDir (const Quaternion& absDir); |
---|
82 | void setAbsDir (float x, float y, float z); |
---|
83 | /** @returns the absolute Direction */ |
---|
84 | inline const Quaternion& getAbsDir () const { return this->absDirection; }; |
---|
85 | /** @returns a Vector pointing into the absolute Direction */ |
---|
86 | inline Vector getAbsDirV() const { return this->absDirection.apply(Vector(0,1,0)); }; |
---|
87 | void shiftDir (const Quaternion& shift); |
---|
88 | |
---|
89 | /** @returns the Speed of the Node */ |
---|
90 | inline float getSpeed() const { return this->velocity.len(); }; |
---|
91 | /** @returns the Velocity of the Node */ |
---|
92 | inline const Vector& getVelocity() const { return this->velocity; }; |
---|
93 | |
---|
94 | |
---|
95 | void addChild (PNode* child); |
---|
96 | void addChild (const char* childName); |
---|
97 | void removeChild (PNode* child); |
---|
98 | void remove(); |
---|
99 | |
---|
100 | void setParent (PNode* parent); |
---|
101 | void setParent (const char* parentName); |
---|
102 | /** @returns the parent of this PNode */ |
---|
103 | inline PNode* getParent () const { return this->parent; }; |
---|
104 | /** @returns the List of Children of this PNode */ |
---|
105 | inline const tList<PNode>* getChildren() const { return this->children; }; |
---|
106 | |
---|
107 | void setParentSoft(PNode* parentNode, float bias = 1.0); |
---|
108 | void setParentSoft(const char* parentName, float bias = 1.0); |
---|
109 | |
---|
110 | /** @param parentMode sets the parentingMode of this Node */ |
---|
111 | void setParentMode (PARENT_MODE parentMode) { this->parentMode = parentMode; }; |
---|
112 | void setParentMode (const char* parentingMode); |
---|
113 | /** @returns the Parenting mode of this node */ |
---|
114 | int getParentMode() const { return this->parentMode; }; |
---|
115 | |
---|
116 | void update (float dt); |
---|
117 | |
---|
118 | void debug (unsigned int depth = 1, unsigned int level = 0) const; |
---|
119 | void debugDraw(unsigned int depth = 1, float size = 1.0, const Vector& color = Vector(1, 0, 0), unsigned int level = 0) const; |
---|
120 | |
---|
121 | |
---|
122 | // helper functions // |
---|
123 | static const char* parentingModeToChar(int parentingMode); |
---|
124 | static PARENT_MODE charToParentingMode(const char* parentingMode); |
---|
125 | private: |
---|
126 | void init(PNode* parent); |
---|
127 | /** tells the child that the parent's Coordinate has changed */ |
---|
128 | inline void parentCoorChanged () { this->bRelCoorChanged = true; } |
---|
129 | /** tells the child that the parent's Direction has changed */ |
---|
130 | inline void parentDirChanged () { this->bRelDirChanged = true; } |
---|
131 | /** @returns the last calculated coordinate */ |
---|
132 | inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; } |
---|
133 | |
---|
134 | |
---|
135 | private: |
---|
136 | bool bRelCoorChanged; //!< If Relative Coordinate has changed since last time we checked |
---|
137 | bool bRelDirChanged; //!< If Relative Direction has changed since last time we checked |
---|
138 | |
---|
139 | Vector relCoordinate; //!< coordinates relative to the parent |
---|
140 | Vector absCoordinate; //!< absolute coordinates in the world ( from (0,0,0) ) |
---|
141 | Quaternion relDirection; //!< direction relative to the parent |
---|
142 | Quaternion absDirection; //!< absolute direvtion in the world ( from (0,0,1) ) |
---|
143 | |
---|
144 | Vector prevRelCoordinate; //!< The last Relative Coordinate from the last update-Cycle. |
---|
145 | Vector lastAbsCoordinate; //!< this is used for speedcalculation, it stores the last coordinate |
---|
146 | Quaternion prevRelDirection; //!< The last Relative Direciton from the last update-Cycle. |
---|
147 | // Quaternion lastAbsDirection; |
---|
148 | |
---|
149 | Vector velocity; //!< Saves the velocity. |
---|
150 | |
---|
151 | Vector* toCoordinate; //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft) |
---|
152 | Quaternion* toDirection; //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft) |
---|
153 | float bias; //!< how fast to iterate to the given position (default is 1) |
---|
154 | |
---|
155 | PNode* parent; //!< a pointer to the parent node |
---|
156 | tList<PNode>* children; //!< list of the children of this PNode |
---|
157 | |
---|
158 | unsigned int parentMode; //!< the mode of the binding |
---|
159 | }; |
---|
160 | |
---|
161 | #endif /* _P_NODE_H */ |
---|
162 | |
---|