btConeShape.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_CONE_MINKOWSKI_H
  14. #define BT_CONE_MINKOWSKI_H
  15. #include "btConvexInternalShape.h"
  16. #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" // for the types
  17. ///The btConeShape implements a cone shape primitive, centered around the origin and aligned with the Y axis. The btConeShapeX is aligned around the X axis and btConeShapeZ around the Z axis.
  18. ATTRIBUTE_ALIGNED16(class)
  19. btConeShape : public btConvexInternalShape
  20. {
  21. btScalar m_sinAngle;
  22. btScalar m_radius;
  23. btScalar m_height;
  24. int m_coneIndices[3];
  25. btVector3 coneLocalSupport(const btVector3& v) const;
  26. public:
  27. BT_DECLARE_ALIGNED_ALLOCATOR();
  28. btConeShape(btScalar radius, btScalar height);
  29. virtual btVector3 localGetSupportingVertex(const btVector3& vec) const;
  30. virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec) const;
  31. virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const;
  32. btScalar getRadius() const { return m_radius; }
  33. btScalar getHeight() const { return m_height; }
  34. void setRadius(const btScalar radius)
  35. {
  36. m_radius = radius;
  37. }
  38. void setHeight(const btScalar height)
  39. {
  40. m_height = height;
  41. }
  42. virtual void calculateLocalInertia(btScalar mass, btVector3 & inertia) const
  43. {
  44. btTransform identity;
  45. identity.setIdentity();
  46. btVector3 aabbMin, aabbMax;
  47. getAabb(identity, aabbMin, aabbMax);
  48. btVector3 halfExtents = (aabbMax - aabbMin) * btScalar(0.5);
  49. btScalar margin = getMargin();
  50. btScalar lx = btScalar(2.) * (halfExtents.x() + margin);
  51. btScalar ly = btScalar(2.) * (halfExtents.y() + margin);
  52. btScalar lz = btScalar(2.) * (halfExtents.z() + margin);
  53. const btScalar x2 = lx * lx;
  54. const btScalar y2 = ly * ly;
  55. const btScalar z2 = lz * lz;
  56. const btScalar scaledmass = mass * btScalar(0.08333333);
  57. inertia = scaledmass * (btVector3(y2 + z2, x2 + z2, x2 + y2));
  58. // inertia.x() = scaledmass * (y2+z2);
  59. // inertia.y() = scaledmass * (x2+z2);
  60. // inertia.z() = scaledmass * (x2+y2);
  61. }
  62. virtual const char* getName() const
  63. {
  64. return "Cone";
  65. }
  66. ///choose upAxis index
  67. void setConeUpIndex(int upIndex);
  68. int getConeUpIndex() const
  69. {
  70. return m_coneIndices[1];
  71. }
  72. virtual btVector3 getAnisotropicRollingFrictionDirection() const
  73. {
  74. return btVector3(0, 1, 0);
  75. }
  76. virtual void setLocalScaling(const btVector3& scaling);
  77. virtual int calculateSerializeBufferSize() const;
  78. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  79. virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
  80. };
  81. ///btConeShape implements a Cone shape, around the X axis
  82. class btConeShapeX : public btConeShape
  83. {
  84. public:
  85. btConeShapeX(btScalar radius, btScalar height);
  86. virtual btVector3 getAnisotropicRollingFrictionDirection() const
  87. {
  88. return btVector3(1, 0, 0);
  89. }
  90. //debugging
  91. virtual const char* getName() const
  92. {
  93. return "ConeX";
  94. }
  95. };
  96. ///btConeShapeZ implements a Cone shape, around the Z axis
  97. class btConeShapeZ : public btConeShape
  98. {
  99. public:
  100. btConeShapeZ(btScalar radius, btScalar height);
  101. virtual btVector3 getAnisotropicRollingFrictionDirection() const
  102. {
  103. return btVector3(0, 0, 1);
  104. }
  105. //debugging
  106. virtual const char* getName() const
  107. {
  108. return "ConeZ";
  109. }
  110. };
  111. ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
  112. struct btConeShapeData
  113. {
  114. btConvexInternalShapeData m_convexInternalShapeData;
  115. int m_upIndex;
  116. char m_padding[4];
  117. };
  118. SIMD_FORCE_INLINE int btConeShape::calculateSerializeBufferSize() const
  119. {
  120. return sizeof(btConeShapeData);
  121. }
  122. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  123. SIMD_FORCE_INLINE const char* btConeShape::serialize(void* dataBuffer, btSerializer* serializer) const
  124. {
  125. btConeShapeData* shapeData = (btConeShapeData*)dataBuffer;
  126. btConvexInternalShape::serialize(&shapeData->m_convexInternalShapeData, serializer);
  127. shapeData->m_upIndex = m_coneIndices[1];
  128. // Fill padding with zeros to appease msan.
  129. shapeData->m_padding[0] = 0;
  130. shapeData->m_padding[1] = 0;
  131. shapeData->m_padding[2] = 0;
  132. shapeData->m_padding[3] = 0;
  133. return "btConeShapeData";
  134. }
  135. #endif //BT_CONE_MINKOWSKI_H