9 #include <argos3/core/simulator/simulator.h>
10 #include <argos3/core/simulator/space/space.h>
11 #include <argos3/core/simulator/entity/embodied_entity.h>
12 #include <argos3/plugins/simulator/physics_engines/dynamics3d/dynamics3d_model.h>
13 #include <argos3/plugins/simulator/physics_engines/dynamics3d/dynamics3d_plugin.h>
14 #include <argos3/plugins/simulator/physics_engines/dynamics3d/bullet/BulletCollision/NarrowPhaseCollision/btRaycastCallback.h>
24 m_cRandomSeedRange(0,1000),
27 m_cDispatcher(&m_cConfiguration),
29 m_cWorld(&m_cDispatcher,
48 m_cSolver.setRandSeed(m_pcRNG->
Uniform(m_cRandomSeedRange));
50 m_cWorld.setGravity(btVector3(0.0,0.0,0.0));
53 for(tPluginIterator = tPluginIterator.begin(&t_tree);
54 tPluginIterator != tPluginIterator.end();
58 pcPlugin->
Init(*tPluginIterator);
76 for(CDynamics3DModel::TMap::iterator itModel = std::begin(m_tPhysicsModels);
77 itModel != std::end(m_tPhysicsModels);
80 for(CDynamics3DPlugin::TMap::iterator itPlugin = std::begin(m_tPhysicsPlugins);
81 itPlugin != std::end(m_tPhysicsPlugins);
83 itPlugin->second->UnregisterModel(*itModel->second);
86 itModel->second->RemoveFromWorld(m_cWorld);
87 itModel->second->Reset();
90 m_cWorld.~btMultiBodyDynamicsWorld();
91 m_cSolver.~btMultiBodyConstraintSolver();
92 m_cDispatcher.~btCollisionDispatcher();
93 m_cConfiguration.~btDefaultCollisionConfiguration();
94 m_cBroadphase.~btDbvtBroadphase();
96 new (&m_cBroadphase) btDbvtBroadphase;
97 new (&m_cConfiguration) btDefaultCollisionConfiguration;
98 new (&m_cDispatcher) btCollisionDispatcher(&m_cConfiguration);
99 new (&m_cSolver) btMultiBodyConstraintSolver;
100 new (&m_cWorld) btMultiBodyDynamicsWorld(&m_cDispatcher,
105 m_cSolver.setRandSeed(m_pcRNG->
Uniform(m_cRandomSeedRange));
107 for(CDynamics3DPlugin::TMap::iterator itPlugin = std::begin(m_tPhysicsPlugins);
108 itPlugin != std::end(m_tPhysicsPlugins);
110 itPlugin->second->Reset();
113 m_cWorld.setInternalTickCallback([] (btDynamicsWorld* pc_world, btScalar f_time_step) {
115 pc_world->clearForces();
116 for(std::pair<const std::string, CDynamics3DPlugin*>& c_plugin :
118 c_plugin.second->
Update();
120 },
static_cast<void*
>(
this),
true);
122 for(CDynamics3DModel::TMap::iterator itModel = std::begin(m_tPhysicsModels);
123 itModel != std::end(m_tPhysicsModels);
126 for(CDynamics3DPlugin::TMap::iterator itPlugin = std::begin(m_tPhysicsPlugins);
127 itPlugin != std::end(m_tPhysicsPlugins);
129 itPlugin->second->RegisterModel(*itModel->second);
132 itModel->second->AddToWorld(m_cWorld);
135 for (
SInt32 i = 0; i < m_cWorld.getNumMultiBodyConstraints(); i++) {
136 m_cWorld.getMultiBodyConstraint(i)->finalizeMultiDof();
145 for(CDynamics3DModel::TMap::iterator itModel = std::begin(m_tPhysicsModels);
146 itModel != std::end(m_tPhysicsModels);
149 for(CDynamics3DPlugin::TMap::iterator itPlugin = std::begin(m_tPhysicsPlugins);
150 itPlugin != std::end(m_tPhysicsPlugins);
152 itPlugin->second->UnregisterModel(*itModel->second);
155 itModel->second->RemoveFromWorld(m_cWorld);
156 delete itModel->second;
159 for(CDynamics3DPlugin::TMap::iterator itPlugin = std::begin(m_tPhysicsPlugins);
160 itPlugin != std::end(m_tPhysicsPlugins);
162 itPlugin->second->Destroy();
163 delete itPlugin->second;
166 m_tPhysicsPlugins.clear();
167 m_tPhysicsModels.clear();
175 for(CDynamics3DModel::TMap::iterator it = m_tPhysicsModels.begin();
176 it != std::end(m_tPhysicsModels); ++it) {
177 it->second->UpdateFromEntityStatus();
182 for(CDynamics3DModel::TMap::iterator it = std::begin(m_tPhysicsModels);
183 it != std::end(m_tPhysicsModels);
185 it->second->UpdateEntityStatus();
193 const CRay3& c_ray)
const {
197 btCollisionWorld::ClosestRayResultCallback cResult(cRayStart, cRayEnd);
200 cResult.m_flags |= btTriangleRaycastCallback::kF_UseGjkConvexCastRaytest;
202 m_cWorld.rayTest(cRayStart, cRayEnd, cResult);
204 if (cResult.hasHit() && cResult.m_collisionObject->getUserPointer() !=
nullptr) {
205 Real f_t = (cResult.m_hitPointWorld - cRayStart).length() / c_ray.
GetLength();
207 static_cast<CDynamics3DModel*
>(cResult.m_collisionObject->getUserPointer());
216 return m_tPhysicsModels.size();
224 CallEntityOperation<CDynamics3DOperationAddEntity, CDynamics3DEngine, SOperationOutcome>
226 return cOutcome.
Value;
234 CallEntityOperation<CDynamics3DOperationRemoveEntity, CDynamics3DEngine, SOperationOutcome>
236 return cOutcome.
Value;
247 for(CDynamics3DPlugin::TMap::iterator itPlugin = std::begin(m_tPhysicsPlugins);
248 itPlugin != std::end(m_tPhysicsPlugins);
250 itPlugin->second->RegisterModel(c_model);
253 m_tPhysicsModels[str_id] = &c_model;
260 CDynamics3DModel::TMap::iterator itModel = m_tPhysicsModels.find(str_id);
261 if(itModel != std::end(m_tPhysicsModels)) {
263 for(CDynamics3DPlugin::TMap::iterator itPlugin = std::begin(m_tPhysicsPlugins);
264 itPlugin != std::end(m_tPhysicsPlugins);
266 itPlugin->second->UnregisterModel(*(itModel->second));
269 itModel->second->RemoveFromWorld(m_cWorld);
271 delete itModel->second;
273 m_tPhysicsModels.erase(itModel);
277 "\" was not found in the dynamics 3D engine \"" <<
287 m_tPhysicsPlugins[str_id] = &c_plugin;
294 CDynamics3DPlugin::TMap::iterator it = m_tPhysicsPlugins.find(str_id);
295 if(it != std::end(m_tPhysicsPlugins)) {
297 m_tPhysicsPlugins.erase(it);
301 "\" was not found in the dynamics 3D engine \"" <<
311 "Michael Allwright [allsey87@gmail.com]",
313 "A 3D dynamics physics engine",
314 "This physics engine is a 3D dynamics engine based on the Bullet Physics SDK\n"
315 "(https://github.com/bulletphysics/bullet3).\n\n"
316 "REQUIRED XML CONFIGURATION\n\n"
317 " <physics_engines>\n"
319 " <dynamics3d id=\"dyn3d\" />\n"
321 " </physics_engines>\n\n"
322 "The 'id' attribute is necessary and must be unique among the physics engines.\n\n"
323 "OPTIONAL XML CONFIGURATION\n\n"
324 "The physics engine supports a number of plugins that add features to the\n"
325 "simulation. In the example below, a floor plane has been added which has a\n"
326 "height of 1 cm and dimensions of the floor as specified in the arena node. By\n"
327 "default, there is no gravity in simulation. This can be changed by adding a\n"
328 "gravity node with a single attribute 'g' which is the downwards acceleration\n"
329 "caused by gravity. Finally, there is a magnetism plugin. This plugin applies\n"
330 "forces and torques to bodies in the simulation whose model contains a magnet\n"
331 "equipped entity. The 'max_distance' attribute is an optional optimization\n"
332 "that sets the maximum distance at which two magnetic dipoles will interact\n"
333 "with each other. In the example, this distance has been set to 4 cm.\n\n"
334 " <physics_engines>\n"
336 " <dynamics3d id=\"dyn3d\">\n"
337 " <floor height=\"0.01\"/>\n"
338 " <gravity g=\"10\" />\n"
339 " <magnetism max_distance=\"0.04\" />\n"
342 " </physics_engines>\n\n",
virtual void AddToWorld(btMultiBodyDynamicsWorld &c_world)=0
signed int SInt32
32-bit signed integer.
void GetNodeAttributeOrDefault(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer, const T &t_default)
Returns the value of a node's attribute, or the passed default value.
float Real
Collects all ARGoS code.
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
Real GetX() const
Returns the x coordinate of this vector.
virtual void SetEngine(CDynamics3DEngine &c_engine)
virtual bool AddEntity(CEntity &c_entity)
Adds an entity to the physics engine.
void RemovePhysicsPlugin(const std::string &str_id)
Real GetY() const
Returns the y coordinate of this vector.
Real GetPhysicsClockTick() const
Returns the length of the physics engine tick.
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.
REGISTER_PHYSICS_ENGINE(CDynamics2DEngine,"dynamics2d","Carlo Pinciroli [ilpincy@gmail.com]","1.0","A 2D dynamics physics engine.","This physics engine is a 2D dynamics engine based on the Chipmunk library\n""(http://code.google.com/p/chipmunk-physics) version 6.0.1.\n\n""REQUIRED XML CONFIGURATION\n\n"" <physics_engines>\n"" ...\n"" <dynamics2d id=\"dyn2d\" />\n"" ...\n"" </physics_engines>\n\n""The 'id' attribute is necessary and must be unique among the physics engines.\n""It is used in the subsequent section <arena_physics> to assign entities to\n""physics engines. If two engines share the same id, initialization aborts.\n\n""OPTIONAL XML CONFIGURATION\n\n""It is possible to set how many iterations this physics engine performs between\n""each simulation step. By default, this physics engine performs 10 steps every\n""two simulation steps. This means that, if the simulation step is 100ms, the\n""physics engine step is, by default, 10ms. Sometimes, collisions and joints are\n""not simulated with sufficient precision using these parameters. By increasing\n""the number of iterations, the temporal granularity of the solver increases and\n""with it its accuracy, at the cost of higher computational cost. To change the\n""number of iterations per simulation step use this syntax:\n\n"" <physics_engines>\n"" ...\n"" <dynamics2d id=\"dyn2d\"\n"" iterations=\"20\" />\n"" ...\n"" </physics_engines>\n\n""The plane of the physics engine can be translated on the Z axis, to simulate\n""for example hovering objects, such as flying robots. To translate the plane\n""2m up the Z axis, use the 'elevation' attribute as follows:\n\n"" <physics_engines>\n"" ...\n"" <dynamics2d id=\"dyn2d\"\n"" elevation=\"2.0\" />\n"" ...\n"" </physics_engines>\n\n""When not specified, the elevation is zero, which means that the plane\n""corresponds to the XY plane.\n\n""The friction parameters between the ground and movable boxes and cylinders can\n""be overridden. You can set both the linear and angular friction parameters.\n""The default value is 1.49 for each of them. To override the values, use this\n""syntax (all attributes are optional):\n\n"" <physics_engines>\n"" ...\n"" <dynamics2d id=\"dyn2d\"\n"" <friction box_linear_friction=\"1.0\"\n"" box_angular_friction=\"2.0\"\n"" cylinder_linear_friction=\"3.0\"\n"" cylinder_angular_friction=\"4.0\" />\n"" </dynamics2d>\n"" ...\n"" </physics_engines>\n\n""For the the robots that use velocity-based control, such as ground robots with\n""the differential_steering actuator (e.g. the foot-bot and the e-puck), it is\n""possible to customize robot-specific attributes that set the maximum force and\n""torque the robot has. The syntax is as follows, taking a foot-bot as example:\n\n"" <arena ...>\n"" ...\n"" <foot-bot id=\"fb0\">\n"" <body position=\"0.4,2.3,0.25\" orientation=\"45,0,0\" />\n"" <controller config=\"mycntrl\" />\n"" <!-- Specify new value for max_force and max_torque -->\n"" <dynamics2d>\n"" <differential_steering max_force=\"0.1\" max_torque=\"0.1\"/>\n"" </dynamics2d>\n"" </foot-bot>\n"" ...\n"" </arena>\n\n""The attributes 'max_force' and 'max_torque' are both optional, and they take the\n""robot-specific default if not set. Check the code of the dynamics2d model of the\n""robot you're using to know the default values.\n\n""By default, this engine uses the bounding-box tree method for collision shape\n""indexing. This method is the default in Chipmunk and it works well most of the\n""times. However, if you are running simulations with hundreds or thousands of\n""identical robots, a different shape collision indexing is available: the spatial\n""hash. The spatial hash is a grid stored in a hashmap. To get the max out of this\n""indexing method, you must set two parameters: the cell size and the suggested\n""minimum number of cells in the space. According to the documentation of\n""Chipmunk, the cell size should correspond to the size of the bounding box of the\n""most common object in the simulation; the minimum number of cells should be at\n""least 10x the number of objects managed by the physics engine. To use this\n""indexing method, use this syntax (all attributes are mandatory):\n\n"" <physics_engines>\n"" ...\n"" <dynamics2d id=\"dyn2d\"\n"" <spatial_hash cell_size=\"1.0\"\n"" cell_num=\"2.0\" />\n"" </dynamics2d>\n"" ...\n"" </physics_engines>\n","Usable")
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
Type to use as return value for operation outcome.
virtual size_t GetNumPhysicsModels()
const std::string & GetId() const
Returns the id of this physics engine.
void RemovePhysicsModel(const std::string &str_id)
CRadians Uniform(const CRange< CRadians > &c_range)
Returns a random value from a uniform distribution.
std::vector< SEmbodiedEntityIntersectionItem > TEmbodiedEntityIntersectionData
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.
static TYPE * New(const std::string &str_label)
Creates a new object of type TYPE
void AddPhysicsPlugin(const std::string &str_id, CDynamics3DPlugin &c_plugin)
virtual void Init(TConfigurationNode &t_tree)
CEmbodiedEntity & GetEmbodiedEntity()
Returns the embodied entity associated to this physics model.
virtual void CheckIntersectionWithRay(TEmbodiedEntityIntersectionData &t_data, const CRay3 &c_ray) const
Check which objects in this engine intersect the given ray.
ticpp::Iterator< ticpp::Element > TConfigurationNodeIterator
The iterator for the ARGoS configuration XML node.
virtual void Destroy()
Undoes whatever was done by Init().
static CRNG * CreateRNG(const std::string &str_category)
Creates a new RNG inside the given category.
virtual void Reset()
Resets the resource.
std::map< std::string, CDynamics3DPlugin * > & GetPhysicsPlugins()
The namespace containing all the ARGoS related code.
Real GetZ() const
Returns the z coordinate of this vector.
virtual void PostSpaceInit()
Executes extra initialization activities after the space has been initialized.
void AddPhysicsModel(const std::string &str_id, CDynamics3DModel &c_model)
virtual bool RemoveEntity(CEntity &c_entity)
Removes an entity from the physics engine.