btCylinderShape.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_CYLINDER_MINKOWSKI_H
  14. #define BT_CYLINDER_MINKOWSKI_H
  15. #include "btBoxShape.h"
  16. #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" // for the types
  17. #include "LinearMath/btVector3.h"
  18. /// The btCylinderShape class implements a cylinder shape primitive, centered around the origin. Its central axis aligned with the Y axis. btCylinderShapeX is aligned with the X axis and btCylinderShapeZ around the Z axis.
  19. ATTRIBUTE_ALIGNED16(class)
  20. btCylinderShape : public btConvexInternalShape
  21. {
  22. protected:
  23. int m_upAxis;
  24. public:
  25. BT_DECLARE_ALIGNED_ALLOCATOR();
  26. btVector3 getHalfExtentsWithMargin() const
  27. {
  28. btVector3 halfExtents = getHalfExtentsWithoutMargin();
  29. btVector3 margin(getMargin(), getMargin(), getMargin());
  30. halfExtents += margin;
  31. return halfExtents;
  32. }
  33. const btVector3& getHalfExtentsWithoutMargin() const
  34. {
  35. return m_implicitShapeDimensions; //changed in Bullet 2.63: assume the scaling and margin are included
  36. }
  37. btCylinderShape(const btVector3& halfExtents);
  38. void getAabb(const btTransform& t, btVector3& aabbMin, btVector3& aabbMax) const;
  39. virtual void calculateLocalInertia(btScalar mass, btVector3 & inertia) const;
  40. virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec) const;
  41. virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const;
  42. virtual void setMargin(btScalar collisionMargin)
  43. {
  44. //correct the m_implicitShapeDimensions for the margin
  45. btVector3 oldMargin(getMargin(), getMargin(), getMargin());
  46. btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions + oldMargin;
  47. btConvexInternalShape::setMargin(collisionMargin);
  48. btVector3 newMargin(getMargin(), getMargin(), getMargin());
  49. m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin;
  50. }
  51. virtual btVector3 localGetSupportingVertex(const btVector3& vec) const
  52. {
  53. btVector3 supVertex;
  54. supVertex = localGetSupportingVertexWithoutMargin(vec);
  55. if (getMargin() != btScalar(0.))
  56. {
  57. btVector3 vecnorm = vec;
  58. if (vecnorm.length2() < (SIMD_EPSILON * SIMD_EPSILON))
  59. {
  60. vecnorm.setValue(btScalar(-1.), btScalar(-1.), btScalar(-1.));
  61. }
  62. vecnorm.normalize();
  63. supVertex += getMargin() * vecnorm;
  64. }
  65. return supVertex;
  66. }
  67. //use box inertia
  68. // virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const;
  69. int getUpAxis() const
  70. {
  71. return m_upAxis;
  72. }
  73. virtual btVector3 getAnisotropicRollingFrictionDirection() const
  74. {
  75. btVector3 aniDir(0, 0, 0);
  76. aniDir[getUpAxis()] = 1;
  77. return aniDir;
  78. }
  79. virtual btScalar getRadius() const
  80. {
  81. return getHalfExtentsWithMargin().getX();
  82. }
  83. virtual void setLocalScaling(const btVector3& scaling)
  84. {
  85. btVector3 oldMargin(getMargin(), getMargin(), getMargin());
  86. btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions + oldMargin;
  87. btVector3 unScaledImplicitShapeDimensionsWithMargin = implicitShapeDimensionsWithMargin / m_localScaling;
  88. btConvexInternalShape::setLocalScaling(scaling);
  89. m_implicitShapeDimensions = (unScaledImplicitShapeDimensionsWithMargin * m_localScaling) - oldMargin;
  90. }
  91. //debugging
  92. virtual const char* getName() const
  93. {
  94. return "CylinderY";
  95. }
  96. virtual int calculateSerializeBufferSize() const;
  97. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  98. virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
  99. };
  100. class btCylinderShapeX : public btCylinderShape
  101. {
  102. public:
  103. BT_DECLARE_ALIGNED_ALLOCATOR();
  104. btCylinderShapeX(const btVector3& halfExtents);
  105. virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec) const;
  106. virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const;
  107. //debugging
  108. virtual const char* getName() const
  109. {
  110. return "CylinderX";
  111. }
  112. virtual btScalar getRadius() const
  113. {
  114. return getHalfExtentsWithMargin().getY();
  115. }
  116. };
  117. class btCylinderShapeZ : public btCylinderShape
  118. {
  119. public:
  120. BT_DECLARE_ALIGNED_ALLOCATOR();
  121. btCylinderShapeZ(const btVector3& halfExtents);
  122. virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec) const;
  123. virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const;
  124. //debugging
  125. virtual const char* getName() const
  126. {
  127. return "CylinderZ";
  128. }
  129. virtual btScalar getRadius() const
  130. {
  131. return getHalfExtentsWithMargin().getX();
  132. }
  133. };
  134. ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
  135. struct btCylinderShapeData
  136. {
  137. btConvexInternalShapeData m_convexInternalShapeData;
  138. int m_upAxis;
  139. char m_padding[4];
  140. };
  141. SIMD_FORCE_INLINE int btCylinderShape::calculateSerializeBufferSize() const
  142. {
  143. return sizeof(btCylinderShapeData);
  144. }
  145. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  146. SIMD_FORCE_INLINE const char* btCylinderShape::serialize(void* dataBuffer, btSerializer* serializer) const
  147. {
  148. btCylinderShapeData* shapeData = (btCylinderShapeData*)dataBuffer;
  149. btConvexInternalShape::serialize(&shapeData->m_convexInternalShapeData, serializer);
  150. shapeData->m_upAxis = m_upAxis;
  151. // Fill padding with zeros to appease msan.
  152. shapeData->m_padding[0] = 0;
  153. shapeData->m_padding[1] = 0;
  154. shapeData->m_padding[2] = 0;
  155. shapeData->m_padding[3] = 0;
  156. return "btCylinderShapeData";
  157. }
  158. #endif //BT_CYLINDER_MINKOWSKI_H