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