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

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btMultiBodyLinkCollider.h
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2013 Erwin Coumans http://bulletphysics.org
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. 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:
35public:
36 btMultiBody* m_multiBody;
37 int m_link;
38
39 virtual ~btMultiBodyLinkCollider()
40 {
41
42 }
43 btMultiBodyLinkCollider(btMultiBody* multiBody, int link)
44 : m_multiBody(multiBody),
45 m_link(link)
46 {
47 m_checkCollideWith = true;
48 //we need to remove the 'CF_STATIC_OBJECT' flag, otherwise links/base doesn't merge islands
49 //this means that some constraints might point to bodies that are not in the islands, causing crashes
50 //if (link>=0 || (multiBody && !multiBody->hasFixedBase()))
51 {
52 m_collisionFlags &= (~btCollisionObject::CF_STATIC_OBJECT);
53 }
54 // else
55 //{
56 // m_collisionFlags |= (btCollisionObject::CF_STATIC_OBJECT);
57 //}
58
59 m_internalType = CO_FEATHERSTONE_LINK;
60 }
61 static btMultiBodyLinkCollider* upcast(btCollisionObject* colObj)
62 {
63 if (colObj->getInternalType() & btCollisionObject::CO_FEATHERSTONE_LINK)
64 return (btMultiBodyLinkCollider*)colObj;
65 return 0;
66 }
67 static const btMultiBodyLinkCollider* upcast(const btCollisionObject* colObj)
68 {
69 if (colObj->getInternalType() & btCollisionObject::CO_FEATHERSTONE_LINK)
70 return (btMultiBodyLinkCollider*)colObj;
71 return 0;
72 }
73
74 virtual bool checkCollideWithOverride(const btCollisionObject* co) const
75 {
76 const btMultiBodyLinkCollider* other = btMultiBodyLinkCollider::upcast(co);
77 if (!other)
78 return true;
79 if (other->m_multiBody != this->m_multiBody)
80 return true;
81 if (!m_multiBody->hasSelfCollision())
82 return false;
83
84 //check if 'link' has collision disabled
85 if (m_link >= 0)
86 {
87 const btMultibodyLink& link = m_multiBody->getLink(this->m_link);
88 if (link.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION)
89 {
90 int parent_of_this = m_link;
91 while (1)
92 {
93 if (parent_of_this == -1)
94 break;
95 parent_of_this = m_multiBody->getLink(parent_of_this).m_parent;
96 if (parent_of_this == other->m_link)
97 {
98 return false;
99 }
100 }
101 }
102 else if (link.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION)
103 {
104 if (link.m_parent == other->m_link)
105 return false;
106 }
107 }
108
109 if (other->m_link >= 0)
110 {
111 const btMultibodyLink& otherLink = other->m_multiBody->getLink(other->m_link);
112 if (otherLink.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION)
113 {
114 int parent_of_other = other->m_link;
115 while (1)
116 {
117 if (parent_of_other == -1)
118 break;
119 parent_of_other = m_multiBody->getLink(parent_of_other).m_parent;
120 if (parent_of_other == this->m_link)
121 return false;
122 }
123 }
124 else if (otherLink.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION)
125 {
126 if (otherLink.m_parent == this->m_link)
127 return false;
128 }
129 }
130 return true;
131 }
132
133 bool isStaticOrKinematic() const
134 {
135 return isStaticOrKinematicObject();
136 }
137
138 bool isKinematic() const
139 {
140 return isKinematicObject();
141 }
142
143 void setDynamicType(int dynamicType)
144 {
145 int oldFlags = getCollisionFlags();
146 oldFlags &= ~(btCollisionObject::CF_STATIC_OBJECT | btCollisionObject::CF_KINEMATIC_OBJECT);
147 setCollisionFlags(oldFlags | dynamicType);
148 }
149
150 virtual int calculateSerializeBufferSize() const;
151
153 virtual const char* serialize(void* dataBuffer, class btSerializer* serializer) const;
154};
155
156// clang-format off
157
158 struct btMultiBodyLinkColliderFloatData
159{
160 btCollisionObjectFloatData m_colObjData;
161 btMultiBodyFloatData *m_multiBody;
162 int m_link;
163 char m_padding[4];
164};
165
166 struct btMultiBodyLinkColliderDoubleData
167{
168 btCollisionObjectDoubleData m_colObjData;
169 btMultiBodyDoubleData *m_multiBody;
170 int m_link;
171 char m_padding[4];
172};
173
174// clang-format on
175
176 SIMD_FORCE_INLINE int btMultiBodyLinkCollider::calculateSerializeBufferSize() const
177{
178 return sizeof(btMultiBodyLinkColliderData);
179}
180
181 SIMD_FORCE_INLINE const char* btMultiBodyLinkCollider::serialize(void* dataBuffer, class btSerializer* serializer) const
182{
183 btMultiBodyLinkColliderData* dataOut = (btMultiBodyLinkColliderData*)dataBuffer;
184 btCollisionObject::serialize(&dataOut->m_colObjData, serializer);
185
186 dataOut->m_link = this->m_link;
187 dataOut->m_multiBody = (btMultiBodyData*)serializer->getUniquePointer(m_multiBody);
188
189 // Fill padding with zeros to appease msan.
190 memset(dataOut->m_padding, 0, sizeof(dataOut->m_padding));
191
192 return btMultiBodyLinkColliderDataName;
193}
194
195#endif //BT_FEATHERSTONE_LINK_COLLIDER_H
btCollisionObject.h
btMultiBodyLinkColliderData
#define btMultiBodyLinkColliderData
Definition btMultiBodyLinkCollider.h:28
btMultiBodyLinkColliderDataName
#define btMultiBodyLinkColliderDataName
Definition btMultiBodyLinkCollider.h:29
BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION
@ BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION
Definition btMultiBodyLink.h:26
BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION
@ BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION
Definition btMultiBodyLink.h:25
btMultiBody.h
btMultiBodyData
#define btMultiBodyData
serialization data, don't change them if you are not familiar with the details of the serialization m...
Definition btMultiBody.h:40
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition btScalar.h:98
btSerializer.h
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition btCollisionObject.h:51
btCollisionObject::CF_STATIC_OBJECT
@ CF_STATIC_OBJECT
Definition btCollisionObject.h:132
btCollisionObject::CF_KINEMATIC_OBJECT
@ CF_KINEMATIC_OBJECT
Definition btCollisionObject.h:133
btCollisionObject::isStaticOrKinematicObject
bool isStaticOrKinematicObject() const
Definition btCollisionObject.h:207
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:81
btCollisionObject::CO_FEATHERSTONE_LINK
@ CO_FEATHERSTONE_LINK
Definition btCollisionObject.h:155
btCollisionObject::setCollisionFlags
void setCollisionFlags(int flags)
Definition btCollisionObject.h:494
btCollisionObject::m_checkCollideWith
int m_checkCollideWith
If some object should have elaborate collision filtering by sub-classes.
Definition btCollisionObject.h:117
btCollisionObject::getInternalType
int getInternalType() const
reserved for Bullet internal usage
Definition btCollisionObject.h:374
btCollisionObject::m_internalType
int m_internalType
m_internalType is reserved to distinguish Bullet's btCollisionObject, btRigidBody,...
Definition btCollisionObject.h:95
btCollisionObject::isKinematicObject
bool isKinematicObject() const
Definition btCollisionObject.h:202
btCollisionObject::~btCollisionObject
virtual ~btCollisionObject()
Definition btCollisionObject.cpp:57
btCollisionObject::m_collisionFlags
int m_collisionFlags
Definition btCollisionObject.h:77
btCollisionObject::getCollisionFlags
int getCollisionFlags() const
Definition btCollisionObject.h:489
btMultiBodyLinkCollider
Definition btMultiBodyLinkCollider.h:33
btMultiBodyLinkCollider::btMultiBodyLinkCollider
btMultiBodyLinkCollider(btMultiBody *multiBody, int link)
Definition btMultiBodyLinkCollider.h:43
btMultiBodyLinkCollider::upcast
static const btMultiBodyLinkCollider * upcast(const btCollisionObject *colObj)
Definition btMultiBodyLinkCollider.h:67
btMultiBodyLinkCollider::calculateSerializeBufferSize
virtual int calculateSerializeBufferSize() const
Definition btMultiBodyLinkCollider.h:176
btMultiBodyLinkCollider::upcast
static btMultiBodyLinkCollider * upcast(btCollisionObject *colObj)
Definition btMultiBodyLinkCollider.h:61
btMultiBodyLinkCollider::~btMultiBodyLinkCollider
virtual ~btMultiBodyLinkCollider()
Definition btMultiBodyLinkCollider.h:39
btMultiBodyLinkCollider::m_link
int m_link
Definition btMultiBodyLinkCollider.h:37
btMultiBodyLinkCollider::isStaticOrKinematic
bool isStaticOrKinematic() const
Definition btMultiBodyLinkCollider.h:133
btMultiBodyLinkCollider::checkCollideWithOverride
virtual bool checkCollideWithOverride(const btCollisionObject *co) const
Definition btMultiBodyLinkCollider.h:74
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:181
btMultiBodyLinkCollider::m_multiBody
btMultiBody * m_multiBody
Definition btMultiBodyLinkCollider.h:36
btMultiBodyLinkCollider::isKinematic
bool isKinematic() const
Definition btMultiBodyLinkCollider.h:138
btMultiBodyLinkCollider::setDynamicType
void setDynamicType(int dynamicType)
Definition btMultiBodyLinkCollider.h:143
btMultiBody
Definition btMultiBody.h:51
btMultiBody::hasSelfCollision
bool hasSelfCollision() const
Definition btMultiBody.h:635
btMultiBody::getLink
const btMultibodyLink & getLink(int index) const
Definition btMultiBody.h:114
btSerializer
Definition btSerializer.h:66
btSerializer::getUniquePointer
virtual void * getUniquePointer(void *oldPtr)=0
btCollisionObjectDoubleData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
Definition btCollisionObject.h:616
btCollisionObjectFloatData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
Definition btCollisionObject.h:651
btMultiBodyDoubleData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
Definition btMultiBody.h:921
btMultiBodyFloatData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
Definition btMultiBody.h:938
btMultiBodyLinkColliderDoubleData
Definition btMultiBodyLinkCollider.h:167
btMultiBodyLinkColliderDoubleData::m_padding
char m_padding[4]
Definition btMultiBodyLinkCollider.h:171
btMultiBodyLinkColliderDoubleData::m_link
int m_link
Definition btMultiBodyLinkCollider.h:170
btMultiBodyLinkColliderDoubleData::m_multiBody
btMultiBodyDoubleData * m_multiBody
Definition btMultiBodyLinkCollider.h:169
btMultiBodyLinkColliderDoubleData::m_colObjData
btCollisionObjectDoubleData m_colObjData
Definition btMultiBodyLinkCollider.h:168
btMultiBodyLinkColliderFloatData
Definition btMultiBodyLinkCollider.h:159
btMultiBodyLinkColliderFloatData::m_padding
char m_padding[4]
Definition btMultiBodyLinkCollider.h:163
btMultiBodyLinkColliderFloatData::m_link
int m_link
Definition btMultiBodyLinkCollider.h:162
btMultiBodyLinkColliderFloatData::m_multiBody
btMultiBodyFloatData * m_multiBody
Definition btMultiBodyLinkCollider.h:161
btMultiBodyLinkColliderFloatData::m_colObjData
btCollisionObjectFloatData m_colObjData
Definition btMultiBodyLinkCollider.h:160
btMultibodyLink
Definition btMultiBodyLink.h:48
btMultibodyLink::m_flags
int m_flags
Definition btMultiBodyLink.h:133
btMultibodyLink::m_parent
int m_parent
Definition btMultiBodyLink.h:54