Bullet Collision Detection & Physics Library
btMultiBodyLinkCollider.h
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2013 Erwin Coumans http://bulletphysics.org
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 
16 #ifndef BT_FEATHERSTONE_LINK_COLLIDER_H
17 #define BT_FEATHERSTONE_LINK_COLLIDER_H
18 
20 
21 #include "btMultiBody.h"
23 
24 #ifdef BT_USE_DOUBLE_PRECISION
25 #define btMultiBodyLinkColliderData btMultiBodyLinkColliderDoubleData
26 #define btMultiBodyLinkColliderDataName "btMultiBodyLinkColliderDoubleData"
27 #else
28 #define btMultiBodyLinkColliderData btMultiBodyLinkColliderFloatData
29 #define btMultiBodyLinkColliderDataName "btMultiBodyLinkColliderFloatData"
30 #endif
31 
33 {
34  //protected:
35 public:
37  int m_link;
38 
39  btMultiBodyLinkCollider(btMultiBody* multiBody, int link)
40  : m_multiBody(multiBody),
41  m_link(link)
42  {
43  m_checkCollideWith = true;
44  //we need to remove the 'CF_STATIC_OBJECT' flag, otherwise links/base doesn't merge islands
45  //this means that some constraints might point to bodies that are not in the islands, causing crashes
46  //if (link>=0 || (multiBody && !multiBody->hasFixedBase()))
47  {
49  }
50  // else
51  //{
52  // m_collisionFlags |= (btCollisionObject::CF_STATIC_OBJECT);
53  //}
54 
56  }
58  {
60  return (btMultiBodyLinkCollider*)colObj;
61  return 0;
62  }
63  static const btMultiBodyLinkCollider* upcast(const btCollisionObject* colObj)
64  {
66  return (btMultiBodyLinkCollider*)colObj;
67  return 0;
68  }
69 
70  virtual bool checkCollideWithOverride(const btCollisionObject* co) const
71  {
73  if (!other)
74  return true;
75  if (other->m_multiBody != this->m_multiBody)
76  return true;
78  return false;
79 
80  //check if 'link' has collision disabled
81  if (m_link >= 0)
82  {
83  const btMultibodyLink& link = m_multiBody->getLink(this->m_link);
85  {
86  int parent_of_this = m_link;
87  while (1)
88  {
89  if (parent_of_this == -1)
90  break;
91  parent_of_this = m_multiBody->getLink(parent_of_this).m_parent;
92  if (parent_of_this == other->m_link)
93  {
94  return false;
95  }
96  }
97  }
99  {
100  if (link.m_parent == other->m_link)
101  return false;
102  }
103  }
104 
105  if (other->m_link >= 0)
106  {
107  const btMultibodyLink& otherLink = other->m_multiBody->getLink(other->m_link);
109  {
110  int parent_of_other = other->m_link;
111  while (1)
112  {
113  if (parent_of_other == -1)
114  break;
115  parent_of_other = m_multiBody->getLink(parent_of_other).m_parent;
116  if (parent_of_other == this->m_link)
117  return false;
118  }
119  }
121  {
122  if (otherLink.m_parent == this->m_link)
123  return false;
124  }
125  }
126  return true;
127  }
128 
129  virtual int calculateSerializeBufferSize() const;
130 
132  virtual const char* serialize(void* dataBuffer, class btSerializer* serializer) const;
133 };
134 
135 // clang-format off
136 
138 {
141  int m_link;
142  char m_padding[4];
143 };
144 
146 {
149  int m_link;
150  char m_padding[4];
151 };
152 
153 // clang-format on
154 
156 {
157  return sizeof(btMultiBodyLinkColliderData);
158 }
159 
160 SIMD_FORCE_INLINE const char* btMultiBodyLinkCollider::serialize(void* dataBuffer, class btSerializer* serializer) const
161 {
163  btCollisionObject::serialize(&dataOut->m_colObjData, serializer);
164 
165  dataOut->m_link = this->m_link;
166  dataOut->m_multiBody = (btMultiBodyData*)serializer->getUniquePointer(m_multiBody);
167 
168  // Fill padding with zeros to appease msan.
169  memset(dataOut->m_padding, 0, sizeof(dataOut->m_padding));
170 
172 }
173 
174 #endif //BT_FEATHERSTONE_LINK_COLLIDER_H
virtual bool checkCollideWithOverride(const btCollisionObject *co) const
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
static btMultiBodyLinkCollider * upcast(btCollisionObject *colObj)
virtual const char * serialize(void *dataBuffer, class btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
virtual void * getUniquePointer(void *oldPtr)=0
#define btMultiBodyLinkColliderDataName
btCollisionObjectDoubleData m_colObjData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btMultiBody.h:789
btCollisionObjectFloatData m_colObjData
#define SIMD_FORCE_INLINE
Definition: btScalar.h:83
btMultiBodyLinkCollider(btMultiBody *multiBody, int link)
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
#define btMultiBodyLinkColliderData
int m_internalType
m_internalType is reserved to distinguish Bullet's btCollisionObject, btRigidBody, btSoftBody, btGhostObject etc.
virtual int calculateSerializeBufferSize() const
static const btMultiBodyLinkCollider * upcast(const btCollisionObject *colObj)
btCollisionObject can be used to manage collision detection objects.
const btMultibodyLink & getLink(int index) const
Definition: btMultiBody.h:114
bool hasSelfCollision() const
Definition: btMultiBody.h:533
#define btMultiBodyData
serialization data, don't change them if you are not familiar with the details of the serialization m...
Definition: btMultiBody.h:40
int getInternalType() const
reserved for Bullet internal usage
int m_checkCollideWith
If some object should have elaborate collision filtering by sub-classes.
virtual const char * serialize(void *dataBuffer, class btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btMultiBody.h:806