| 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 | #include <list> |
|---|
| 24 | |
|---|
| 25 | // FORWARD DECLARATION |
|---|
| 26 | class TiXmlElement; |
|---|
| 27 | template<class T> class tList; |
|---|
| 28 | |
|---|
| 29 | #define PNODE_ITERATION_DELTA .001 |
|---|
| 30 | //! Parental linkage modes |
|---|
| 31 | typedef enum |
|---|
| 32 | { |
|---|
| 33 | // PARENTAL FOLLOWING |
|---|
| 34 | PNODE_LOCAL_ROTATE = 0x00000001, //!< Rotates all the children around their centers. |
|---|
| 35 | PNODE_ROTATE_MOVEMENT = 0x00000002, //!< Moves all the children around the center of their parent, without the rotation around their own centers. |
|---|
| 36 | |
|---|
| 37 | PNODE_MOVEMENT = 0x00000004, //!< Moves all children along with the parent. |
|---|
| 38 | // special linkage modes |
|---|
| 39 | PNODE_ALL = 0x00000003, //!< Moves all children around the center of their parent, and also rotates their centers |
|---|
| 40 | PNODE_ROTATE_AND_MOVE = 0x00000005, //!< 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 | PNODE_INACTIVE_NODE = 0x00000010, //!< If the node is Active (if not, it and its child wont be updated). |
|---|
| 44 | |
|---|
| 45 | // REPARENTING |
|---|
| 46 | PNODE_REPARENT_TO_NULLPARENT = 0x00000100, |
|---|
| 47 | PNODE_REPARENT_KEEP_POSITION = 0x00000200, |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | // DELETION |
|---|
| 51 | PNODE_PROHIBIT_CHILD_DELETE = 0x00010000, |
|---|
| 52 | PNODE_PROHIBIT_DELETE_WITH_PARENT = 0x00020000, |
|---|
| 53 | |
|---|
| 54 | } PARENT_MODE; |
|---|
| 55 | |
|---|
| 56 | //! The default mode of the translation-binding. |
|---|
| 57 | #define PNODE_PARENT_MODE_DEFAULT PNODE_ALL | PNODE_REPARENT_KEEP_POSITION |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | //! Patent Node is a Engine to calculate the position of an Object in respect to the position of its parent. |
|---|
| 61 | class PNode : virtual public BaseObject { |
|---|
| 62 | |
|---|
| 63 | public: |
|---|
| 64 | PNode (); |
|---|
| 65 | PNode(const TiXmlElement* root); |
|---|
| 66 | PNode (const Vector& absCoor, PNode* pNode); |
|---|
| 67 | virtual ~PNode (); |
|---|
| 68 | |
|---|
| 69 | void loadParams(const TiXmlElement* root); |
|---|
| 70 | |
|---|
| 71 | void setRelCoor (const Vector& relCoord); |
|---|
| 72 | void setRelCoor (float x, float y, float z); |
|---|
| 73 | void setRelCoorSoft(const Vector& relCoordSoft, float bias = 1.0); |
|---|
| 74 | void setRelCoorSoft(float x, float y, float z, float bias = 1.0); |
|---|
| 75 | /** @returns the relative position */ |
|---|
| 76 | inline const Vector& getRelCoor () const { return this->prevRelCoordinate; }; |
|---|
| 77 | /** @returns the Relative Coordinate Destination */ |
|---|
| 78 | inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)? *this->toCoordinate : this->relCoordinate; }; |
|---|
| 79 | void setAbsCoor (const Vector& absCoord); |
|---|
| 80 | void setAbsCoor (float x, float y, float z); |
|---|
| 81 | void setAbsCoorSoft(const Vector& absCoordSoft, float bias = 1.0); |
|---|
| 82 | void setAbsCoorSoft(float x, float y, float z, float bias = 1.0); |
|---|
| 83 | /** @returns the absolute position */ |
|---|
| 84 | inline const Vector& getAbsCoor () const { return this->absCoordinate; }; |
|---|
| 85 | void shiftCoor (const Vector& shift); |
|---|
| 86 | void shiftCoor (float x, float y, float z) { this->shiftCoor(Vector(x, y, z)); }; |
|---|
| 87 | |
|---|
| 88 | void setRelDir (const Quaternion& relDir); |
|---|
| 89 | void setRelDir (float x, float y, float z); |
|---|
| 90 | void setRelDirSoft(const Quaternion& relDirSoft, float bias = 1.0); |
|---|
| 91 | void setRelDirSoft(float x, float y, float z, float bias = 1.0); |
|---|
| 92 | /** @returns the relative Direction */ |
|---|
| 93 | inline const Quaternion& getRelDir () const { return this->prevRelDirection; }; |
|---|
| 94 | /** @returns the Relative Directional Destination */ |
|---|
| 95 | inline const Quaternion& getRelDirSoft2D() const { return (this->toDirection)? *this->toDirection : this->relDirection; }; |
|---|
| 96 | /** @returns a Vector pointing into the relative Direction */ |
|---|
| 97 | inline Vector getRelDirV() const { return this->prevRelDirection.apply(Vector(0,1,0)); }; |
|---|
| 98 | void setAbsDir (const Quaternion& absDir); |
|---|
| 99 | void setAbsDir (float x, float y, float z); |
|---|
| 100 | void setAbsDirSoft(const Quaternion& absDirSoft, float bias = 1.0); |
|---|
| 101 | void setAbsDirSoft(float x, float y, float z, float bias = 1.0); |
|---|
| 102 | void shiftDir (const Quaternion& shift); |
|---|
| 103 | /** @returns the absolute Direction */ |
|---|
| 104 | inline const Quaternion& getAbsDir () const { return this->absDirection; }; |
|---|
| 105 | /** @returns a Vector pointing into the absolute Direction */ |
|---|
| 106 | inline Vector getAbsDirV() const { return this->absDirection.apply(Vector(0,1,0)); }; |
|---|
| 107 | /** @returns A Vector pointing into the forward direction (X) of the Node */ |
|---|
| 108 | inline Vector getAbsDirX() const { return this->absDirection.apply(Vector(1,0,0)); }; |
|---|
| 109 | /** @returns A Vector pointing into the upward direction (Y) of the Node */ |
|---|
| 110 | inline Vector getAbsDirY() const { return this->absDirection.apply(Vector(0,1,0)); }; |
|---|
| 111 | /** @returns A Vector pointing into the right direction (Z) of the Node */ |
|---|
| 112 | inline Vector getAbsDirZ() const { return this->absDirection.apply(Vector(0,0,1)); }; |
|---|
| 113 | |
|---|
| 114 | /** @returns the Speed of the Node */ |
|---|
| 115 | inline float getSpeed() const { return this->velocity.len(); }; |
|---|
| 116 | /** @returns the Velocity of the Node */ |
|---|
| 117 | inline const Vector& getVelocity() const { return this->velocity; }; |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | void addChild (PNode* child); |
|---|
| 121 | void addChild (const char* childName); |
|---|
| 122 | void removeChild (PNode* child); |
|---|
| 123 | void removeNode(); |
|---|
| 124 | |
|---|
| 125 | void setParent (PNode* parent); |
|---|
| 126 | void setParent (const char* parentName); |
|---|
| 127 | /** @returns the parent of this PNode */ |
|---|
| 128 | inline PNode* getParent () const { return this->parent; }; |
|---|
| 129 | /** @returns the List of Children of this PNode */ |
|---|
| 130 | // inline const tList<PNode>* getChildren() const { return this->children; }; |
|---|
| 131 | |
|---|
| 132 | void setParentSoft(PNode* parentNode, float bias = 1.0); |
|---|
| 133 | void setParentSoft(const char* parentName, float bias = 1.0); |
|---|
| 134 | |
|---|
| 135 | /** @param parentMode sets the parentingMode of this Node */ |
|---|
| 136 | void setParentMode (PARENT_MODE parentMode) { this->parentMode = parentMode; }; |
|---|
| 137 | void setParentMode (const char* parentingMode); |
|---|
| 138 | /** @returns the Parenting mode of this node */ |
|---|
| 139 | int getParentMode() const { return this->parentMode; }; |
|---|
| 140 | |
|---|
| 141 | void updateNode (float dt); |
|---|
| 142 | |
|---|
| 143 | void countChildNodes(int& nodes) const; |
|---|
| 144 | void debugNode (unsigned int depth = 1, unsigned int level = 0) const; |
|---|
| 145 | void debugDraw(unsigned int depth = 1, float size = 1.0, const Vector& color = Vector(1, 0, 0), unsigned int level = 0) const; |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | // helper functions // |
|---|
| 149 | static const char* parentingModeToChar(int parentingMode); |
|---|
| 150 | static PARENT_MODE charToParentingMode(const char* parentingMode); |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | private: |
|---|
| 154 | void init(PNode* parent); |
|---|
| 155 | /** tells the child that the parent's Coordinate has changed */ |
|---|
| 156 | inline void parentCoorChanged () { this->bRelCoorChanged = true; } |
|---|
| 157 | /** tells the child that the parent's Direction has changed */ |
|---|
| 158 | inline void parentDirChanged () { this->bRelDirChanged = true; } |
|---|
| 159 | /** @returns the last calculated coordinate */ |
|---|
| 160 | inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | private: |
|---|
| 164 | bool bRelCoorChanged; //!< If Relative Coordinate has changed since last time we checked |
|---|
| 165 | bool bRelDirChanged; //!< If Relative Direction has changed since last time we checked |
|---|
| 166 | |
|---|
| 167 | Vector relCoordinate; //!< coordinates relative to the parent |
|---|
| 168 | Vector absCoordinate; //!< absolute coordinates in the world ( from (0,0,0) ) |
|---|
| 169 | Quaternion relDirection; //!< direction relative to the parent |
|---|
| 170 | Quaternion absDirection; //!< absolute direvtion in the world ( from (0,0,1) ) |
|---|
| 171 | |
|---|
| 172 | Vector prevRelCoordinate; //!< The last Relative Coordinate from the last update-Cycle. |
|---|
| 173 | Vector lastAbsCoordinate; //!< this is used for speedcalculation, it stores the last coordinate |
|---|
| 174 | Quaternion prevRelDirection; //!< The last Relative Direciton from the last update-Cycle. |
|---|
| 175 | // Quaternion lastAbsDirection; |
|---|
| 176 | |
|---|
| 177 | Vector velocity; //!< Saves the velocity. |
|---|
| 178 | |
|---|
| 179 | Vector* toCoordinate; //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft) |
|---|
| 180 | Quaternion* toDirection; //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft) |
|---|
| 181 | float bias; //!< how fast to iterate to the given position (default is 1) |
|---|
| 182 | |
|---|
| 183 | PNode* parent; //!< a pointer to the parent node |
|---|
| 184 | std::list<PNode*> children; //!< list of the children of this PNode |
|---|
| 185 | |
|---|
| 186 | unsigned int parentMode; //!< the mode of the binding |
|---|
| 187 | }; |
|---|
| 188 | |
|---|
| 189 | #endif /* _P_NODE_H */ |
|---|
| 190 | |
|---|