dynamics2d_engine.h
Go to the documentation of this file.
1 
7 #ifndef DYNAMICS2D_ENGINE_H
8 #define DYNAMICS2D_ENGINE_H
9 
10 namespace argos {
11  class CDynamics2DEngine;
12  class CDynamics2DModel;
13  class CGripperEquippedEntity;
14  class CEmbodiedEntity;
15 }
16 
17 #include <argos3/core/simulator/entity/controllable_entity.h>
18 #include <argos3/core/simulator/physics_engine/physics_engine.h>
19 #include <argos3/plugins/simulator/physics_engines/dynamics2d/chipmunk-physics/include/chipmunk.h>
20 
21 namespace argos {
22 
23  /****************************************/
24  /****************************************/
25 
29  cpVect GripperAnchor;
30  cpConstraint* GripConstraint;
31 
33  CGripperEquippedEntity& c_entity,
34  cpVect t_anchor);
37  };
38 
39  /****************************************/
40  /****************************************/
41 
43 
44  public:
45 
46  enum EShapeType {
50  };
51 
52  enum ELayerType {
54  LAYER_NORMAL = CP_ALL_LAYERS
55  };
56 
58 
59  virtual ~CDynamics2DEngine() {}
60 
61  virtual void Init(TConfigurationNode& t_tree);
62  virtual void Reset();
63  virtual void Update();
64  virtual void Destroy();
65 
66  virtual size_t GetNumPhysicsModels();
67  virtual bool AddEntity(CEntity& c_entity);
68  virtual bool RemoveEntity(CEntity& c_entity);
69 
71  const CRay3& c_ray) const;
72 
73  inline cpFloat GetBoxLinearFriction() const {
74  return m_fBoxLinearFriction;
75  }
76 
77  inline cpFloat GetBoxAngularFriction() const {
78  return m_fBoxAngularFriction;
79  }
80 
81  inline cpFloat GetCylinderLinearFriction() const {
82  return m_fCylinderLinearFriction;
83  }
84 
85  inline cpFloat GetCylinderAngularFriction() const {
86  return m_fCylinderAngularFriction;
87  }
88 
89  inline cpSpace* GetPhysicsSpace() {
90  return m_ptSpace;
91  }
92 
93  inline const cpSpace* GetPhysicsSpace() const {
94  return m_ptSpace;
95  }
96 
97  inline cpBody* GetGroundBody() {
98  return m_ptGroundBody;
99  }
100 
101  inline Real GetElevation() const {
102  return m_fElevation;
103  }
104 
105  inline Real GetDamping() const {
106  return cpSpaceGetDamping(m_ptSpace);
107  }
108 
109  inline void SetDamping(Real f_damping) {
110  cpSpaceSetDamping(m_ptSpace, f_damping);
111  }
112 
113  inline CVector2 GetGravity() const {
114  return CVector2(m_ptSpace->gravity.x, m_ptSpace->gravity.y);
115  }
116 
117  inline void SetGravity(const CVector2& c_gravity) {
118  m_ptSpace->gravity = cpv(c_gravity.GetX(), c_gravity.GetY());
119  }
120 
121  inline Real GetGrippingRigidity() const {
122  return m_fGrippingRigidity;
123  }
124 
125  inline void SetGrippingRigidity(Real f_gripping_rigidity) {
126  m_fGrippingRigidity = f_gripping_rigidity;
127  }
128 
129  void PositionPhysicsToSpace(CVector3& c_new_pos,
130  const CVector3& c_original_pos,
131  const cpBody* pt_body);
132 
133  void OrientationPhysicsToSpace(CQuaternion& c_new_orient,
134  cpBody* pt_body);
135 
136  void AddPhysicsModel(const std::string& str_id,
137  CDynamics2DModel& c_model);
138  void RemovePhysicsModel(const std::string& str_id);
139 
140  private:
141 
142  cpFloat m_fBoxLinearFriction;
143  cpFloat m_fBoxAngularFriction;
144  cpFloat m_fCylinderLinearFriction;
145  cpFloat m_fCylinderAngularFriction;
146  cpSpace* m_ptSpace;
147  cpBody* m_ptGroundBody;
148  Real m_fGrippingRigidity;
149  Real m_fElevation;
150 
151  CControllableEntity::TMap m_tControllableEntities;
152  std::map<std::string, CDynamics2DModel*> m_tPhysicsModels;
153 
154  };
155 
156  /****************************************/
157  /****************************************/
158 
159  template <typename ACTION>
160  class CDynamics2DOperation : public CEntityOperation<ACTION, CDynamics2DEngine, SOperationOutcome> {
161  public:
163  };
164 
165  class CDynamics2DOperationAddEntity : public CDynamics2DOperation<CDynamics2DOperationAddEntity> {
166  public:
168  };
169 
170  class CDynamics2DOperationRemoveEntity : public CDynamics2DOperation<CDynamics2DOperationRemoveEntity> {
171  public:
173  };
174 
175 #define REGISTER_DYNAMICS2D_OPERATION(ACTION, OPERATION, ENTITY) \
176  REGISTER_ENTITY_OPERATION(ACTION, CDynamics2DEngine, OPERATION, SOperationOutcome, ENTITY);
177 
178 #define REGISTER_STANDARD_DYNAMICS2D_OPERATION_ADD_ENTITY(SPACE_ENTITY, DYN2D_MODEL) \
179  class CDynamics2DOperationAdd ## SPACE_ENTITY : public CDynamics2DOperationAddEntity { \
180  public: \
181  CDynamics2DOperationAdd ## SPACE_ENTITY() {} \
182  virtual ~CDynamics2DOperationAdd ## SPACE_ENTITY() {} \
183  SOperationOutcome ApplyTo(CDynamics2DEngine& c_engine, \
184  SPACE_ENTITY& c_entity) { \
185  auto* pcPhysModel = new DYN2D_MODEL(c_engine, \
186  c_entity); \
187  c_engine.AddPhysicsModel(c_entity.GetId(), \
188  *pcPhysModel); \
189  c_entity. \
190  GetComponent<CEmbodiedEntity>("body"). \
191  AddPhysicsModel(c_engine.GetId(), *pcPhysModel); \
192  return SOperationOutcome(true); \
193  } \
194  }; \
195  REGISTER_DYNAMICS2D_OPERATION(CDynamics2DOperationAddEntity, \
196  CDynamics2DOperationAdd ## SPACE_ENTITY, \
197  SPACE_ENTITY);
198 
199 #define REGISTER_STANDARD_DYNAMICS2D_OPERATION_REMOVE_ENTITY(SPACE_ENTITY) \
200  class CDynamics2DOperationRemove ## SPACE_ENTITY : public CDynamics2DOperationRemoveEntity { \
201  public: \
202  CDynamics2DOperationRemove ## SPACE_ENTITY() {} \
203  virtual ~CDynamics2DOperationRemove ## SPACE_ENTITY() {} \
204  SOperationOutcome ApplyTo(CDynamics2DEngine& c_engine, \
205  SPACE_ENTITY& c_entity) { \
206  c_engine.RemovePhysicsModel(c_entity.GetId()); \
207  c_entity. \
208  GetComponent<CEmbodiedEntity>("body"). \
209  RemovePhysicsModel(c_engine.GetId()); \
210  return SOperationOutcome(true); \
211  } \
212  }; \
213  REGISTER_DYNAMICS2D_OPERATION(CDynamics2DOperationRemoveEntity, \
214  CDynamics2DOperationRemove ## SPACE_ENTITY, \
215  SPACE_ENTITY);
216 
217 #define REGISTER_STANDARD_DYNAMICS2D_OPERATIONS_ON_ENTITY(SPACE_ENTITY, DYN2D_ENTITY) \
218  REGISTER_STANDARD_DYNAMICS2D_OPERATION_ADD_ENTITY(SPACE_ENTITY, DYN2D_ENTITY) \
219  REGISTER_STANDARD_DYNAMICS2D_OPERATION_REMOVE_ENTITY(SPACE_ENTITY)
220 
221  /****************************************/
222  /****************************************/
223 
224 }
225 
226 #endif
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
std::vector< SEmbodiedEntityIntersectionItem > TEmbodiedEntityIntersectionData
std::map< std::string, CControllableEntity * > TMap
A map of controllable entities, indexed by id.
The basic entity type.
Definition: entity.h:90
The basic operation to be stored in the vtable.
Definition: entity.h:331
A 2D vector class.
Definition: vector2.h:27
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector2.h:110
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector2.h:94
A 3D vector class.
Definition: vector3.h:31
An entity that stores the state of a robot gripper.
SDynamics2DEngineGripperData(CDynamics2DEngine &c_engine, CGripperEquippedEntity &c_entity, cpVect t_anchor)
CGripperEquippedEntity & GripperEntity
void SetDamping(Real f_damping)
cpFloat GetBoxAngularFriction() const
virtual void Reset()
Resets the resource.
cpFloat GetBoxLinearFriction() const
void SetGravity(const CVector2 &c_gravity)
void RemovePhysicsModel(const std::string &str_id)
void PositionPhysicsToSpace(CVector3 &c_new_pos, const CVector3 &c_original_pos, const cpBody *pt_body)
virtual size_t GetNumPhysicsModels()
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.
void SetGrippingRigidity(Real f_gripping_rigidity)
virtual void Destroy()
Undoes whatever was done by Init().
virtual void CheckIntersectionWithRay(TEmbodiedEntityIntersectionData &t_data, const CRay3 &c_ray) const
Check which objects in this engine intersect the given ray.
cpFloat GetCylinderLinearFriction() const
void AddPhysicsModel(const std::string &str_id, CDynamics2DModel &c_model)
void OrientationPhysicsToSpace(CQuaternion &c_new_orient, cpBody *pt_body)
cpFloat GetCylinderAngularFriction() const
virtual bool RemoveEntity(CEntity &c_entity)
Removes an entity from the physics engine.
virtual bool AddEntity(CEntity &c_entity)
Adds an entity to the physics engine.
const cpSpace * GetPhysicsSpace() const
The base class for models in the dynamics 2D engine.