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