btBoxShape.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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_OBB_BOX_MINKOWSKI_H
  14. #define BT_OBB_BOX_MINKOWSKI_H
  15. #include "btPolyhedralConvexShape.h"
  16. #include "btCollisionMargin.h"
  17. #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
  18. #include "LinearMath/btVector3.h"
  19. #include "LinearMath/btMinMax.h"
  20. ///The btBoxShape is a box primitive around the origin, its sides axis aligned with length specified by half extents, in local shape coordinates. When used as part of a btCollisionObject or btRigidBody it will be an oriented box in world space.
  21. ATTRIBUTE_ALIGNED16(class)
  22. btBoxShape : public btPolyhedralConvexShape
  23. {
  24. //btVector3 m_boxHalfExtents1; //use m_implicitShapeDimensions instead
  25. public:
  26. BT_DECLARE_ALIGNED_ALLOCATOR();
  27. btVector3 getHalfExtentsWithMargin() const
  28. {
  29. btVector3 halfExtents = getHalfExtentsWithoutMargin();
  30. btVector3 margin(getMargin(), getMargin(), getMargin());
  31. halfExtents += margin;
  32. return halfExtents;
  33. }
  34. const btVector3& getHalfExtentsWithoutMargin() const
  35. {
  36. return m_implicitShapeDimensions; //scaling is included, margin is not
  37. }
  38. virtual btVector3 localGetSupportingVertex(const btVector3& vec) const
  39. {
  40. btVector3 halfExtents = getHalfExtentsWithoutMargin();
  41. btVector3 margin(getMargin(), getMargin(), getMargin());
  42. halfExtents += margin;
  43. return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
  44. btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
  45. btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
  46. }
  47. SIMD_FORCE_INLINE btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec) const
  48. {
  49. const btVector3& halfExtents = getHalfExtentsWithoutMargin();
  50. return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
  51. btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
  52. btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
  53. }
  54. virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
  55. {
  56. const btVector3& halfExtents = getHalfExtentsWithoutMargin();
  57. for (int i = 0; i < numVectors; i++)
  58. {
  59. const btVector3& vec = vectors[i];
  60. supportVerticesOut[i].setValue(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
  61. btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
  62. btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
  63. }
  64. }
  65. btBoxShape(const btVector3& boxHalfExtents);
  66. virtual void setMargin(btScalar collisionMargin)
  67. {
  68. //correct the m_implicitShapeDimensions for the margin
  69. btVector3 oldMargin(getMargin(), getMargin(), getMargin());
  70. btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions + oldMargin;
  71. btConvexInternalShape::setMargin(collisionMargin);
  72. btVector3 newMargin(getMargin(), getMargin(), getMargin());
  73. m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin;
  74. }
  75. virtual void setLocalScaling(const btVector3& scaling)
  76. {
  77. btVector3 oldMargin(getMargin(), getMargin(), getMargin());
  78. btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions + oldMargin;
  79. btVector3 unScaledImplicitShapeDimensionsWithMargin = implicitShapeDimensionsWithMargin / m_localScaling;
  80. btConvexInternalShape::setLocalScaling(scaling);
  81. m_implicitShapeDimensions = (unScaledImplicitShapeDimensionsWithMargin * m_localScaling) - oldMargin;
  82. }
  83. virtual void getAabb(const btTransform& t, btVector3& aabbMin, btVector3& aabbMax) const;
  84. virtual void calculateLocalInertia(btScalar mass, btVector3 & inertia) const;
  85. virtual void getPlane(btVector3 & planeNormal, btVector3 & planeSupport, int i) const
  86. {
  87. //this plane might not be aligned...
  88. btVector4 plane;
  89. getPlaneEquation(plane, i);
  90. planeNormal = btVector3(plane.getX(), plane.getY(), plane.getZ());
  91. planeSupport = localGetSupportingVertex(-planeNormal);
  92. }
  93. virtual int getNumPlanes() const
  94. {
  95. return 6;
  96. }
  97. virtual int getNumVertices() const
  98. {
  99. return 8;
  100. }
  101. virtual int getNumEdges() const
  102. {
  103. return 12;
  104. }
  105. virtual void getVertex(int i, btVector3& vtx) const
  106. {
  107. btVector3 halfExtents = getHalfExtentsWithMargin();
  108. vtx = btVector3(
  109. halfExtents.x() * (1 - (i & 1)) - halfExtents.x() * (i & 1),
  110. halfExtents.y() * (1 - ((i & 2) >> 1)) - halfExtents.y() * ((i & 2) >> 1),
  111. halfExtents.z() * (1 - ((i & 4) >> 2)) - halfExtents.z() * ((i & 4) >> 2));
  112. }
  113. virtual void getPlaneEquation(btVector4 & plane, int i) const
  114. {
  115. btVector3 halfExtents = getHalfExtentsWithoutMargin();
  116. switch (i)
  117. {
  118. case 0:
  119. plane.setValue(btScalar(1.), btScalar(0.), btScalar(0.), -halfExtents.x());
  120. break;
  121. case 1:
  122. plane.setValue(btScalar(-1.), btScalar(0.), btScalar(0.), -halfExtents.x());
  123. break;
  124. case 2:
  125. plane.setValue(btScalar(0.), btScalar(1.), btScalar(0.), -halfExtents.y());
  126. break;
  127. case 3:
  128. plane.setValue(btScalar(0.), btScalar(-1.), btScalar(0.), -halfExtents.y());
  129. break;
  130. case 4:
  131. plane.setValue(btScalar(0.), btScalar(0.), btScalar(1.), -halfExtents.z());
  132. break;
  133. case 5:
  134. plane.setValue(btScalar(0.), btScalar(0.), btScalar(-1.), -halfExtents.z());
  135. break;
  136. default:
  137. btAssert(0);
  138. }
  139. }
  140. virtual void getEdge(int i, btVector3& pa, btVector3& pb) const
  141. //virtual void getEdge(int i,Edge& edge) const
  142. {
  143. int edgeVert0 = 0;
  144. int edgeVert1 = 0;
  145. switch (i)
  146. {
  147. case 0:
  148. edgeVert0 = 0;
  149. edgeVert1 = 1;
  150. break;
  151. case 1:
  152. edgeVert0 = 0;
  153. edgeVert1 = 2;
  154. break;
  155. case 2:
  156. edgeVert0 = 1;
  157. edgeVert1 = 3;
  158. break;
  159. case 3:
  160. edgeVert0 = 2;
  161. edgeVert1 = 3;
  162. break;
  163. case 4:
  164. edgeVert0 = 0;
  165. edgeVert1 = 4;
  166. break;
  167. case 5:
  168. edgeVert0 = 1;
  169. edgeVert1 = 5;
  170. break;
  171. case 6:
  172. edgeVert0 = 2;
  173. edgeVert1 = 6;
  174. break;
  175. case 7:
  176. edgeVert0 = 3;
  177. edgeVert1 = 7;
  178. break;
  179. case 8:
  180. edgeVert0 = 4;
  181. edgeVert1 = 5;
  182. break;
  183. case 9:
  184. edgeVert0 = 4;
  185. edgeVert1 = 6;
  186. break;
  187. case 10:
  188. edgeVert0 = 5;
  189. edgeVert1 = 7;
  190. break;
  191. case 11:
  192. edgeVert0 = 6;
  193. edgeVert1 = 7;
  194. break;
  195. default:
  196. btAssert(0);
  197. }
  198. getVertex(edgeVert0, pa);
  199. getVertex(edgeVert1, pb);
  200. }
  201. virtual bool isInside(const btVector3& pt, btScalar tolerance) const
  202. {
  203. btVector3 halfExtents = getHalfExtentsWithoutMargin();
  204. //btScalar minDist = 2*tolerance;
  205. bool result = (pt.x() <= (halfExtents.x() + tolerance)) &&
  206. (pt.x() >= (-halfExtents.x() - tolerance)) &&
  207. (pt.y() <= (halfExtents.y() + tolerance)) &&
  208. (pt.y() >= (-halfExtents.y() - tolerance)) &&
  209. (pt.z() <= (halfExtents.z() + tolerance)) &&
  210. (pt.z() >= (-halfExtents.z() - tolerance));
  211. return result;
  212. }
  213. //debugging
  214. virtual const char* getName() const
  215. {
  216. return "Box";
  217. }
  218. virtual int getNumPreferredPenetrationDirections() const
  219. {
  220. return 6;
  221. }
  222. virtual void getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const
  223. {
  224. switch (index)
  225. {
  226. case 0:
  227. penetrationVector.setValue(btScalar(1.), btScalar(0.), btScalar(0.));
  228. break;
  229. case 1:
  230. penetrationVector.setValue(btScalar(-1.), btScalar(0.), btScalar(0.));
  231. break;
  232. case 2:
  233. penetrationVector.setValue(btScalar(0.), btScalar(1.), btScalar(0.));
  234. break;
  235. case 3:
  236. penetrationVector.setValue(btScalar(0.), btScalar(-1.), btScalar(0.));
  237. break;
  238. case 4:
  239. penetrationVector.setValue(btScalar(0.), btScalar(0.), btScalar(1.));
  240. break;
  241. case 5:
  242. penetrationVector.setValue(btScalar(0.), btScalar(0.), btScalar(-1.));
  243. break;
  244. default:
  245. btAssert(0);
  246. }
  247. }
  248. };
  249. #endif //BT_OBB_BOX_MINKOWSKI_H