/*! * @file p_node.h * @brief Definition of a parenting node * * parenting is how coordinates are handled in orxonox, meaning, that all coordinates * are representet relative to another parent node. this nodes build a parenting * tree of one-sided references (from up to down referenced). * Every node manages itself a list of childrens (of whos it is parent - easy...). * * absCoordinate, absDirection have to be recalculated as soon as there was a change in * place or ortientation. this is only the case if * o bDirChanged is true (so changed) AND timeStamp != now * o bCoorChanged is true (so moved) AND timeStamp != now * this conditions make it cheaper to recalculate the tree (reduces redundant work). */ #ifndef _P_NODE_H #define _P_NODE_H #include "base_object.h" #include "vector.h" #include // FORWARD DECLARATION class TiXmlElement; template class tList; #define PNODE_ITERATION_DELTA .001 //! Parental linkage modes typedef enum { // PARENTAL FOLLOWING PNODE_LOCAL_ROTATE = 0x00000001, //!< Rotates all the children around their centers. PNODE_ROTATE_MOVEMENT = 0x00000002, //!< Moves all the children around the center of their parent, without the rotation around their own centers. PNODE_MOVEMENT = 0x00000004, //!< Moves all children along with the parent. // special linkage modes PNODE_ALL = 0x00000003, //!< Moves all children around the center of their parent, and also rotates their centers 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. PNODE_INACTIVE_NODE = 0x00000010, //!< If the node is Active (if not, it and its child wont be updated). // REPARENTING PNODE_REPARENT_TO_NULLPARENT = 0x00000100, PNODE_REPARENT_KEEP_POSITION = 0x00000200, // DELETION PNODE_PROHIBIT_CHILD_DELETE = 0x00010000, PNODE_PROHIBIT_DELETE_WITH_PARENT = 0x00020000, } PARENT_MODE; //! The default mode of the translation-binding. #define PNODE_PARENT_MODE_DEFAULT PNODE_ALL | PNODE_REPARENT_KEEP_POSITION //! Patent Node is a Engine to calculate the position of an Object in respect to the position of its parent. class PNode : virtual public BaseObject { public: PNode (); PNode(const TiXmlElement* root); PNode (const Vector& absCoor, PNode* pNode); virtual ~PNode (); void loadParams(const TiXmlElement* root); void setRelCoor (const Vector& relCoord); void setRelCoor (float x, float y, float z); void setRelCoorSoft(const Vector& relCoordSoft, float bias = 1.0); void setRelCoorSoft(float x, float y, float z, float bias = 1.0); /** @returns the relative position */ inline const Vector& getRelCoor () const { return this->prevRelCoordinate; }; /** @returns the Relative Coordinate Destination */ inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)? *this->toCoordinate : this->relCoordinate; }; void setAbsCoor (const Vector& absCoord); void setAbsCoor (float x, float y, float z); void setAbsCoorSoft(const Vector& absCoordSoft, float bias = 1.0); void setAbsCoorSoft(float x, float y, float z, float bias = 1.0); /** @returns the absolute position */ inline const Vector& getAbsCoor () const { return this->absCoordinate; }; void shiftCoor (const Vector& shift); void shiftCoor (float x, float y, float z) { this->shiftCoor(Vector(x, y, z)); }; void setRelDir (const Quaternion& relDir); void setRelDir (float x, float y, float z); void setRelDirSoft(const Quaternion& relDirSoft, float bias = 1.0); void setRelDirSoft(float x, float y, float z, float bias = 1.0); /** @returns the relative Direction */ inline const Quaternion& getRelDir () const { return this->prevRelDirection; }; /** @returns the Relative Directional Destination */ inline const Quaternion& getRelDirSoft2D() const { return (this->toDirection)? *this->toDirection : this->relDirection; }; /** @returns a Vector pointing into the relative Direction */ inline Vector getRelDirV() const { return this->prevRelDirection.apply(Vector(0,1,0)); }; void setAbsDir (const Quaternion& absDir); void setAbsDir (float x, float y, float z); void setAbsDirSoft(const Quaternion& absDirSoft, float bias = 1.0); void setAbsDirSoft(float x, float y, float z, float bias = 1.0); void shiftDir (const Quaternion& shift); /** @returns the absolute Direction */ inline const Quaternion& getAbsDir () const { return this->absDirection; }; /** @returns a Vector pointing into the absolute Direction */ inline Vector getAbsDirV() const { return this->absDirection.apply(Vector(0,1,0)); }; /** @returns A Vector pointing into the forward direction (X) of the Node */ inline Vector getAbsDirX() const { return this->absDirection.apply(Vector(1,0,0)); }; /** @returns A Vector pointing into the upward direction (Y) of the Node */ inline Vector getAbsDirY() const { return this->absDirection.apply(Vector(0,1,0)); }; /** @returns A Vector pointing into the right direction (Z) of the Node */ inline Vector getAbsDirZ() const { return this->absDirection.apply(Vector(0,0,1)); }; /** @returns the Speed of the Node */ inline float getSpeed() const { return this->velocity.len(); }; /** @returns the Velocity of the Node */ inline const Vector& getVelocity() const { return this->velocity; }; void addChild (PNode* child); void addChild (const char* childName); void removeChild (PNode* child); void removeNode(); void setParent (PNode* parent); void setParent (const char* parentName); /** @returns the parent of this PNode */ inline PNode* getParent () const { return this->parent; }; /** @returns the List of Children of this PNode */ // inline const tList* getChildren() const { return this->children; }; void setParentSoft(PNode* parentNode, float bias = 1.0); void setParentSoft(const char* parentName, float bias = 1.0); /** @param parentMode sets the parentingMode of this Node */ void setParentMode (PARENT_MODE parentMode) { this->parentMode = parentMode; }; void setParentMode (const char* parentingMode); /** @returns the Parenting mode of this node */ int getParentMode() const { return this->parentMode; }; void updateNode (float dt); void countChildNodes(int& nodes) const; void debugNode (unsigned int depth = 1, unsigned int level = 0) const; void debugDraw(unsigned int depth = 1, float size = 1.0, const Vector& color = Vector(1, 0, 0), unsigned int level = 0) const; // helper functions // static const char* parentingModeToChar(int parentingMode); static PARENT_MODE charToParentingMode(const char* parentingMode); private: void init(PNode* parent); /** tells the child that the parent's Coordinate has changed */ inline void parentCoorChanged () { this->bRelCoorChanged = true; } /** tells the child that the parent's Direction has changed */ inline void parentDirChanged () { this->bRelDirChanged = true; } /** @returns the last calculated coordinate */ inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; } private: bool bRelCoorChanged; //!< If Relative Coordinate has changed since last time we checked bool bRelDirChanged; //!< If Relative Direction has changed since last time we checked Vector relCoordinate; //!< coordinates relative to the parent Vector absCoordinate; //!< absolute coordinates in the world ( from (0,0,0) ) Quaternion relDirection; //!< direction relative to the parent Quaternion absDirection; //!< absolute direvtion in the world ( from (0,0,1) ) Vector prevRelCoordinate; //!< The last Relative Coordinate from the last update-Cycle. Vector lastAbsCoordinate; //!< this is used for speedcalculation, it stores the last coordinate Quaternion prevRelDirection; //!< The last Relative Direciton from the last update-Cycle. // Quaternion lastAbsDirection; Vector velocity; //!< Saves the velocity. Vector* toCoordinate; //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft) Quaternion* toDirection; //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft) float bias; //!< how fast to iterate to the given position (default is 1) PNode* parent; //!< a pointer to the parent node std::list children; //!< list of the children of this PNode unsigned int parentMode; //!< the mode of the binding }; #endif /* _P_NODE_H */