dynamics3d_engine.h
Go to the documentation of this file.
1 
7 #ifndef DYNAMICS3D_ENGINE_H
8 #define DYNAMICS3D_ENGINE_H
9 
10 namespace argos {
11  class CDynamics3DEngine;
12  class CDynamics3DModel;
13  class CDynamics3DPlugin;
14 }
15 
16 #include <argos3/core/simulator/entity/controllable_entity.h>
17 #include <argos3/core/simulator/physics_engine/physics_engine.h>
18 #include <argos3/core/utility/math/ray2.h>
19 #include <argos3/core/utility/math/rng.h>
20 
21 #ifdef __APPLE__
22 #pragma clang diagnostic push
23 #pragma clang diagnostic ignored "-Wreorder"
24 #else
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wreorder"
27 #endif
28 
29 #include <argos3/plugins/simulator/physics_engines/dynamics3d/bullet/btBulletDynamicsCommon.h>
30 #include <argos3/plugins/simulator/physics_engines/dynamics3d/bullet/BulletCollision/NarrowPhaseCollision/btRaycastCallback.h>
31 #include <argos3/plugins/simulator/physics_engines/dynamics3d/bullet/BulletDynamics/Featherstone/btMultiBodyConstraintSolver.h>
32 #include <argos3/plugins/simulator/physics_engines/dynamics3d/bullet/BulletDynamics/Featherstone/btMultiBodyDynamicsWorld.h>
33 
34 #ifdef __APPLE__
35 #pragma clang diagnostic pop
36 #else
37 #pragma GCC diagnostic pop
38 #endif
39 
40 namespace argos {
41 
42  /****************************************/
43  /****************************************/
44 
46 
47  public:
48 
50 
51  virtual ~CDynamics3DEngine() {}
52 
53  virtual void Init(TConfigurationNode& t_tree);
54 
55  virtual void Reset();
56 
57  virtual void Update();
58 
59  virtual void Destroy();
60 
61  virtual void PostSpaceInit();
62 
63  virtual size_t GetNumPhysicsModels();
64 
65  virtual bool AddEntity(CEntity& c_entity);
66 
67  virtual bool RemoveEntity(CEntity& c_entity);
68 
70  const CRay3& c_ray) const;
71 
72  inline btMultiBodyDynamicsWorld& GetWorld() {
73  return m_cWorld;
74  }
75 
76  inline std::map<std::string, CDynamics3DPlugin*>& GetPhysicsPlugins() {
77  return m_tPhysicsPlugins;
78  }
79 
80  inline btCollisionDispatcher& GetDispatcher() {
81  return m_cDispatcher;
82  }
83 
84  inline const btCollisionDispatcher& GetDispatcher() const {
85  return m_cDispatcher;
86  }
87 
88  inline btScalar GetDefaultFriction() const {
89  return m_fDefaultFriction;
90  }
91 
92  void AddPhysicsModel(const std::string& str_id,
93  CDynamics3DModel& c_model);
94 
95  void RemovePhysicsModel(const std::string& str_id);
96 
97  void AddPhysicsPlugin(const std::string& str_id,
98  CDynamics3DPlugin& c_plugin);
99 
100  void RemovePhysicsPlugin(const std::string& str_id);
101 
102  private:
103  /* Maps of models and plugins */
104  std::map<std::string, CDynamics3DModel*> m_tPhysicsModels;
105  std::map<std::string, CDynamics3DPlugin*> m_tPhysicsPlugins;
106  /* Random number generation */
107  CRandom::CRNG* m_pcRNG;
108  CRange<UInt32> m_cRandomSeedRange;
109  /* Debug filename */
110  std::string m_strDebugFilename;
111  /* Default friction */
112  btScalar m_fDefaultFriction;
113  /* Bullet Physics World Data */
114  btDbvtBroadphase m_cBroadphase;
115  btDefaultCollisionConfiguration m_cConfiguration;
116  btCollisionDispatcher m_cDispatcher;
117  btMultiBodyConstraintSolver m_cSolver;
118  btMultiBodyDynamicsWorld m_cWorld;
119  };
120 
121  /****************************************/
122  /****************************************/
123 
124  template <typename ACTION>
125  class CDynamics3DOperation : public CEntityOperation<ACTION, CDynamics3DEngine, SOperationOutcome> {
126  public:
128  };
129 
130  class CDynamics3DOperationAddEntity : public CDynamics3DOperation<CDynamics3DOperationAddEntity> {
131  public:
133  };
134 
135  class CDynamics3DOperationRemoveEntity : public CDynamics3DOperation<CDynamics3DOperationRemoveEntity> {
136  public:
138  };
139 
140 #define REGISTER_DYNAMICS3D_OPERATION(ACTION, OPERATION, ENTITY) \
141  REGISTER_ENTITY_OPERATION(ACTION, CDynamics3DEngine, OPERATION, SOperationOutcome, ENTITY);
142 
143 #define REGISTER_STANDARD_DYNAMICS3D_OPERATION_ADD_ENTITY(SPACE_ENTITY, DYN3D_MODEL) \
144  class CDynamics3DOperationAdd ## SPACE_ENTITY : public CDynamics3DOperationAddEntity { \
145  public: \
146  CDynamics3DOperationAdd ## SPACE_ENTITY() {} \
147  virtual ~CDynamics3DOperationAdd ## SPACE_ENTITY() {} \
148  SOperationOutcome ApplyTo(CDynamics3DEngine& c_engine, \
149  SPACE_ENTITY& c_entity) { \
150  auto* pcPhysModel = new DYN3D_MODEL(c_engine, \
151  c_entity); \
152  c_engine.AddPhysicsModel(c_entity.GetId(), \
153  *pcPhysModel); \
154  c_entity. \
155  GetComponent<CEmbodiedEntity>("body"). \
156  AddPhysicsModel(c_engine.GetId(), *pcPhysModel); \
157  return SOperationOutcome(true); \
158  } \
159  }; \
160  REGISTER_DYNAMICS3D_OPERATION(CDynamics3DOperationAddEntity, \
161  CDynamics3DOperationAdd ## SPACE_ENTITY, \
162  SPACE_ENTITY);
163 
164 #define REGISTER_STANDARD_DYNAMICS3D_OPERATION_REMOVE_ENTITY(SPACE_ENTITY) \
165  class CDynamics3DOperationRemove ## SPACE_ENTITY : public CDynamics3DOperationRemoveEntity { \
166  public: \
167  CDynamics3DOperationRemove ## SPACE_ENTITY() {} \
168  virtual ~CDynamics3DOperationRemove ## SPACE_ENTITY() {} \
169  SOperationOutcome ApplyTo(CDynamics3DEngine& c_engine, \
170  SPACE_ENTITY& c_entity) { \
171  c_engine.RemovePhysicsModel(c_entity.GetId()); \
172  c_entity. \
173  GetComponent<CEmbodiedEntity>("body"). \
174  RemovePhysicsModel(c_engine.GetId()); \
175  return SOperationOutcome(true); \
176  } \
177  }; \
178  REGISTER_DYNAMICS3D_OPERATION(CDynamics3DOperationRemoveEntity, \
179  CDynamics3DOperationRemove ## SPACE_ENTITY, \
180  SPACE_ENTITY);
181 
182 #define REGISTER_STANDARD_DYNAMICS3D_OPERATIONS_ON_ENTITY(SPACE_ENTITY, DYN3D_ENTITY) \
183  REGISTER_STANDARD_DYNAMICS3D_OPERATION_ADD_ENTITY(SPACE_ENTITY, DYN3D_ENTITY) \
184  REGISTER_STANDARD_DYNAMICS3D_OPERATION_REMOVE_ENTITY(SPACE_ENTITY)
185 
186  /****************************************/
187  /****************************************/
188 
189 }
190 
191 #endif
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
The basic entity type.
Definition: entity.h:90
The basic operation to be stored in the vtable.
Definition: entity.h:331
The RNG.
Definition: rng.h:90
virtual size_t GetNumPhysicsModels()
btMultiBodyDynamicsWorld & GetWorld()
void AddPhysicsModel(const std::string &str_id, CDynamics3DModel &c_model)
void RemovePhysicsModel(const std::string &str_id)
virtual bool AddEntity(CEntity &c_entity)
Adds an entity to the physics engine.
btCollisionDispatcher & GetDispatcher()
const btCollisionDispatcher & GetDispatcher() const
virtual void CheckIntersectionWithRay(TEmbodiedEntityIntersectionData &t_data, const CRay3 &c_ray) const
Check which objects in this engine intersect the given ray.
virtual bool RemoveEntity(CEntity &c_entity)
Removes an entity from the physics engine.
virtual void Reset()
Resets the resource.
btScalar GetDefaultFriction() const
virtual void Destroy()
Undoes whatever was done by Init().
std::map< std::string, CDynamics3DPlugin * > & GetPhysicsPlugins()
void AddPhysicsPlugin(const std::string &str_id, CDynamics3DPlugin &c_plugin)
void RemovePhysicsPlugin(const std::string &str_id)
virtual void PostSpaceInit()
Executes extra initialization activities after the space has been initialized.
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.