[4570] | 1 | /*! |
---|
[5391] | 2 | * @file p_node.h |
---|
[6042] | 3 | * @brief Definition of THE Parenting Node |
---|
[5391] | 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 |
---|
[6042] | 12 | * o bRelCoorChanged is true (so moved) |
---|
| 13 | * o bRelDirChanged is true (so changed) |
---|
[5391] | 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" |
---|
[6341] | 22 | #include "stdincl.h" |
---|
[6042] | 23 | |
---|
[3804] | 24 | #include "vector.h" |
---|
[5770] | 25 | #include <list> |
---|
[3246] | 26 | |
---|
[5405] | 27 | // FORWARD DECLARATION |
---|
[4436] | 28 | class TiXmlElement; |
---|
[3246] | 29 | |
---|
[5006] | 30 | #define PNODE_ITERATION_DELTA .001 |
---|
[6054] | 31 | |
---|
[4765] | 32 | //! Parental linkage modes |
---|
| 33 | typedef enum |
---|
| 34 | { |
---|
[5769] | 35 | // PARENTAL FOLLOWING |
---|
[6042] | 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. |
---|
[4765] | 38 | |
---|
[6042] | 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. |
---|
[3565] | 43 | |
---|
[5769] | 44 | |
---|
| 45 | // REPARENTING |
---|
[6078] | 46 | PNODE_REPARENT_TO_NULL = 0x0010, //!< Reparents to the Null, if the Parent is Removed. Meaning the Node wont have a parent anymore. |
---|
[6048] | 47 | PNODE_REPARENT_TO_PARENTS_PARENT = 0x0020, //!< Reparents the Node to the parents (old) parent it the parent gets removed. |
---|
[6078] | 48 | ///////////////////////////////////////////// // ELSE: Reparents to the NullParent. |
---|
[6042] | 49 | PNODE_REPARENT_DELETE_CHILDREN = 0x0040, //!< Deletes the Children of the node when This Node is Removed. (Use with care). |
---|
[6054] | 50 | /// FIXME |
---|
| 51 | PNODE_REPARENT_KEEP_POSITION = 0x0080, //!< Tries to keep the Position if the Node is reparented. |
---|
[5769] | 52 | |
---|
| 53 | |
---|
| 54 | // DELETION |
---|
[6042] | 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 |
---|
[6054] | 57 | PNODE_REPARENT_CHILDREN_ON_REMOVE = 0x0400, //!< Reparents the Children of the Node if the Node gets Removed. |
---|
[6078] | 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. |
---|
[5769] | 59 | |
---|
[6042] | 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 | |
---|
[4765] | 66 | } PARENT_MODE; |
---|
| 67 | |
---|
[3450] | 68 | //! The default mode of the translation-binding. |
---|
[6042] | 69 | #define PNODE_PARENT_MODE_DEFAULT PNODE_ALL | \ |
---|
| 70 | PNODE_REPARENT_KEEP_POSITION |
---|
[3246] | 71 | |
---|
[4440] | 72 | |
---|
[3450] | 73 | //! Patent Node is a Engine to calculate the position of an Object in respect to the position of its parent. |
---|
[4382] | 74 | class PNode : virtual public BaseObject { |
---|
[3246] | 75 | public: |
---|
[6078] | 76 | PNode (PNode* parent = PNode::getNullParent(), long nodeFlags = PNODE_PARENT_MODE_DEFAULT); |
---|
[3365] | 77 | virtual ~PNode (); |
---|
[3246] | 78 | |
---|
[4436] | 79 | void loadParams(const TiXmlElement* root); |
---|
| 80 | |
---|
[6424] | 81 | void init(); |
---|
| 82 | |
---|
[6078] | 83 | // ACTIVATION // |
---|
[6142] | 84 | inline void activateNode() { this->bActive = this->bRelCoorChanged = this->bRelDirChanged = true; }; |
---|
[6054] | 85 | inline void deactivateNode() { this->bActive = false; }; |
---|
| 86 | inline bool getNodeActiveState() { return this->bActive; }; |
---|
| 87 | |
---|
[6078] | 88 | // POSITION // |
---|
[3810] | 89 | void setRelCoor (const Vector& relCoord); |
---|
[4610] | 90 | void setRelCoor (float x, float y, float z); |
---|
[4992] | 91 | void setRelCoorSoft(const Vector& relCoordSoft, float bias = 1.0); |
---|
| 92 | void setRelCoorSoft(float x, float y, float z, float bias = 1.0); |
---|
[4836] | 93 | /** @returns the relative position */ |
---|
[5050] | 94 | inline const Vector& getRelCoor () const { return this->prevRelCoordinate; }; |
---|
[5113] | 95 | /** @returns the Relative Coordinate Destination */ |
---|
[5915] | 96 | inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)? *this->toCoordinate : this->relCoordinate; }; |
---|
[3809] | 97 | void setAbsCoor (const Vector& absCoord); |
---|
[4610] | 98 | void setAbsCoor (float x, float y, float z); |
---|
[5406] | 99 | void setAbsCoorSoft(const Vector& absCoordSoft, float bias = 1.0); |
---|
| 100 | void setAbsCoorSoft(float x, float y, float z, float bias = 1.0); |
---|
[4836] | 101 | /** @returns the absolute position */ |
---|
[4372] | 102 | inline const Vector& getAbsCoor () const { return this->absCoordinate; }; |
---|
[3809] | 103 | void shiftCoor (const Vector& shift); |
---|
[5750] | 104 | void shiftCoor (float x, float y, float z) { this->shiftCoor(Vector(x, y, z)); }; |
---|
[3246] | 105 | |
---|
[6078] | 106 | // SPEED // |
---|
| 107 | /** @returns the Speed of the Node */ |
---|
| 108 | inline float getSpeed() const { return this->velocity.len(); }; |
---|
| 109 | /** @returns the Velocity of the Node */ |
---|
| 110 | inline const Vector& getVelocity() const { return this->velocity; }; |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | // ROTATION // |
---|
[3810] | 114 | void setRelDir (const Quaternion& relDir); |
---|
[4771] | 115 | void setRelDir (float x, float y, float z); |
---|
[4992] | 116 | void setRelDirSoft(const Quaternion& relDirSoft, float bias = 1.0); |
---|
| 117 | void setRelDirSoft(float x, float y, float z, float bias = 1.0); |
---|
[4836] | 118 | /** @returns the relative Direction */ |
---|
[5050] | 119 | inline const Quaternion& getRelDir () const { return this->prevRelDirection; }; |
---|
[5113] | 120 | /** @returns the Relative Directional Destination */ |
---|
[5915] | 121 | inline const Quaternion& getRelDirSoft2D() const { return (this->toDirection)? *this->toDirection : this->relDirection; }; |
---|
[4836] | 122 | /** @returns a Vector pointing into the relative Direction */ |
---|
[5050] | 123 | inline Vector getRelDirV() const { return this->prevRelDirection.apply(Vector(0,1,0)); }; |
---|
[3810] | 124 | void setAbsDir (const Quaternion& absDir); |
---|
[4771] | 125 | void setAbsDir (float x, float y, float z); |
---|
[5414] | 126 | void setAbsDirSoft(const Quaternion& absDirSoft, float bias = 1.0); |
---|
| 127 | void setAbsDirSoft(float x, float y, float z, float bias = 1.0); |
---|
[5915] | 128 | void shiftDir (const Quaternion& shift); |
---|
[4836] | 129 | /** @returns the absolute Direction */ |
---|
[4372] | 130 | inline const Quaternion& getAbsDir () const { return this->absDirection; }; |
---|
[4836] | 131 | /** @returns a Vector pointing into the absolute Direction */ |
---|
[4372] | 132 | inline Vector getAbsDirV() const { return this->absDirection.apply(Vector(0,1,0)); }; |
---|
[5915] | 133 | /** @returns A Vector pointing into the forward direction (X) of the Node */ |
---|
| 134 | inline Vector getAbsDirX() const { return this->absDirection.apply(Vector(1,0,0)); }; |
---|
| 135 | /** @returns A Vector pointing into the upward direction (Y) of the Node */ |
---|
| 136 | inline Vector getAbsDirY() const { return this->absDirection.apply(Vector(0,1,0)); }; |
---|
| 137 | /** @returns A Vector pointing into the right direction (Z) of the Node */ |
---|
| 138 | inline Vector getAbsDirZ() const { return this->absDirection.apply(Vector(0,0,1)); }; |
---|
[3246] | 139 | |
---|
[3644] | 140 | |
---|
[6078] | 141 | // PARENTING // |
---|
[5382] | 142 | void addChild (PNode* child); |
---|
[4765] | 143 | void addChild (const char* childName); |
---|
[4993] | 144 | void removeChild (PNode* child); |
---|
[5769] | 145 | void removeNode(); |
---|
[3537] | 146 | |
---|
[6054] | 147 | /** @param parent the new parent of this node */ |
---|
[6048] | 148 | inline void setParent (PNode* parent) { parent->addChild(this); }; |
---|
[4987] | 149 | void setParent (const char* parentName); |
---|
[4966] | 150 | /** @returns the parent of this PNode */ |
---|
[5387] | 151 | inline PNode* getParent () const { return this->parent; }; |
---|
| 152 | /** @returns the List of Children of this PNode */ |
---|
[6071] | 153 | const std::list<PNode*>& getNodesChildren() const { return this->children; }; |
---|
[4761] | 154 | |
---|
[5382] | 155 | void setParentSoft(PNode* parentNode, float bias = 1.0); |
---|
| 156 | void setParentSoft(const char* parentName, float bias = 1.0); |
---|
[4987] | 157 | |
---|
[6078] | 158 | // PARENTING_MODE AND OTHER FLAGS // |
---|
[6054] | 159 | void setParentMode (PARENT_MODE parentMode); |
---|
[4765] | 160 | void setParentMode (const char* parentingMode); |
---|
[4836] | 161 | /** @returns the Parenting mode of this node */ |
---|
[6054] | 162 | int getParentMode() const { return 0x000f & this->parentMode; }; |
---|
[3246] | 163 | |
---|
[6078] | 164 | void addNodeFlags(unsigned short nodeFlags); |
---|
| 165 | void removeNodeFlags(unsigned short nodeFlags); |
---|
[6054] | 166 | |
---|
[6078] | 167 | // NULL_PARENT // |
---|
[6074] | 168 | /** @returns the NullParent, the (main) ROOT of the PNode Tree. If it does not yet exist, it will be created. */ |
---|
[6078] | 169 | static PNode* getNullParent() { return (PNode::nullParent != NULL)? PNode::nullParent : PNode::createNullParent(); }; |
---|
[6054] | 170 | |
---|
[6078] | 171 | // UPDATING // |
---|
[5769] | 172 | void updateNode (float dt); |
---|
[3246] | 173 | |
---|
[6078] | 174 | // DEBUG // |
---|
[5769] | 175 | void countChildNodes(int& nodes) const; |
---|
[6424] | 176 | void debugNodeSC (unsigned int depth = 1, unsigned int level = 0) { this->debugNode(depth, level); }; |
---|
[5769] | 177 | void debugNode (unsigned int depth = 1, unsigned int level = 0) const; |
---|
[5383] | 178 | void debugDraw(unsigned int depth = 1, float size = 1.0, const Vector& color = Vector(1, 0, 0), unsigned int level = 0) const; |
---|
[3365] | 179 | |
---|
[6078] | 180 | // HELPER_FUNCTIONS // |
---|
[4993] | 181 | static const char* parentingModeToChar(int parentingMode); |
---|
| 182 | static PARENT_MODE charToParentingMode(const char* parentingMode); |
---|
[5750] | 183 | |
---|
[6341] | 184 | int writeState(const byte* data, int length, int sender); |
---|
| 185 | int readState(byte* data, int maxLength ); |
---|
[5750] | 186 | |
---|
[4440] | 187 | private: |
---|
[4987] | 188 | /** tells the child that the parent's Coordinate has changed */ |
---|
[4440] | 189 | inline void parentCoorChanged () { this->bRelCoorChanged = true; } |
---|
[4987] | 190 | /** tells the child that the parent's Direction has changed */ |
---|
[4440] | 191 | inline void parentDirChanged () { this->bRelDirChanged = true; } |
---|
[4836] | 192 | /** @returns the last calculated coordinate */ |
---|
[4993] | 193 | inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; } |
---|
[3246] | 194 | |
---|
[6078] | 195 | static PNode* createNullParent(); |
---|
[6048] | 196 | void reparent(); |
---|
[6075] | 197 | bool checkIntegrity(const PNode* checkParent) const; |
---|
[6078] | 198 | void eraseChild(PNode* child); |
---|
[3537] | 199 | |
---|
[3644] | 200 | private: |
---|
[5770] | 201 | bool bRelCoorChanged; //!< If Relative Coordinate has changed since last time we checked |
---|
| 202 | bool bRelDirChanged; //!< If Relative Direction has changed since last time we checked |
---|
[3644] | 203 | |
---|
[5770] | 204 | Vector relCoordinate; //!< coordinates relative to the parent |
---|
| 205 | Vector absCoordinate; //!< absolute coordinates in the world ( from (0,0,0) ) |
---|
| 206 | Quaternion relDirection; //!< direction relative to the parent |
---|
| 207 | Quaternion absDirection; //!< absolute direvtion in the world ( from (0,0,1) ) |
---|
[4440] | 208 | |
---|
[5770] | 209 | Vector prevRelCoordinate; //!< The last Relative Coordinate from the last update-Cycle. |
---|
| 210 | Vector lastAbsCoordinate; //!< this is used for speedcalculation, it stores the last coordinate |
---|
| 211 | Quaternion prevRelDirection; //!< The last Relative Direciton from the last update-Cycle. |
---|
| 212 | // Quaternion lastAbsDirection; |
---|
[4440] | 213 | |
---|
[5770] | 214 | Vector velocity; //!< Saves the velocity. |
---|
[4987] | 215 | |
---|
[5770] | 216 | Vector* toCoordinate; //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft) |
---|
| 217 | Quaternion* toDirection; //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft) |
---|
| 218 | float bias; //!< how fast to iterate to the given position (default is 1) |
---|
[4987] | 219 | |
---|
[6042] | 220 | PNode* parent; //!< a pointer to the parent node. |
---|
| 221 | std::list<PNode*> children; //!< list of the children of this PNode. |
---|
[4440] | 222 | |
---|
[6042] | 223 | bool bActive; //!< If the Node is Active (for cutting off the rest of the tree in update). |
---|
| 224 | unsigned short parentMode; //!< the mode of the binding |
---|
[6074] | 225 | |
---|
| 226 | |
---|
| 227 | static PNode* nullParent; //!< The ROOT of the main PNode Tree. |
---|
[3246] | 228 | }; |
---|
| 229 | |
---|
| 230 | #endif /* _P_NODE_H */ |
---|