btCapsuleShape.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "bullet/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. ATTRIBUTE_ALIGNED16(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. BT_DECLARE_ALIGNED_ALLOCATOR();
  29. btCapsuleShape(btScalar radius,btScalar height);
  30. ///CollisionShape Interface
  31. virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const;
  32. /// btConvexShape Interface
  33. virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec)const;
  34. virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const;
  35. virtual void setMargin(btScalar collisionMargin)
  36. {
  37. //correct the m_implicitShapeDimensions for the margin
  38. btVector3 oldMargin(getMargin(),getMargin(),getMargin());
  39. btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
  40. btConvexInternalShape::setMargin(collisionMargin);
  41. btVector3 newMargin(getMargin(),getMargin(),getMargin());
  42. m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin;
  43. }
  44. virtual void getAabb (const btTransform& t, btVector3& aabbMin, btVector3& aabbMax) const
  45. {
  46. btVector3 halfExtents(getRadius(),getRadius(),getRadius());
  47. halfExtents[m_upAxis] = getRadius() + getHalfHeight();
  48. halfExtents += btVector3(getMargin(),getMargin(),getMargin());
  49. btMatrix3x3 abs_b = t.getBasis().absolute();
  50. btVector3 center = t.getOrigin();
  51. btVector3 extent = halfExtents.dot3(abs_b[0], abs_b[1], abs_b[2]);
  52. aabbMin = center - extent;
  53. aabbMax = center + extent;
  54. }
  55. virtual const char* getName()const
  56. {
  57. return "CapsuleShape";
  58. }
  59. int getUpAxis() const
  60. {
  61. return m_upAxis;
  62. }
  63. btScalar getRadius() const
  64. {
  65. int radiusAxis = (m_upAxis+2)%3;
  66. return m_implicitShapeDimensions[radiusAxis];
  67. }
  68. btScalar getHalfHeight() const
  69. {
  70. return m_implicitShapeDimensions[m_upAxis];
  71. }
  72. virtual void setLocalScaling(const btVector3& scaling)
  73. {
  74. btVector3 oldMargin(getMargin(),getMargin(),getMargin());
  75. btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
  76. btVector3 unScaledImplicitShapeDimensionsWithMargin = implicitShapeDimensionsWithMargin / m_localScaling;
  77. btConvexInternalShape::setLocalScaling(scaling);
  78. m_implicitShapeDimensions = (unScaledImplicitShapeDimensionsWithMargin * m_localScaling) - oldMargin;
  79. }
  80. virtual btVector3 getAnisotropicRollingFrictionDirection() const
  81. {
  82. btVector3 aniDir(0,0,0);
  83. aniDir[getUpAxis()]=1;
  84. return aniDir;
  85. }
  86. virtual int calculateSerializeBufferSize() const;
  87. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  88. virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
  89. };
  90. ///btCapsuleShapeX represents a capsule around the Z axis
  91. ///the total height is height+2*radius, so the height is just the height between the center of each 'sphere' of the capsule caps.
  92. class btCapsuleShapeX : public btCapsuleShape
  93. {
  94. public:
  95. btCapsuleShapeX(btScalar radius,btScalar height);
  96. //debugging
  97. virtual const char* getName()const
  98. {
  99. return "CapsuleX";
  100. }
  101. };
  102. ///btCapsuleShapeZ represents a capsule around the Z axis
  103. ///the total height is height+2*radius, so the height is just the height between the center of each 'sphere' of the capsule caps.
  104. class btCapsuleShapeZ : public btCapsuleShape
  105. {
  106. public:
  107. btCapsuleShapeZ(btScalar radius,btScalar height);
  108. //debugging
  109. virtual const char* getName()const
  110. {
  111. return "CapsuleZ";
  112. }
  113. };
  114. ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
  115. struct btCapsuleShapeData
  116. {
  117. btConvexInternalShapeData m_convexInternalShapeData;
  118. int m_upAxis;
  119. char m_padding[4];
  120. };
  121. SIMD_FORCE_INLINE int btCapsuleShape::calculateSerializeBufferSize() const
  122. {
  123. return sizeof(btCapsuleShapeData);
  124. }
  125. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  126. SIMD_FORCE_INLINE const char* btCapsuleShape::serialize(void* dataBuffer, btSerializer* serializer) const
  127. {
  128. btCapsuleShapeData* shapeData = (btCapsuleShapeData*) dataBuffer;
  129. btConvexInternalShape::serialize(&shapeData->m_convexInternalShapeData,serializer);
  130. shapeData->m_upAxis = m_upAxis;
  131. return "btCapsuleShapeData";
  132. }
  133. #endif //BT_CAPSULE_SHAPE_H