您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

174 行
5.7 KiB

  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 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_CAPSULE_SHAPE_H
  14. #define BT_CAPSULE_SHAPE_H
  15. #include "btConvexInternalShape.h"
  16. #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" // for the types
  17. ///The btCapsuleShape represents a capsule around the Y axis, there is also the btCapsuleShapeX aligned around the X axis and btCapsuleShapeZ around the Z axis.
  18. ///The total height is height+2*radius, so the height is just the height between the center of each 'sphere' of the capsule caps.
  19. ///The btCapsuleShape is a convex hull of two spheres. The btMultiSphereShape is a more general collision shape that takes the convex hull of multiple sphere, so it can also represent a capsule when just using two spheres.
  20. class btCapsuleShape : public btConvexInternalShape
  21. {
  22. protected:
  23. int m_upAxis;
  24. protected:
  25. ///only used for btCapsuleShapeZ and btCapsuleShapeX subclasses.
  26. btCapsuleShape() : btConvexInternalShape() {m_shapeType = CAPSULE_SHAPE_PROXYTYPE;};
  27. public:
  28. btCapsuleShape(btScalar radius,btScalar height);
  29. ///CollisionShape Interface
  30. virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const;
  31. /// btConvexShape Interface
  32. virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec)const;
  33. virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const;
  34. virtual void setMargin(btScalar collisionMargin)
  35. {
  36. //correct the m_implicitShapeDimensions for the margin
  37. btVector3 oldMargin(getMargin(),getMargin(),getMargin());
  38. btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
  39. btConvexInternalShape::setMargin(collisionMargin);
  40. btVector3 newMargin(getMargin(),getMargin(),getMargin());
  41. m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin;
  42. }
  43. virtual void getAabb (const btTransform& t, btVector3& aabbMin, btVector3& aabbMax) const
  44. {
  45. btVector3 halfExtents(getRadius(),getRadius(),getRadius());
  46. halfExtents[m_upAxis] = getRadius() + getHalfHeight();
  47. halfExtents += btVector3(getMargin(),getMargin(),getMargin());
  48. btMatrix3x3 abs_b = t.getBasis().absolute();
  49. btVector3 center = t.getOrigin();
  50. btVector3 extent = btVector3(abs_b[0].dot(halfExtents),abs_b[1].dot(halfExtents),abs_b[2].dot(halfExtents));
  51. aabbMin = center - extent;
  52. aabbMax = center + extent;
  53. }
  54. virtual const char* getName()const
  55. {
  56. return "CapsuleShape";
  57. }
  58. int getUpAxis() const
  59. {
  60. return m_upAxis;
  61. }
  62. btScalar getRadius() const
  63. {
  64. int radiusAxis = (m_upAxis+2)%3;
  65. return m_implicitShapeDimensions[radiusAxis];
  66. }
  67. btScalar getHalfHeight() const
  68. {
  69. return m_implicitShapeDimensions[m_upAxis];
  70. }
  71. virtual void setLocalScaling(const btVector3& scaling)
  72. {
  73. btVector3 oldMargin(getMargin(),getMargin(),getMargin());
  74. btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
  75. btVector3 unScaledImplicitShapeDimensionsWithMargin = implicitShapeDimensionsWithMargin / m_localScaling;
  76. btConvexInternalShape::setLocalScaling(scaling);
  77. m_implicitShapeDimensions = (unScaledImplicitShapeDimensionsWithMargin * m_localScaling) - oldMargin;
  78. }
  79. virtual int calculateSerializeBufferSize() const;
  80. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  81. virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
  82. };
  83. ///btCapsuleShapeX represents a capsule around the Z axis
  84. ///the total height is height+2*radius, so the height is just the height between the center of each 'sphere' of the capsule caps.
  85. class btCapsuleShapeX : public btCapsuleShape
  86. {
  87. public:
  88. btCapsuleShapeX(btScalar radius,btScalar height);
  89. //debugging
  90. virtual const char* getName()const
  91. {
  92. return "CapsuleX";
  93. }
  94. };
  95. ///btCapsuleShapeZ represents a capsule around the Z axis
  96. ///the total height is height+2*radius, so the height is just the height between the center of each 'sphere' of the capsule caps.
  97. class btCapsuleShapeZ : public btCapsuleShape
  98. {
  99. public:
  100. btCapsuleShapeZ(btScalar radius,btScalar height);
  101. //debugging
  102. virtual const char* getName()const
  103. {
  104. return "CapsuleZ";
  105. }
  106. };
  107. ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
  108. struct btCapsuleShapeData
  109. {
  110. btConvexInternalShapeData m_convexInternalShapeData;
  111. int m_upAxis;
  112. char m_padding[4];
  113. };
  114. SIMD_FORCE_INLINE int btCapsuleShape::calculateSerializeBufferSize() const
  115. {
  116. return sizeof(btCapsuleShapeData);
  117. }
  118. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  119. SIMD_FORCE_INLINE const char* btCapsuleShape::serialize(void* dataBuffer, btSerializer* serializer) const
  120. {
  121. btCapsuleShapeData* shapeData = (btCapsuleShapeData*) dataBuffer;
  122. btConvexInternalShape::serialize(&shapeData->m_convexInternalShapeData,serializer);
  123. shapeData->m_upAxis = m_upAxis;
  124. return "btCapsuleShapeData";
  125. }
  126. #endif //BT_CAPSULE_SHAPE_H