dynamics3d_model.cpp
Go to the documentation of this file.
1 
7 #include "dynamics3d_model.h"
8 
9 #include <argos3/core/simulator/physics_engine/physics_model.h>
10 #include <argos3/core/simulator/entity/composable_entity.h>
11 #include <argos3/core/utility/math/vector3.h>
12 #include <argos3/core/utility/math/quaternion.h>
13 #include <argos3/plugins/simulator/physics_engines/dynamics3d/dynamics3d_engine.h>
14 
15 namespace argos {
16 
17  /****************************************/
18  /****************************************/
19 
20  CDynamics3DModel::CAbstractBody::SData::SData(const btTransform& c_start_transform,
21  const btTransform& c_center_of_mass_offset,
22  const btVector3& c_inertia,
23  btScalar f_mass,
24  btScalar f_friction,
25  const std::vector<SDipole>& vec_dipoles) :
26  StartTransform(c_start_transform),
27  InverseStartTransform(c_start_transform.inverse()),
28  CenterOfMassOffset(c_center_of_mass_offset),
29  InverseCenterOfMassOffset(c_center_of_mass_offset.inverse()),
30  Inertia(c_inertia),
31  Mass(f_mass),
32  Friction(f_friction),
33  Dipoles(vec_dipoles) {}
34 
35  /****************************************/
36  /****************************************/
37 
39  SAnchor* ps_anchor,
40  const std::shared_ptr<btCollisionShape>& ptr_shape,
41  const SData& s_data) :
42  m_cModel(c_model),
43  m_psAnchor(ps_anchor),
44  m_ptrShape(ptr_shape),
45  m_sData(s_data) {}
46 
47  /****************************************/
48  /****************************************/
49 
51  return m_cModel;
52  }
53 
54  /****************************************/
55  /****************************************/
56 
58  return *m_psAnchor;
59  }
60 
61  /****************************************/
62  /****************************************/
63 
65  return *(m_ptrShape.get());
66  }
67 
68  /****************************************/
69  /****************************************/
70 
72  return m_sData;
73  }
74 
75  /****************************************/
76  /****************************************/
77 
79  if(m_psAnchor != nullptr) {
80  /* Offset the world transform to respect ARGoS conventions */
81  btTransform cTransform =
82  GetTransform() * m_sData.CenterOfMassOffset;
83  /* Retrieve the position and orientation */
84  const btVector3& cPosition = cTransform.getOrigin();
85  const btQuaternion& cOrientation = cTransform.getRotation();
86  /* Swap coordinate system and set anchor position */
87  m_psAnchor->Position.Set(cPosition.getX(), -cPosition.getZ(), cPosition.getY());
88  /* Swap coordinate system and set anchor orientation */
89  m_psAnchor->Orientation.Set(cOrientation.getW(),
90  cOrientation.getX(),
91  -cOrientation.getZ(),
92  cOrientation.getY());
93  }
94  }
95 
96  /****************************************/
97  /****************************************/
98 
100  CComposableEntity& c_entity) :
101  CPhysicsModel(c_engine, c_entity.GetComponent<CEmbodiedEntity>("body")),
102  m_cEngine(c_engine),
103  m_cComposableEntity(c_entity) {}
104 
105  /****************************************/
106  /****************************************/
107 
109  /* Rerun collision detection */
110  m_cEngine.GetWorld().performDiscreteCollisionDetection();
111  /* Get the collision dispatcher */
112  const btCollisionDispatcher& cDispatcher =
113  m_cEngine.GetDispatcher();
114  /* For each manifold from the collision dispatcher */
115  for(SInt32 i = 0; i < cDispatcher.getNumManifolds(); i++) {
116  const btPersistentManifold* pcContactManifold =
117  cDispatcher.getManifoldByIndexInternal(i);
118  const auto* pcModelA =
119  static_cast<const CDynamics3DModel*>(pcContactManifold->getBody0()->getUserPointer());
120  const auto* pcModelB =
121  static_cast<const CDynamics3DModel*>(pcContactManifold->getBody1()->getUserPointer());
122  /* Ignore self-collisions */
123  if(pcModelA == pcModelB) {
124  continue;
125  }
126  /* Ignore collisions that don't belong to a model (e.g. a collision with the ground) */
127  if((pcModelA == nullptr) || (pcModelB == nullptr)) {
128  continue;
129  }
130  /* Only check collisions that involve this model */
131  if((pcModelA == this) || (pcModelB == this)) {
132  /* One of the two bodies involved in the contact manifold belongs to this model,
133  check for contact points with negative distance to indicate a collision */
134  for(SInt32 j = 0; j < pcContactManifold->getNumContacts(); j++) {
135  const btManifoldPoint& cManifoldPoint = pcContactManifold->getContactPoint(j);
136  if (cManifoldPoint.getDistance() < 0.0f) {
137  return true;
138  }
139  }
140  }
141  }
142  return false;
143  }
144 
145  /****************************************/
146  /****************************************/
147 
149  /* Update the anchor associated with each body */
150  for(const std::shared_ptr<CAbstractBody>& ptr_body : m_vecBodies) {
151  ptr_body->UpdateAnchor();
152  }
153  /* Call CPhysicsModel::UpdateEntityStatus which updates the AABB and origin anchor */
155  }
156 
157  /****************************************/
158  /****************************************/
159 
160 }
161 
signed int SInt32
32-bit signed integer.
Definition: datatypes.h:93
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
Basic class for an entity that contains other entities.
This entity is a link to a body in the physics engine.
An anchor related to the body of an entity.
Definition: physics_model.h:38
virtual void UpdateEntityStatus()
Updates the status of the associated entity.
btMultiBodyDynamicsWorld & GetWorld()
btCollisionDispatcher & GetDispatcher()
virtual bool IsCollidingWithSomething() const
Returns true if this model is colliding with another model.
std::vector< std::shared_ptr< CAbstractBody > > m_vecBodies
virtual void UpdateEntityStatus()
Updates the status of the associated entity.
CDynamics3DModel(CDynamics3DEngine &c_engine, CComposableEntity &c_entity)
std::shared_ptr< btCollisionShape > m_ptrShape
CAbstractBody(CDynamics3DModel &c_model, SAnchor *ps_anchor, const std::shared_ptr< btCollisionShape > &ptr_shape, const SData &s_data)
SData(const btTransform &c_start_transform, const btTransform &c_center_of_mass_offset, const btVector3 &c_inertia, btScalar f_mass, btScalar f_friction, const std::vector< SDipole > &vec_dipoles=std::vector< SDipole >())