btCylinderShape.h 6.1 KB

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