[4570] | 1 | /*! |
---|
[5391] | 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 | */ |
---|
[3246] | 16 | |
---|
| 17 | |
---|
| 18 | #ifndef _P_NODE_H |
---|
| 19 | #define _P_NODE_H |
---|
| 20 | |
---|
[3543] | 21 | #include "base_object.h" |
---|
[3804] | 22 | #include "vector.h" |
---|
[3246] | 23 | |
---|
[5405] | 24 | // FORWARD DECLARATION |
---|
[4436] | 25 | class TiXmlElement; |
---|
[3607] | 26 | template<class T> class tList; |
---|
[3246] | 27 | |
---|
[5006] | 28 | #define PNODE_ITERATION_DELTA .001 |
---|
[4765] | 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. |
---|
[3565] | 36 | // special linkage modes |
---|
[4765] | 37 | PNODE_ALL = 3, //!< Moves all children around the center of their parent, and also rotates their centers |
---|
[4991] | 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. |
---|
[3565] | 39 | |
---|
[4765] | 40 | } PARENT_MODE; |
---|
| 41 | |
---|
[3450] | 42 | //! The default mode of the translation-binding. |
---|
[5209] | 43 | #define PNODE_PARENT_MODE_DEFAULT PNODE_ALL |
---|
[3246] | 44 | |
---|
[4440] | 45 | |
---|
[3450] | 46 | //! Patent Node is a Engine to calculate the position of an Object in respect to the position of its parent. |
---|
[4382] | 47 | class PNode : virtual public BaseObject { |
---|
[3365] | 48 | |
---|
[3246] | 49 | public: |
---|
| 50 | PNode (); |
---|
[4436] | 51 | PNode(const TiXmlElement* root); |
---|
[4993] | 52 | PNode (const Vector& absCoor, PNode* pNode); |
---|
[3365] | 53 | virtual ~PNode (); |
---|
[3246] | 54 | |
---|
[4436] | 55 | void loadParams(const TiXmlElement* root); |
---|
| 56 | |
---|
[3810] | 57 | void setRelCoor (const Vector& relCoord); |
---|
[4610] | 58 | void setRelCoor (float x, float y, float z); |
---|
[4992] | 59 | void setRelCoorSoft(const Vector& relCoordSoft, float bias = 1.0); |
---|
| 60 | void setRelCoorSoft(float x, float y, float z, float bias = 1.0); |
---|
[4836] | 61 | /** @returns the relative position */ |
---|
[5050] | 62 | inline const Vector& getRelCoor () const { return this->prevRelCoordinate; }; |
---|
[5113] | 63 | /** @returns the Relative Coordinate Destination */ |
---|
| 64 | inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)?*this->toCoordinate:this->relCoordinate; }; |
---|
[3809] | 65 | void setAbsCoor (const Vector& absCoord); |
---|
[4610] | 66 | void setAbsCoor (float x, float y, float z); |
---|
[5406] | 67 | void setAbsCoorSoft(const Vector& absCoordSoft, float bias = 1.0); |
---|
| 68 | void setAbsCoorSoft(float x, float y, float z, float bias = 1.0); |
---|
[4836] | 69 | /** @returns the absolute position */ |
---|
[4372] | 70 | inline const Vector& getAbsCoor () const { return this->absCoordinate; }; |
---|
[3809] | 71 | void shiftCoor (const Vector& shift); |
---|
[5750] | 72 | void shiftCoor (float x, float y, float z) { this->shiftCoor(Vector(x, y, z)); }; |
---|
[3246] | 73 | |
---|
[3810] | 74 | void setRelDir (const Quaternion& relDir); |
---|
[4771] | 75 | void setRelDir (float x, float y, float z); |
---|
[4992] | 76 | void setRelDirSoft(const Quaternion& relDirSoft, float bias = 1.0); |
---|
| 77 | void setRelDirSoft(float x, float y, float z, float bias = 1.0); |
---|
[4836] | 78 | /** @returns the relative Direction */ |
---|
[5050] | 79 | inline const Quaternion& getRelDir () const { return this->prevRelDirection; }; |
---|
[5113] | 80 | /** @returns the Relative Directional Destination */ |
---|
| 81 | inline const Quaternion& getRelDirSoft2D() const { return (this->toDirection)?*this->toDirection:this->relDirection; }; |
---|
[4836] | 82 | /** @returns a Vector pointing into the relative Direction */ |
---|
[5050] | 83 | inline Vector getRelDirV() const { return this->prevRelDirection.apply(Vector(0,1,0)); }; |
---|
[3810] | 84 | void setAbsDir (const Quaternion& absDir); |
---|
[4771] | 85 | void setAbsDir (float x, float y, float z); |
---|
[5414] | 86 | void setAbsDirSoft(const Quaternion& absDirSoft, float bias = 1.0); |
---|
| 87 | void setAbsDirSoft(float x, float y, float z, float bias = 1.0); |
---|
[4836] | 88 | /** @returns the absolute Direction */ |
---|
[4372] | 89 | inline const Quaternion& getAbsDir () const { return this->absDirection; }; |
---|
[4836] | 90 | /** @returns a Vector pointing into the absolute Direction */ |
---|
[4372] | 91 | inline Vector getAbsDirV() const { return this->absDirection.apply(Vector(0,1,0)); }; |
---|
[3802] | 92 | void shiftDir (const Quaternion& shift); |
---|
[3246] | 93 | |
---|
[4836] | 94 | /** @returns the Speed of the Node */ |
---|
[4993] | 95 | inline float getSpeed() const { return this->velocity.len(); }; |
---|
[4836] | 96 | /** @returns the Velocity of the Node */ |
---|
[4993] | 97 | inline const Vector& getVelocity() const { return this->velocity; }; |
---|
[3644] | 98 | |
---|
[4440] | 99 | |
---|
[5382] | 100 | void addChild (PNode* child); |
---|
[4765] | 101 | void addChild (const char* childName); |
---|
[4993] | 102 | void removeChild (PNode* child); |
---|
[3537] | 103 | void remove(); |
---|
| 104 | |
---|
[3535] | 105 | void setParent (PNode* parent); |
---|
[4987] | 106 | void setParent (const char* parentName); |
---|
[4966] | 107 | /** @returns the parent of this PNode */ |
---|
[5387] | 108 | inline PNode* getParent () const { return this->parent; }; |
---|
| 109 | /** @returns the List of Children of this PNode */ |
---|
| 110 | inline const tList<PNode>* getChildren() const { return this->children; }; |
---|
[4761] | 111 | |
---|
[5382] | 112 | void setParentSoft(PNode* parentNode, float bias = 1.0); |
---|
| 113 | void setParentSoft(const char* parentName, float bias = 1.0); |
---|
[4987] | 114 | |
---|
[4993] | 115 | /** @param parentMode sets the parentingMode of this Node */ |
---|
| 116 | void setParentMode (PARENT_MODE parentMode) { this->parentMode = parentMode; }; |
---|
[4765] | 117 | void setParentMode (const char* parentingMode); |
---|
[4836] | 118 | /** @returns the Parenting mode of this node */ |
---|
[4444] | 119 | int getParentMode() const { return this->parentMode; }; |
---|
[3246] | 120 | |
---|
[4440] | 121 | void update (float dt); |
---|
[3246] | 122 | |
---|
[4574] | 123 | void debug (unsigned int depth = 1, unsigned int level = 0) const; |
---|
[5383] | 124 | void debugDraw(unsigned int depth = 1, float size = 1.0, const Vector& color = Vector(1, 0, 0), unsigned int level = 0) const; |
---|
[3365] | 125 | |
---|
[4993] | 126 | |
---|
| 127 | // helper functions // |
---|
| 128 | static const char* parentingModeToChar(int parentingMode); |
---|
| 129 | static PARENT_MODE charToParentingMode(const char* parentingMode); |
---|
[5750] | 130 | |
---|
| 131 | |
---|
[4440] | 132 | private: |
---|
| 133 | void init(PNode* parent); |
---|
[4987] | 134 | /** tells the child that the parent's Coordinate has changed */ |
---|
[4440] | 135 | inline void parentCoorChanged () { this->bRelCoorChanged = true; } |
---|
[4987] | 136 | /** tells the child that the parent's Direction has changed */ |
---|
[4440] | 137 | inline void parentDirChanged () { this->bRelDirChanged = true; } |
---|
[4836] | 138 | /** @returns the last calculated coordinate */ |
---|
[4993] | 139 | inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; } |
---|
[3246] | 140 | |
---|
[3537] | 141 | |
---|
[3644] | 142 | private: |
---|
[4440] | 143 | bool bRelCoorChanged; //!< If Relative Coordinate has changed since last time we checked |
---|
| 144 | bool bRelDirChanged; //!< If Relative Direction has changed since last time we checked |
---|
[3644] | 145 | |
---|
[4440] | 146 | Vector relCoordinate; //!< coordinates relative to the parent |
---|
| 147 | Vector absCoordinate; //!< absolute coordinates in the world ( from (0,0,0) ) |
---|
| 148 | Quaternion relDirection; //!< direction relative to the parent |
---|
| 149 | Quaternion absDirection; //!< absolute direvtion in the world ( from (0,0,1) ) |
---|
| 150 | |
---|
[5050] | 151 | Vector prevRelCoordinate; //!< The last Relative Coordinate from the last update-Cycle. |
---|
[4440] | 152 | Vector lastAbsCoordinate; //!< this is used for speedcalculation, it stores the last coordinate |
---|
[5050] | 153 | Quaternion prevRelDirection; //!< The last Relative Direciton from the last update-Cycle. |
---|
| 154 | // Quaternion lastAbsDirection; |
---|
[4440] | 155 | |
---|
[5050] | 156 | Vector velocity; //!< Saves the velocity. |
---|
[4987] | 157 | |
---|
[5382] | 158 | Vector* toCoordinate; //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft) |
---|
| 159 | Quaternion* toDirection; //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft) |
---|
[4992] | 160 | float bias; //!< how fast to iterate to the given position (default is 1) |
---|
[4987] | 161 | |
---|
[4440] | 162 | PNode* parent; //!< a pointer to the parent node |
---|
[4987] | 163 | tList<PNode>* children; //!< list of the children of this PNode |
---|
[4440] | 164 | |
---|
[4444] | 165 | unsigned int parentMode; //!< the mode of the binding |
---|
[3246] | 166 | }; |
---|
| 167 | |
---|
| 168 | #endif /* _P_NODE_H */ |
---|
[4570] | 169 | |
---|