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();
237 return cOutcome.
Value;
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)"
signed int SInt32
32-bit signed integer.
float Real
Collects all ARGoS code.
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
#define REGISTER_PHYSICS_ENGINE(CLASSNAME, LABEL, AUTHOR, VERSION, BRIEF_DESCRIPTION, LONG_DESCRIPTION, STATUS)
The namespace containing all the ARGoS related code.
ticpp::Iterator< ticpp::Element > TConfigurationNodeIterator
The iterator for the ARGoS configuration XML node.
RETURN_VALUE CallEntityOperation(PLUGIN &t_plugin, CEntity &c_entity)
Calls the operation corresponding to the given context and operand Skips the function call if the ope...
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.
std::vector< SEmbodiedEntityIntersectionItem > TEmbodiedEntityIntersectionData
Type to use as return value for operation outcome.
static Real GetSimulationClockTick()
Returns the simulation clock tick.
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.
const std::string & GetId() const
Returns the id of this physics engine.
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)