btCylinderShape.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #include "btCylinderShape.h"
  14. btCylinderShape::btCylinderShape(const btVector3& halfExtents)
  15. : btConvexInternalShape(),
  16. m_upAxis(1)
  17. {
  18. btVector3 margin(getMargin(), getMargin(), getMargin());
  19. m_implicitShapeDimensions = (halfExtents * m_localScaling) - margin;
  20. setSafeMargin(halfExtents);
  21. m_shapeType = CYLINDER_SHAPE_PROXYTYPE;
  22. }
  23. btCylinderShapeX::btCylinderShapeX(const btVector3& halfExtents)
  24. : btCylinderShape(halfExtents)
  25. {
  26. m_upAxis = 0;
  27. }
  28. btCylinderShapeZ::btCylinderShapeZ(const btVector3& halfExtents)
  29. : btCylinderShape(halfExtents)
  30. {
  31. m_upAxis = 2;
  32. }
  33. void btCylinderShape::getAabb(const btTransform& t, btVector3& aabbMin, btVector3& aabbMax) const
  34. {
  35. btTransformAabb(getHalfExtentsWithoutMargin(), getMargin(), t, aabbMin, aabbMax);
  36. }
  37. void btCylinderShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
  38. {
  39. //Until Bullet 2.77 a box approximation was used, so uncomment this if you need backwards compatibility
  40. //#define USE_BOX_INERTIA_APPROXIMATION 1
  41. #ifndef USE_BOX_INERTIA_APPROXIMATION
  42. /*
  43. cylinder is defined as following:
  44. *
  45. * - principle axis aligned along y by default, radius in x, z-value not used
  46. * - for btCylinderShapeX: principle axis aligned along x, radius in y direction, z-value not used
  47. * - for btCylinderShapeZ: principle axis aligned along z, radius in x direction, y-value not used
  48. *
  49. */
  50. btScalar radius2; // square of cylinder radius
  51. btScalar height2; // square of cylinder height
  52. btVector3 halfExtents = getHalfExtentsWithMargin(); // get cylinder dimension
  53. btScalar div12 = mass / 12.f;
  54. btScalar div4 = mass / 4.f;
  55. btScalar div2 = mass / 2.f;
  56. int idxRadius, idxHeight;
  57. switch (m_upAxis) // get indices of radius and height of cylinder
  58. {
  59. case 0: // cylinder is aligned along x
  60. idxRadius = 1;
  61. idxHeight = 0;
  62. break;
  63. case 2: // cylinder is aligned along z
  64. idxRadius = 0;
  65. idxHeight = 2;
  66. break;
  67. default: // cylinder is aligned along y
  68. idxRadius = 0;
  69. idxHeight = 1;
  70. }
  71. // calculate squares
  72. radius2 = halfExtents[idxRadius] * halfExtents[idxRadius];
  73. height2 = btScalar(4.) * halfExtents[idxHeight] * halfExtents[idxHeight];
  74. // calculate tensor terms
  75. btScalar t1 = div12 * height2 + div4 * radius2;
  76. btScalar t2 = div2 * radius2;
  77. switch (m_upAxis) // set diagonal elements of inertia tensor
  78. {
  79. case 0: // cylinder is aligned along x
  80. inertia.setValue(t2, t1, t1);
  81. break;
  82. case 2: // cylinder is aligned along z
  83. inertia.setValue(t1, t1, t2);
  84. break;
  85. default: // cylinder is aligned along y
  86. inertia.setValue(t1, t2, t1);
  87. }
  88. #else //USE_BOX_INERTIA_APPROXIMATION
  89. //approximation of box shape
  90. btVector3 halfExtents = getHalfExtentsWithMargin();
  91. btScalar lx = btScalar(2.) * (halfExtents.x());
  92. btScalar ly = btScalar(2.) * (halfExtents.y());
  93. btScalar lz = btScalar(2.) * (halfExtents.z());
  94. inertia.setValue(mass / (btScalar(12.0)) * (ly * ly + lz * lz),
  95. mass / (btScalar(12.0)) * (lx * lx + lz * lz),
  96. mass / (btScalar(12.0)) * (lx * lx + ly * ly));
  97. #endif //USE_BOX_INERTIA_APPROXIMATION
  98. }
  99. SIMD_FORCE_INLINE btVector3 CylinderLocalSupportX(const btVector3& halfExtents, const btVector3& v)
  100. {
  101. const int cylinderUpAxis = 0;
  102. const int XX = 1;
  103. const int YY = 0;
  104. const int ZZ = 2;
  105. //mapping depends on how cylinder local orientation is
  106. // extents of the cylinder is: X,Y is for radius, and Z for height
  107. btScalar radius = halfExtents[XX];
  108. btScalar halfHeight = halfExtents[cylinderUpAxis];
  109. btVector3 tmp;
  110. btScalar d;
  111. btScalar s = btSqrt(v[XX] * v[XX] + v[ZZ] * v[ZZ]);
  112. if (s != btScalar(0.0))
  113. {
  114. d = radius / s;
  115. tmp[XX] = v[XX] * d;
  116. tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
  117. tmp[ZZ] = v[ZZ] * d;
  118. return tmp;
  119. }
  120. else
  121. {
  122. tmp[XX] = radius;
  123. tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
  124. tmp[ZZ] = btScalar(0.0);
  125. return tmp;
  126. }
  127. }
  128. inline btVector3 CylinderLocalSupportY(const btVector3& halfExtents, const btVector3& v)
  129. {
  130. const int cylinderUpAxis = 1;
  131. const int XX = 0;
  132. const int YY = 1;
  133. const int ZZ = 2;
  134. btScalar radius = halfExtents[XX];
  135. btScalar halfHeight = halfExtents[cylinderUpAxis];
  136. btVector3 tmp;
  137. btScalar d;
  138. btScalar s = btSqrt(v[XX] * v[XX] + v[ZZ] * v[ZZ]);
  139. if (s != btScalar(0.0))
  140. {
  141. d = radius / s;
  142. tmp[XX] = v[XX] * d;
  143. tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
  144. tmp[ZZ] = v[ZZ] * d;
  145. return tmp;
  146. }
  147. else
  148. {
  149. tmp[XX] = radius;
  150. tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
  151. tmp[ZZ] = btScalar(0.0);
  152. return tmp;
  153. }
  154. }
  155. inline btVector3 CylinderLocalSupportZ(const btVector3& halfExtents, const btVector3& v)
  156. {
  157. const int cylinderUpAxis = 2;
  158. const int XX = 0;
  159. const int YY = 2;
  160. const int ZZ = 1;
  161. //mapping depends on how cylinder local orientation is
  162. // extents of the cylinder is: X,Y is for radius, and Z for height
  163. btScalar radius = halfExtents[XX];
  164. btScalar halfHeight = halfExtents[cylinderUpAxis];
  165. btVector3 tmp;
  166. btScalar d;
  167. btScalar s = btSqrt(v[XX] * v[XX] + v[ZZ] * v[ZZ]);
  168. if (s != btScalar(0.0))
  169. {
  170. d = radius / s;
  171. tmp[XX] = v[XX] * d;
  172. tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
  173. tmp[ZZ] = v[ZZ] * d;
  174. return tmp;
  175. }
  176. else
  177. {
  178. tmp[XX] = radius;
  179. tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
  180. tmp[ZZ] = btScalar(0.0);
  181. return tmp;
  182. }
  183. }
  184. btVector3 btCylinderShapeX::localGetSupportingVertexWithoutMargin(const btVector3& vec) const
  185. {
  186. return CylinderLocalSupportX(getHalfExtentsWithoutMargin(), vec);
  187. }
  188. btVector3 btCylinderShapeZ::localGetSupportingVertexWithoutMargin(const btVector3& vec) const
  189. {
  190. return CylinderLocalSupportZ(getHalfExtentsWithoutMargin(), vec);
  191. }
  192. btVector3 btCylinderShape::localGetSupportingVertexWithoutMargin(const btVector3& vec) const
  193. {
  194. return CylinderLocalSupportY(getHalfExtentsWithoutMargin(), vec);
  195. }
  196. void btCylinderShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
  197. {
  198. for (int i = 0; i < numVectors; i++)
  199. {
  200. supportVerticesOut[i] = CylinderLocalSupportY(getHalfExtentsWithoutMargin(), vectors[i]);
  201. }
  202. }
  203. void btCylinderShapeZ::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
  204. {
  205. for (int i = 0; i < numVectors; i++)
  206. {
  207. supportVerticesOut[i] = CylinderLocalSupportZ(getHalfExtentsWithoutMargin(), vectors[i]);
  208. }
  209. }
  210. void btCylinderShapeX::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
  211. {
  212. for (int i = 0; i < numVectors; i++)
  213. {
  214. supportVerticesOut[i] = CylinderLocalSupportX(getHalfExtentsWithoutMargin(), vectors[i]);
  215. }
  216. }