btBoxShape.h 8.1 KB

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