2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2013 Erwin Coumans http://bulletphysics.org
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:
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.
16 #ifndef BT_FEATHERSTONE_LINK_COLLIDER_H
17 #define BT_FEATHERSTONE_LINK_COLLIDER_H
19 #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
21 #include "btMultiBody.h"
22 #include "LinearMath/btSerializer.h"
24 #ifdef BT_USE_DOUBLE_PRECISION
25 #define btMultiBodyLinkColliderData btMultiBodyLinkColliderDoubleData
26 #define btMultiBodyLinkColliderDataName "btMultiBodyLinkColliderDoubleData"
28 #define btMultiBodyLinkColliderData btMultiBodyLinkColliderFloatData
29 #define btMultiBodyLinkColliderDataName "btMultiBodyLinkColliderFloatData"
32 class btMultiBodyLinkCollider : public btCollisionObject
36 btMultiBody* m_multiBody;
39 virtual ~btMultiBodyLinkCollider()
43 btMultiBodyLinkCollider(btMultiBody* multiBody, int link)
44 : m_multiBody(multiBody),
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()))
52 m_collisionFlags &= (~btCollisionObject::CF_STATIC_OBJECT);
56 // m_collisionFlags |= (btCollisionObject::CF_STATIC_OBJECT);
59 m_internalType = CO_FEATHERSTONE_LINK;
61 static btMultiBodyLinkCollider* upcast(btCollisionObject* colObj)
63 if (colObj->getInternalType() & btCollisionObject::CO_FEATHERSTONE_LINK)
64 return (btMultiBodyLinkCollider*)colObj;
67 static const btMultiBodyLinkCollider* upcast(const btCollisionObject* colObj)
69 if (colObj->getInternalType() & btCollisionObject::CO_FEATHERSTONE_LINK)
70 return (btMultiBodyLinkCollider*)colObj;
74 virtual bool checkCollideWithOverride(const btCollisionObject* co) const
76 const btMultiBodyLinkCollider* other = btMultiBodyLinkCollider::upcast(co);
79 if (other->m_multiBody != this->m_multiBody)
81 if (!m_multiBody->hasSelfCollision())
84 //check if 'link' has collision disabled
87 const btMultibodyLink& link = m_multiBody->getLink(this->m_link);
88 if (link.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION)
90 int parent_of_this = m_link;
93 if (parent_of_this == -1)
95 parent_of_this = m_multiBody->getLink(parent_of_this).m_parent;
96 if (parent_of_this == other->m_link)
102 else if (link.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION)
104 if (link.m_parent == other->m_link)
109 if (other->m_link >= 0)
111 const btMultibodyLink& otherLink = other->m_multiBody->getLink(other->m_link);
112 if (otherLink.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION)
114 int parent_of_other = other->m_link;
117 if (parent_of_other == -1)
119 parent_of_other = m_multiBody->getLink(parent_of_other).m_parent;
120 if (parent_of_other == this->m_link)
124 else if (otherLink.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION)
126 if (otherLink.m_parent == this->m_link)
133 bool isStaticOrKinematic() const
135 return isStaticOrKinematicObject();
138 bool isKinematic() const
140 return isKinematicObject();
143 void setDynamicType(int dynamicType)
145 int oldFlags = getCollisionFlags();
146 oldFlags &= ~(btCollisionObject::CF_STATIC_OBJECT | btCollisionObject::CF_KINEMATIC_OBJECT);
147 setCollisionFlags(oldFlags | dynamicType);
150 virtual int calculateSerializeBufferSize() const;
152 ///fills the dataBuffer and returns the struct name (and 0 on failure)
153 virtual const char* serialize(void* dataBuffer, class btSerializer* serializer) const;
158 struct btMultiBodyLinkColliderFloatData
160 btCollisionObjectFloatData m_colObjData;
161 btMultiBodyFloatData *m_multiBody;
166 struct btMultiBodyLinkColliderDoubleData
168 btCollisionObjectDoubleData m_colObjData;
169 btMultiBodyDoubleData *m_multiBody;
176 SIMD_FORCE_INLINE int btMultiBodyLinkCollider::calculateSerializeBufferSize() const
178 return sizeof(btMultiBodyLinkColliderData);
181 SIMD_FORCE_INLINE const char* btMultiBodyLinkCollider::serialize(void* dataBuffer, class btSerializer* serializer) const
183 btMultiBodyLinkColliderData* dataOut = (btMultiBodyLinkColliderData*)dataBuffer;
184 btCollisionObject::serialize(&dataOut->m_colObjData, serializer);
186 dataOut->m_link = this->m_link;
187 dataOut->m_multiBody = (btMultiBodyData*)serializer->getUniquePointer(m_multiBody);
189 // Fill padding with zeros to appease msan.
190 memset(dataOut->m_padding, 0, sizeof(dataOut->m_padding));
192 return btMultiBodyLinkColliderDataName;
195 #endif //BT_FEATHERSTONE_LINK_COLLIDER_H