Homepage Demos Overview Downloads Tutorials Reference
Credits

WalkNode.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_WalkNode_h_
00003 #define INCLUDED_WalkNode_h_
00004 
00005 #include "Behaviors/StateNode.h"
00006 #include "Motion/MotionManager.h"
00007 #include "Motion/WalkMC.h"
00008 #include "Motion/MMAccessor.h"
00009 
00010 //! A StateNode for walking in a direction
00011 class WalkNode : public StateNode {
00012 public:
00013   //!constructor
00014   WalkNode(StateNode * p=NULL)
00015     : StateNode("WalkNode",p), walkid(MotionManager::invalid_MC_ID), walkidIsMine(true), x(0), y(0), a(0)
00016   {
00017     setRetain(false);
00018   }
00019 
00020   //!constructor, positive is counter-clockwise from above (to match coordinate system)
00021   WalkNode(float xvel, float yvel, float avel, StateNode * p=NULL)
00022     : StateNode("WalkNode",p), walkid(MotionManager::invalid_MC_ID), walkidIsMine(true), x(xvel), y(yvel), a(avel)
00023   {
00024     setRetain(false);
00025   }
00026 
00027   //! sets the velocity of the walk
00028   void setVelocity(float xvel, float yvel, float avel) {
00029     x=xvel;
00030     y=yvel;
00031     a=avel;
00032     updateWalk(x,y,a);
00033   }
00034   
00035   //! sets the velocity in x direction (positive is forward)
00036   void setXVelocity(float xvel) { x=xvel; updateWalk(x,y,a); }
00037 
00038   //! returns the velocity in x direction (positive is forward)
00039   float getXVelocity() { return x; }
00040 
00041   //! sets the velocity in y direction (positive is forward)
00042   void setYVelocity(float yvel) { y=yvel; updateWalk(x,y,a); }
00043 
00044   //! returns the velocity in y direction (positive is forward)
00045   float getYVelocity() { return y; }
00046 
00047   //! sets the velocity of the turn, positive is counter-clockwise from above (to match coordinate system)
00048   void setAVelocity(float avel) { a=avel; updateWalk(x,y,a); }
00049 
00050   //! returns the velocity of the turn, positive is counter-clockwise from above (to match coordinate system)
00051   float getAVelocity() { return a; }
00052 
00053   virtual void DoStart() {
00054     StateNode::DoStart();
00055     updateWalk(x,y,a);
00056   }
00057 
00058   virtual void DoStop() {
00059     updateWalk(0,0,0);
00060     StateNode::DoStop();
00061   }
00062 
00063   //! removes #walkid if #walkidIsMine
00064   virtual void teardown() {
00065     if(walkidIsMine) {
00066       motman->removeMotion(walkid);
00067       walkid=MotionManager::invalid_MC_ID;
00068     }
00069   }
00070 
00071   //! use this to force the WalkNode to use a shared WalkMC - set to MotionManager::invalid_MC_ID to reset to internally generated walk
00072   virtual void setWalkID(MotionManager::MC_ID id) {
00073     if(walkidIsMine) {
00074       motman->removeMotion(walkid);
00075       walkid=MotionManager::invalid_MC_ID;
00076     }
00077     walkid=id;
00078     walkidIsMine=(id==MotionManager::invalid_MC_ID);
00079   }
00080 
00081   //! use this to access the WalkMC that the WalkNode is using
00082   virtual MotionManager::MC_ID getWalkID() { return walkid; }
00083 
00084   //! returns true if #walkid was created (and will be destroyed) by this WalkNode - false if assigned by setWalkID()
00085   virtual bool ownsWalkID() { return walkidIsMine; }
00086 
00087 protected:
00088   //!if the walk is invalid, create; then set xya
00089   void updateWalk(float xvel, float yvel, float avel) {
00090     if(walkid==MotionManager::invalid_MC_ID) {
00091       SharedObject<WalkMC> walk;
00092       walk->setTargetVelocity(xvel,yvel,avel);
00093       walkid=motman->addPersistentMotion(walk);
00094       walkidIsMine=true;
00095     } else {
00096       MMAccessor<WalkMC> walk(walkid);
00097       walk->setTargetVelocity(xvel,yvel,avel);
00098     }
00099   }
00100 
00101   MotionManager::MC_ID walkid; //!< the current WalkMC
00102   bool walkidIsMine; //!< true if the walk was created in updateWalk (instead of assigned externally)
00103   float x; //!< velocity in x direction (positive is forward)
00104   float y; //!< velocity in y direction (positive is dog's left)
00105   float a; //!< velocity of the turn, positive is counter-clockwise from above (to match coordinate system)
00106 };
00107 
00108 /*! @file
00109  * @brief Describes WalkNode, a  StateNode for walking in a direction
00110  * @author ejt (Creator)
00111  *
00112  * $Author: ejt $
00113  * $Name: tekkotsu-2_2 $
00114  * $Revision: 1.5 $
00115  * $State: Exp $
00116  * $Date: 2004/10/17 01:16:10 $
00117  */
00118 
00119 #endif

Tekkotsu v2.2
Generated Tue Oct 19 14:19:16 2004 by Doxygen 1.3.9.1