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>
24 m_cRandomSeedRange(0,1000),
25 m_fDefaultFriction(1.0f),
28 m_cDispatcher(&m_cConfiguration),
30 m_cWorld(&m_cDispatcher,
44 m_cSolver.setRandSeed(m_pcRNG->
Uniform(m_cRandomSeedRange));
46 m_cWorld.setGravity(btVector3(0.0,0.0,0.0));
49 for(tPluginIterator = tPluginIterator.begin(&t_tree);
50 tPluginIterator != tPluginIterator.end();
54 pcPlugin->
Init(*tPluginIterator);
74 for(
auto itModel = std::begin(m_tPhysicsModels);
75 itModel != std::end(m_tPhysicsModels);
78 for(
auto itPlugin = std::begin(m_tPhysicsPlugins);
79 itPlugin != std::end(m_tPhysicsPlugins);
81 itPlugin->second->UnregisterModel(*itModel->second);
84 itModel->second->RemoveFromWorld(m_cWorld);
86 itModel->second->Reset();
89 m_cWorld.~btMultiBodyDynamicsWorld();
90 m_cSolver.~btMultiBodyConstraintSolver();
91 m_cDispatcher.~btCollisionDispatcher();
92 m_cConfiguration.~btDefaultCollisionConfiguration();
93 m_cBroadphase.~btDbvtBroadphase();
95 new (&m_cBroadphase) btDbvtBroadphase;
96 new (&m_cConfiguration) btDefaultCollisionConfiguration;
97 new (&m_cDispatcher) btCollisionDispatcher(&m_cConfiguration);
98 new (&m_cSolver) btMultiBodyConstraintSolver;
99 new (&m_cWorld) btMultiBodyDynamicsWorld(&m_cDispatcher,
104 m_cSolver.setRandSeed(m_pcRNG->
Uniform(m_cRandomSeedRange));
106 for(
auto itPlugin = std::begin(m_tPhysicsPlugins);
107 itPlugin != std::end(m_tPhysicsPlugins);
109 itPlugin->second->Reset();
112 m_cWorld.setInternalTickCallback([] (btDynamicsWorld* pc_world, btScalar f_time_step) {
114 pc_world->clearForces();
115 for(std::pair<const std::string, CDynamics3DPlugin*>& c_plugin :
116 pc_engine->GetPhysicsPlugins()) {
117 c_plugin.second->
Update();
119 },
static_cast<void*
>(
this),
true);
121 for(
auto itModel = std::begin(m_tPhysicsModels);
122 itModel != std::end(m_tPhysicsModels);
125 for(
auto itPlugin = std::begin(m_tPhysicsPlugins);
126 itPlugin != std::end(m_tPhysicsPlugins);
128 itPlugin->second->RegisterModel(*itModel->second);
131 itModel->second->AddToWorld(m_cWorld);
134 for (
SInt32 i = 0; i < m_cWorld.getNumMultiBodyConstraints(); i++) {
135 m_cWorld.getMultiBodyConstraint(i)->finalizeMultiDof();
144 for(
auto itModel = std::begin(m_tPhysicsModels);
145 itModel != std::end(m_tPhysicsModels);
148 for(
auto itPlugin = std::begin(m_tPhysicsPlugins);
149 itPlugin != std::end(m_tPhysicsPlugins);
151 itPlugin->second->UnregisterModel(*itModel->second);
154 itModel->second->RemoveFromWorld(m_cWorld);
155 delete itModel->second;
158 for(
auto itPlugin = std::begin(m_tPhysicsPlugins);
159 itPlugin != std::end(m_tPhysicsPlugins);
161 itPlugin->second->Destroy();
162 delete itPlugin->second;
165 m_tPhysicsPlugins.clear();
166 m_tPhysicsModels.clear();
174 for(
auto it = m_tPhysicsModels.begin();
175 it != std::end(m_tPhysicsModels); ++it) {
176 it->second->UpdateFromEntityStatus();
183 for(
auto it = std::begin(m_tPhysicsModels);
184 it != std::end(m_tPhysicsModels);
186 it->second->UpdateEntityStatus();
189 if(!m_strDebugFilename.empty()) {
190 btDefaultSerializer cSerializer;
191 m_cWorld.serialize(&cSerializer);
192 std::ofstream cDebugOutput(m_strDebugFilename);
193 if(cDebugOutput.is_open()) {
194 cDebugOutput.write(
reinterpret_cast<const char*
>(cSerializer.getBufferPointer()),
195 cSerializer.getCurrentBufferSize());
204 const CRay3& c_ray)
const {
208 btCollisionWorld::ClosestRayResultCallback cResult(cRayStart, cRayEnd);
211 cResult.m_flags |= btTriangleRaycastCallback::kF_UseGjkConvexCastRaytest;
213 m_cWorld.rayTest(cRayStart, cRayEnd, cResult);
215 if (cResult.hasHit() && cResult.m_collisionObject->getUserPointer() !=
nullptr) {
216 Real f_t = (cResult.m_hitPointWorld - cRayStart).length() / c_ray.
GetLength();
218 static_cast<CDynamics3DModel*
>(cResult.m_collisionObject->getUserPointer());
227 return m_tPhysicsModels.size();
235 CallEntityOperation<CDynamics3DOperationAddEntity, CDynamics3DEngine, SOperationOutcome>
237 return cOutcome.
Value;
245 CallEntityOperation<CDynamics3DOperationRemoveEntity, CDynamics3DEngine, SOperationOutcome>
247 return cOutcome.
Value;
258 for(
auto itPlugin = std::begin(m_tPhysicsPlugins);
259 itPlugin != std::end(m_tPhysicsPlugins);
261 itPlugin->second->RegisterModel(c_model);
264 m_tPhysicsModels[str_id] = &c_model;
271 auto itModel = m_tPhysicsModels.find(str_id);
272 if(itModel != std::end(m_tPhysicsModels)) {
274 for(
auto itPlugin = std::begin(m_tPhysicsPlugins);
275 itPlugin != std::end(m_tPhysicsPlugins);
277 itPlugin->second->UnregisterModel(*(itModel->second));
280 itModel->second->RemoveFromWorld(m_cWorld);
282 delete itModel->second;
284 m_tPhysicsModels.erase(itModel);
288 "\" was not found in the dynamics 3D engine \"" <<
298 m_tPhysicsPlugins[str_id] = &c_plugin;
305 auto it = m_tPhysicsPlugins.find(str_id);
306 if(it != std::end(m_tPhysicsPlugins)) {
308 m_tPhysicsPlugins.erase(it);
312 "\" was not found in the dynamics 3D engine \"" <<
322 "Michael Allwright [allsey87@gmail.com]",
324 "A 3D dynamics physics engine",
325 "This physics engine is a 3D dynamics engine based on the Bullet Physics SDK\n"
326 "(https://github.com/bulletphysics/bullet3).\n\n"
328 "REQUIRED XML CONFIGURATION\n\n"
329 " <physics_engines>\n"
331 " <dynamics3d id=\"dyn3d\" />\n"
333 " </physics_engines>\n\n"
335 "The 'id' attribute is necessary and must be unique among the physics engines.\n\n"
337 "Multiple physics engines of this type cannot be used, and defining '<boundaries>'\n"
338 "as a child tag under the '<dynamics3d>' tree will result in an initialization error.\n\n"
340 "OPTIONAL XML CONFIGURATION\n\n"
342 "It is possible to change the default friction used in the simulation from\n"
343 "its initial value of 1.0 using the default_friction attribute as shown\n"
344 "below. For debugging purposes, it is also possible to provide a filename\n"
345 "via the debug_file attribute which will cause the Bullet world to be\n"
346 "serialized and written out to a file at the end of each step. This file can\n"
347 "then be opened using the Bullet example browser and can provide useful\n"
348 "insights into the stability of a simulation.\n\n"
350 " <physics_engines>\n"
352 " <dynamics3d id=\"dyn3d\"\n"
353 " default_friction=\"1.0\"\n"
354 " debug_file=\"dynamics3d.bullet\"/>\n"
356 " </physics_engines>\n\n"
358 "The physics engine supports a number of plugins that add features to the\n"
359 "simulation. In the example below, a floor plane has been added which has a\n"
360 "height of 1 cm and the dimensions of the floor as specified by the arena\n"
361 "node. It is possible to change the coefficient of friction of the floor\n"
362 "using the friction attribute. This will override the default friction used\n"
363 "by the physics engine. By default, there will be no gravity in the\n"
364 "simulation. This can be changed by adding a gravity node with a single\n"
365 "attribute 'g' which is the downwards acceleration caused by gravity.\n"
366 "Finally, there is a magnetism plugin. This plugin applies forces and\n"
367 "torques to bodies in the simulation that contains magnetic dipoles. The\n"
368 "'max_distance' attribute is an optional optimization that sets the maximum\n"
369 "distance at which two magnetic dipoles will interact with each other. In\n"
370 "the example below, this distance has been set to 4 cm.\n\n"
372 " <physics_engines>\n"
374 " <dynamics3d id=\"dyn3d\" default_friction=\"2.0\">\n"
375 " <floor height=\"0.01\" friction=\"0.05\"/>\n"
376 " <gravity g=\"10\" />\n"
377 " <magnetism max_distance=\"0.04\" />\n"
380 " </physics_engines>\n\n",
382 "Usable (multiple engines not supported)"
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
signed int SInt32
32-bit signed integer.
float Real
Collects all ARGoS code.
The namespace containing all the ARGoS related code.
ticpp::Iterator< ticpp::Element > TConfigurationNodeIterator
The iterator for the ARGoS configuration XML node.
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.
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
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" "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" "Multiple physics engines can also be used. If multiple physics engines are used,\n" "the disjoint union of the area within the arena assigned to each engine must cover\n" "the entire arena without overlapping. If the entire arena is not covered, robots can\n" "\"escape\" the configured physics engines and cause a fatal exception (this is not an\n" "issue when a single physics engine is used, because the engine covers the entire arena\n" "by default). To use multiple physics engines, use the following syntax (all attributes\n" "are mandatory):\n\n" " <physics_engines>\n" " ...\n" " <dynamics2d id=\"dyn2d0\">\n" " <boundaries>\n" " <top height=\"1.0\"/>\n" " <botton height=\"0.0\"/>\n" " <sides>\n" " <vertex point=\"0.0, 0.0\"/>\n" " <vertex point=\"4.0, 0.0\"/>\n" " <vertex point=\"4.0, 4.0\"/>\n" " <vertex point=\"0.0, 4.0\"/>\n" " </sides>\n" " </boundaries>\n" " </dynamics2d>\n" " <dynamics2d id=\"dyn2d1\">\n" " ..." " </dynamics2d>\n" " ...\n" " </physics_engines>\n\n" "The 'top' and 'bottom' nodes are relevant for 3D physics engines. For 2D\n" "engines, it safe to set their height to 1.0 and 0.0, respectively. A physics\n" "engine can be defined having any number of sides >= 3, as long as the sides form\n" "a closed polygon in the 2D plane. The vertices must be declared in\n" "counter-clockwise order. In the above example, the physics engine \"dyn2d0\" is\n" "assigned to the area within the arena with lower-left coordinates (0,0) and\n" "upper-right coordinates (4,4) and vertices are specified in counter clockwise\n" "order: south-east, south-west, north-west, north-east.\n\n" "OPTIMIZATION HINTS\n\n" "1. A single physics engine is generally sufficient for small swarms (say <= 50\n" " robots) within a reasonably small arena to obtain faster than real-time\n" " performance with optimized code. For larger swarms and/or large arenas,\n" " multiple engines should be used for maximum performance.\n\n" "2. In general, using the same number of ARGoS threads as physics engines gives\n" " maximum performance (1-thread per engine per CPU core).\n\n" "3. Using multiple engines in simulations with any of the following\n" " characteristics generally incurs more overhead (due to thread context\n" " switching) than the performance benefits from multiple engines:\n" " - Small swarms\n" " - Small arenas\n" " - Less available ARGoS threads than assigned physics engines\n" " - Less available CPU cores than assigned ARGoS threads\n\n" "4. A good starting strategy for physics engine boundary assignment is to assign\n" " each physics engine the same amount of area within the arena. This will be\n" " sufficient for most cases. Depending on the nature of the simulation, using\n" " non-homogeneous physics engine sizes may yield increased performance. An\n" " example would be a narrow hallway between larger open areas in the arena--the\n" " hallway will likely experience increased robot density and assigning more\n" " physics engines to that area than the relatively unpopulated open areas may\n" " increase performance.\n\n" "5. 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\n" " the times. However, if you are running simulations with hundreds or thousands\n" " of identical robots, a different shape collision indexing is available: the\n" " spatial hash. The spatial hash is a grid stored in a hashmap. To get the max\n" " out of this indexing method, you must set two parameters: the cell size and\n" " the suggested minimum number of cells in the space. According to the\n" " documentation of Chipmunk, the cell size should correspond to the size of the\n" " bounding box of the most common object in the simulation; the minimum number\n" " of cells should be at least 10x the number of objects managed by the physics\n" " engine. To use this indexing method, use this syntax (all attributes are\n" " mandatory):\n\n" " <physics_engines>\n" " ...\n" " <dynamics2d id=\"dyn2d\">\n" " <spatial_hash>\n" " <cell_size=\"1.0\"/>\n" " <cell_num=\"2.0\"/>\n" " </spatial_hash>\n" " </dynamics2d>\n" " ...\n" " </physics_engines>\n", "Usable")
std::vector< SEmbodiedEntityIntersectionItem > TEmbodiedEntityIntersectionData
Type to use as return value for operation outcome.
static Real GetSimulationClockTick()
Returns the simulation clock tick.
const std::string & GetId() const
Returns the id of this physics engine.
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.
Real GetPhysicsClockTick() const
Returns the length of the physics engine tick.
UInt32 GetIterations() const
Returns the number of iterations per simulation clock tick.
static CRNG * CreateRNG(const std::string &str_category)
Creates a new RNG inside the given category.
CRadians Uniform(const CRange< CRadians > &c_range)
Returns a random value from a uniform distribution.
Real GetX() const
Returns the x coordinate of this vector.
Real GetY() const
Returns the y coordinate of this vector.
Real GetZ() const
Returns the z coordinate of this vector.
static TYPE * New(const std::string &str_label)
Creates a new object of type TYPE
virtual size_t GetNumPhysicsModels()
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.
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.
virtual void Destroy()
Undoes whatever was done by Init().
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.
virtual void AddToWorld(btMultiBodyDynamicsWorld &c_world)=0
virtual void SetEngine(CDynamics3DEngine &c_engine)
virtual void Init(TConfigurationNode &t_tree)