btMultiBodyLinkCollider.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2013 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_FEATHERSTONE_LINK_COLLIDER_H
  14. #define BT_FEATHERSTONE_LINK_COLLIDER_H
  15. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  16. #include "btMultiBody.h"
  17. #include "LinearMath/btSerializer.h"
  18. #ifdef BT_USE_DOUBLE_PRECISION
  19. #define btMultiBodyLinkColliderData btMultiBodyLinkColliderDoubleData
  20. #define btMultiBodyLinkColliderDataName "btMultiBodyLinkColliderDoubleData"
  21. #else
  22. #define btMultiBodyLinkColliderData btMultiBodyLinkColliderFloatData
  23. #define btMultiBodyLinkColliderDataName "btMultiBodyLinkColliderFloatData"
  24. #endif
  25. class btMultiBodyLinkCollider : public btCollisionObject
  26. {
  27. //protected:
  28. public:
  29. btMultiBody* m_multiBody;
  30. int m_link;
  31. virtual ~btMultiBodyLinkCollider()
  32. {
  33. }
  34. btMultiBodyLinkCollider(btMultiBody* multiBody, int link)
  35. : m_multiBody(multiBody),
  36. m_link(link)
  37. {
  38. m_checkCollideWith = true;
  39. //we need to remove the 'CF_STATIC_OBJECT' flag, otherwise links/base doesn't merge islands
  40. //this means that some constraints might point to bodies that are not in the islands, causing crashes
  41. //if (link>=0 || (multiBody && !multiBody->hasFixedBase()))
  42. {
  43. m_collisionFlags &= (~btCollisionObject::CF_STATIC_OBJECT);
  44. }
  45. // else
  46. //{
  47. // m_collisionFlags |= (btCollisionObject::CF_STATIC_OBJECT);
  48. //}
  49. m_internalType = CO_FEATHERSTONE_LINK;
  50. }
  51. static btMultiBodyLinkCollider* upcast(btCollisionObject* colObj)
  52. {
  53. if (colObj->getInternalType() & btCollisionObject::CO_FEATHERSTONE_LINK)
  54. return (btMultiBodyLinkCollider*)colObj;
  55. return 0;
  56. }
  57. static const btMultiBodyLinkCollider* upcast(const btCollisionObject* colObj)
  58. {
  59. if (colObj->getInternalType() & btCollisionObject::CO_FEATHERSTONE_LINK)
  60. return (btMultiBodyLinkCollider*)colObj;
  61. return 0;
  62. }
  63. virtual bool checkCollideWithOverride(const btCollisionObject* co) const
  64. {
  65. const btMultiBodyLinkCollider* other = btMultiBodyLinkCollider::upcast(co);
  66. if (!other)
  67. return true;
  68. if (other->m_multiBody != this->m_multiBody)
  69. return true;
  70. if (!m_multiBody->hasSelfCollision())
  71. return false;
  72. //check if 'link' has collision disabled
  73. if (m_link >= 0)
  74. {
  75. const btMultibodyLink& link = m_multiBody->getLink(this->m_link);
  76. if (link.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION)
  77. {
  78. int parent_of_this = m_link;
  79. while (1)
  80. {
  81. if (parent_of_this == -1)
  82. break;
  83. parent_of_this = m_multiBody->getLink(parent_of_this).m_parent;
  84. if (parent_of_this == other->m_link)
  85. {
  86. return false;
  87. }
  88. }
  89. }
  90. else if (link.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION)
  91. {
  92. if (link.m_parent == other->m_link)
  93. return false;
  94. }
  95. }
  96. if (other->m_link >= 0)
  97. {
  98. const btMultibodyLink& otherLink = other->m_multiBody->getLink(other->m_link);
  99. if (otherLink.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION)
  100. {
  101. int parent_of_other = other->m_link;
  102. while (1)
  103. {
  104. if (parent_of_other == -1)
  105. break;
  106. parent_of_other = m_multiBody->getLink(parent_of_other).m_parent;
  107. if (parent_of_other == this->m_link)
  108. return false;
  109. }
  110. }
  111. else if (otherLink.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION)
  112. {
  113. if (otherLink.m_parent == this->m_link)
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. bool isStaticOrKinematic() const
  120. {
  121. return isStaticOrKinematicObject();
  122. }
  123. bool isKinematic() const
  124. {
  125. return isKinematicObject();
  126. }
  127. void setDynamicType(int dynamicType)
  128. {
  129. int oldFlags = getCollisionFlags();
  130. oldFlags &= ~(btCollisionObject::CF_STATIC_OBJECT | btCollisionObject::CF_KINEMATIC_OBJECT);
  131. setCollisionFlags(oldFlags | dynamicType);
  132. }
  133. virtual int calculateSerializeBufferSize() const;
  134. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  135. virtual const char* serialize(void* dataBuffer, class btSerializer* serializer) const;
  136. };
  137. // clang-format off
  138. struct btMultiBodyLinkColliderFloatData
  139. {
  140. btCollisionObjectFloatData m_colObjData;
  141. btMultiBodyFloatData *m_multiBody;
  142. int m_link;
  143. char m_padding[4];
  144. };
  145. struct btMultiBodyLinkColliderDoubleData
  146. {
  147. btCollisionObjectDoubleData m_colObjData;
  148. btMultiBodyDoubleData *m_multiBody;
  149. int m_link;
  150. char m_padding[4];
  151. };
  152. // clang-format on
  153. SIMD_FORCE_INLINE int btMultiBodyLinkCollider::calculateSerializeBufferSize() const
  154. {
  155. return sizeof(btMultiBodyLinkColliderData);
  156. }
  157. SIMD_FORCE_INLINE const char* btMultiBodyLinkCollider::serialize(void* dataBuffer, class btSerializer* serializer) const
  158. {
  159. btMultiBodyLinkColliderData* dataOut = (btMultiBodyLinkColliderData*)dataBuffer;
  160. btCollisionObject::serialize(&dataOut->m_colObjData, serializer);
  161. dataOut->m_link = this->m_link;
  162. dataOut->m_multiBody = (btMultiBodyData*)serializer->getUniquePointer(m_multiBody);
  163. // Fill padding with zeros to appease msan.
  164. memset(dataOut->m_padding, 0, sizeof(dataOut->m_padding));
  165. return btMultiBodyLinkColliderDataName;
  166. }
  167. #endif //BT_FEATHERSTONE_LINK_COLLIDER_H