Bullet Collision Detection & Physics Library: btMultiBodyLinkCollider.h Source File

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
19 #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
20
21 #include "btMultiBody.h"
22 #include "LinearMath/btSerializer.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
32 class btMultiBodyLinkCollider : public btCollisionObject
33 {
34 //protected:
35 public:
36 btMultiBody* m_multiBody;
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 {
48 m_collisionFlags &= (~btCollisionObject::CF_STATIC_OBJECT);
49 }
50 // else
51 //{
52 // m_collisionFlags |= (btCollisionObject::CF_STATIC_OBJECT);
53 //}
54
55 m_internalType = CO_FEATHERSTONE_LINK;
56 }
57 static btMultiBodyLinkCollider* upcast(btCollisionObject* colObj)
58 {
59 if (colObj->getInternalType() & btCollisionObject::CO_FEATHERSTONE_LINK)
60 return (btMultiBodyLinkCollider*)colObj;
61 return 0;
62 }
63 static const btMultiBodyLinkCollider* upcast(const btCollisionObject* colObj)
64 {
65 if (colObj->getInternalType() & btCollisionObject::CO_FEATHERSTONE_LINK)
66 return (btMultiBodyLinkCollider*)colObj;
67 return 0;
68 }
69
70 virtual bool checkCollideWithOverride(const btCollisionObject* co) const
71 {
72 const btMultiBodyLinkCollider* other = btMultiBodyLinkCollider::upcast(co);
73 if (!other)
74 return true;
75 if (other->m_multiBody != this->m_multiBody)
76 return true;
77 if (!m_multiBody->hasSelfCollision())
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);
84 if (link.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION)
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 }
98 else if (link.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION)
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);
108 if (otherLink.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION)
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 }
120 else if (otherLink.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION)
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
137 struct btMultiBodyLinkColliderFloatData
138 {
139 btCollisionObjectFloatData m_colObjData;
140 btMultiBodyFloatData *m_multiBody;
141 int m_link;
142 char m_padding[4];
143 };
144
145 struct btMultiBodyLinkColliderDoubleData
146 {
147 btCollisionObjectDoubleData m_colObjData;
148 btMultiBodyDoubleData *m_multiBody;
149 int m_link;
150 char m_padding[4];
151 };
152
153 // clang-format on
154
155 SIMD_FORCE_INLINE int btMultiBodyLinkCollider::calculateSerializeBufferSize() const
156 {
157 return sizeof(btMultiBodyLinkColliderData);
158 }
159
160 SIMD_FORCE_INLINE const char* btMultiBodyLinkCollider::serialize(void* dataBuffer, class btSerializer* serializer) const
161 {
162 btMultiBodyLinkColliderData* dataOut = (btMultiBodyLinkColliderData*)dataBuffer;
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
171 return btMultiBodyLinkColliderDataName;
172 }
173
174 #endif //BT_FEATHERSTONE_LINK_COLLIDER_H
btMultiBodyLinkColliderFloatData
Definition: btMultiBodyLinkCollider.h:137
btMultiBodyLinkCollider::checkCollideWithOverride
virtual bool checkCollideWithOverride(const btCollisionObject *co) const
Definition: btMultiBodyLinkCollider.h:70
btCollisionObjectFloatData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btCollisionObject.h:626
btMultiBodyLinkCollider::upcast
static btMultiBodyLinkCollider * upcast(btCollisionObject *colObj)
Definition: btMultiBodyLinkCollider.h:57
btCollisionObject::serialize
virtual const char * serialize(void *dataBuffer, class btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
Definition: btCollisionObject.cpp:80
BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION
Definition: btMultiBodyLink.h:25
btSerializer::getUniquePointer
virtual void * getUniquePointer(void *oldPtr)=0
btMultiBodyLinkColliderDataName
#define btMultiBodyLinkColliderDataName
Definition: btMultiBodyLinkCollider.h:29
btMultiBodyLinkColliderDoubleData::m_colObjData
btCollisionObjectDoubleData m_colObjData
Definition: btMultiBodyLinkCollider.h:147
btMultiBodyDoubleData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btMultiBody.h:789
BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION
Definition: btMultiBodyLink.h:26
btMultiBodyLinkColliderFloatData::m_padding
char m_padding[4]
Definition: btMultiBodyLinkCollider.h:142
btMultiBodyLinkColliderFloatData::m_colObjData
btCollisionObjectFloatData m_colObjData
Definition: btMultiBodyLinkCollider.h:139
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:83
btCollisionObject::CF_STATIC_OBJECT
Definition: btCollisionObject.h:128
btMultibodyLink
Definition: btMultiBodyLink.h:47
btMultiBody
Definition: btMultiBody.h:49
btMultiBodyLinkColliderFloatData::m_multiBody
btMultiBodyFloatData * m_multiBody
Definition: btMultiBodyLinkCollider.h:140
btSerializer
Definition: btSerializer.h:65
btCollisionObject::~btCollisionObject
virtual ~btCollisionObject()
Definition: btCollisionObject.cpp:56
btMultiBodyLinkCollider::btMultiBodyLinkCollider
btMultiBodyLinkCollider(btMultiBody *multiBody, int link)
Definition: btMultiBodyLinkCollider.h:39
btCollisionObject::CO_FEATHERSTONE_LINK
Definition: btCollisionObject.h:151
btMultiBodyLinkColliderDoubleData
Definition: btMultiBodyLinkCollider.h:145
btMultiBodyLinkColliderDoubleData::m_link
int m_link
Definition: btMultiBodyLinkCollider.h:149
btCollisionObjectDoubleData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btCollisionObject.h:591
btMultiBodyLinkColliderData
#define btMultiBodyLinkColliderData
Definition: btMultiBodyLinkCollider.h:28
btCollisionObject::m_internalType
int m_internalType
m_internalType is reserved to distinguish Bullet&#39;s btCollisionObject, btRigidBody, btSoftBody, btGhostObject etc.
Definition: btCollisionObject.h:94
btMultiBodyLinkCollider::calculateSerializeBufferSize
virtual int calculateSerializeBufferSize() const
Definition: btMultiBodyLinkCollider.h:155
btMultiBodyLinkCollider::upcast
static const btMultiBodyLinkCollider * upcast(const btCollisionObject *colObj)
Definition: btMultiBodyLinkCollider.h:63
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition: btCollisionObject.h:48
btCollisionObject::m_collisionFlags
int m_collisionFlags
Definition: btCollisionObject.h:76
btMultiBodyLinkColliderFloatData::m_link
int m_link
Definition: btMultiBodyLinkCollider.h:141
btMultiBody::getLink
const btMultibodyLink & getLink(int index) const
Definition: btMultiBody.h:114
btMultibodyLink::m_flags
int m_flags
Definition: btMultiBodyLink.h:128
btMultiBody.h
btMultiBody::hasSelfCollision
bool hasSelfCollision() const
Definition: btMultiBody.h:533
btMultiBodyLinkCollider
Definition: btMultiBodyLinkCollider.h:32
btMultiBodyData
#define btMultiBodyData
serialization data, don&#39;t change them if you are not familiar with the details of the serialization m...
Definition: btMultiBody.h:40
btCollisionObject::getInternalType
int getInternalType() const
reserved for Bullet internal usage
Definition: btCollisionObject.h:360
btCollisionObject.h
btCollisionObject::m_checkCollideWith
int m_checkCollideWith
If some object should have elaborate collision filtering by sub-classes.
Definition: btCollisionObject.h:114
btMultiBodyLinkCollider::serialize
virtual const char * serialize(void *dataBuffer, class btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
Definition: btMultiBodyLinkCollider.h:160
btMultiBodyLinkCollider::m_link
int m_link
Definition: btMultiBodyLinkCollider.h:37
btMultiBodyFloatData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btMultiBody.h:806
btMultiBodyLinkCollider::m_multiBody
btMultiBody * m_multiBody
Definition: btMultiBodyLinkCollider.h:36
btMultibodyLink::m_parent
int m_parent
Definition: btMultiBodyLink.h:54
btMultiBodyLinkColliderDoubleData::m_multiBody
btMultiBodyDoubleData * m_multiBody
Definition: btMultiBodyLinkCollider.h:148
btSerializer.h
btMultiBodyLinkColliderDoubleData::m_padding
char m_padding[4]
Definition: btMultiBodyLinkCollider.h:150