btBox2dShape.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  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_2D_SHAPE_H
  14. #define BT_OBB_BOX_2D_SHAPE_H
  15. #include "bullet/BulletCollision//CollisionShapes/btPolyhedralConvexShape.h"
  16. #include "bullet/BulletCollision//CollisionShapes/btCollisionMargin.h"
  17. #include "bullet/BulletCollision//BroadphaseCollision/btBroadphaseProxy.h"
  18. #include "bullet/LinearMath/btVector3.h"
  19. #include "bullet/LinearMath/btMinMax.h"
  20. ///The btBox2dShape 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) btBox2dShape: public btPolyhedralConvexShape
  22. {
  23. //btVector3 m_boxHalfExtents1; //use m_implicitShapeDimensions instead
  24. btVector3 m_centroid;
  25. btVector3 m_vertices[4];
  26. btVector3 m_normals[4];
  27. public:
  28. BT_DECLARE_ALIGNED_ALLOCATOR();
  29. btVector3 getHalfExtentsWithMargin() const
  30. {
  31. btVector3 halfExtents = getHalfExtentsWithoutMargin();
  32. btVector3 margin(getMargin(),getMargin(),getMargin());
  33. halfExtents += margin;
  34. return halfExtents;
  35. }
  36. const btVector3& getHalfExtentsWithoutMargin() const
  37. {
  38. return m_implicitShapeDimensions;//changed in Bullet 2.63: assume the scaling and margin are included
  39. }
  40. virtual btVector3 localGetSupportingVertex(const btVector3& vec) const
  41. {
  42. btVector3 halfExtents = getHalfExtentsWithoutMargin();
  43. btVector3 margin(getMargin(),getMargin(),getMargin());
  44. halfExtents += margin;
  45. return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
  46. btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
  47. btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
  48. }
  49. SIMD_FORCE_INLINE btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec)const
  50. {
  51. const btVector3& halfExtents = getHalfExtentsWithoutMargin();
  52. return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
  53. btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
  54. btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
  55. }
  56. virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
  57. {
  58. const btVector3& halfExtents = getHalfExtentsWithoutMargin();
  59. for (int i=0;i<numVectors;i++)
  60. {
  61. const btVector3& vec = vectors[i];
  62. supportVerticesOut[i].setValue(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
  63. btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
  64. btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
  65. }
  66. }
  67. ///a btBox2dShape is a flat 2D box in the X-Y plane (Z extents are zero)
  68. btBox2dShape( const btVector3& boxHalfExtents)
  69. : btPolyhedralConvexShape(),
  70. m_centroid(0,0,0)
  71. {
  72. m_vertices[0].setValue(-boxHalfExtents.getX(),-boxHalfExtents.getY(),0);
  73. m_vertices[1].setValue(boxHalfExtents.getX(),-boxHalfExtents.getY(),0);
  74. m_vertices[2].setValue(boxHalfExtents.getX(),boxHalfExtents.getY(),0);
  75. m_vertices[3].setValue(-boxHalfExtents.getX(),boxHalfExtents.getY(),0);
  76. m_normals[0].setValue(0,-1,0);
  77. m_normals[1].setValue(1,0,0);
  78. m_normals[2].setValue(0,1,0);
  79. m_normals[3].setValue(-1,0,0);
  80. btScalar minDimension = boxHalfExtents.getX();
  81. if (minDimension>boxHalfExtents.getY())
  82. minDimension = boxHalfExtents.getY();
  83. setSafeMargin(minDimension);
  84. m_shapeType = BOX_2D_SHAPE_PROXYTYPE;
  85. btVector3 margin(getMargin(),getMargin(),getMargin());
  86. m_implicitShapeDimensions = (boxHalfExtents * m_localScaling) - margin;
  87. };
  88. virtual void setMargin(btScalar collisionMargin)
  89. {
  90. //correct the m_implicitShapeDimensions for the margin
  91. btVector3 oldMargin(getMargin(),getMargin(),getMargin());
  92. btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
  93. btConvexInternalShape::setMargin(collisionMargin);
  94. btVector3 newMargin(getMargin(),getMargin(),getMargin());
  95. m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin;
  96. }
  97. virtual void setLocalScaling(const btVector3& scaling)
  98. {
  99. btVector3 oldMargin(getMargin(),getMargin(),getMargin());
  100. btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
  101. btVector3 unScaledImplicitShapeDimensionsWithMargin = implicitShapeDimensionsWithMargin / m_localScaling;
  102. btConvexInternalShape::setLocalScaling(scaling);
  103. m_implicitShapeDimensions = (unScaledImplicitShapeDimensionsWithMargin * m_localScaling) - oldMargin;
  104. }
  105. virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const;
  106. virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const;
  107. int getVertexCount() const
  108. {
  109. return 4;
  110. }
  111. virtual int getNumVertices()const
  112. {
  113. return 4;
  114. }
  115. const btVector3* getVertices() const
  116. {
  117. return &m_vertices[0];
  118. }
  119. const btVector3* getNormals() const
  120. {
  121. return &m_normals[0];
  122. }
  123. virtual void getPlane(btVector3& planeNormal,btVector3& planeSupport,int i ) const
  124. {
  125. //this plane might not be aligned...
  126. btVector4 plane ;
  127. getPlaneEquation(plane,i);
  128. planeNormal = btVector3(plane.getX(),plane.getY(),plane.getZ());
  129. planeSupport = localGetSupportingVertex(-planeNormal);
  130. }
  131. const btVector3& getCentroid() const
  132. {
  133. return m_centroid;
  134. }
  135. virtual int getNumPlanes() const
  136. {
  137. return 6;
  138. }
  139. virtual int getNumEdges() const
  140. {
  141. return 12;
  142. }
  143. virtual void getVertex(int i,btVector3& vtx) const
  144. {
  145. btVector3 halfExtents = getHalfExtentsWithoutMargin();
  146. vtx = btVector3(
  147. halfExtents.x() * (1-(i&1)) - halfExtents.x() * (i&1),
  148. halfExtents.y() * (1-((i&2)>>1)) - halfExtents.y() * ((i&2)>>1),
  149. halfExtents.z() * (1-((i&4)>>2)) - halfExtents.z() * ((i&4)>>2));
  150. }
  151. virtual void getPlaneEquation(btVector4& plane,int i) const
  152. {
  153. btVector3 halfExtents = getHalfExtentsWithoutMargin();
  154. switch (i)
  155. {
  156. case 0:
  157. plane.setValue(btScalar(1.),btScalar(0.),btScalar(0.),-halfExtents.x());
  158. break;
  159. case 1:
  160. plane.setValue(btScalar(-1.),btScalar(0.),btScalar(0.),-halfExtents.x());
  161. break;
  162. case 2:
  163. plane.setValue(btScalar(0.),btScalar(1.),btScalar(0.),-halfExtents.y());
  164. break;
  165. case 3:
  166. plane.setValue(btScalar(0.),btScalar(-1.),btScalar(0.),-halfExtents.y());
  167. break;
  168. case 4:
  169. plane.setValue(btScalar(0.),btScalar(0.),btScalar(1.),-halfExtents.z());
  170. break;
  171. case 5:
  172. plane.setValue(btScalar(0.),btScalar(0.),btScalar(-1.),-halfExtents.z());
  173. break;
  174. default:
  175. btAssert(0);
  176. }
  177. }
  178. virtual void getEdge(int i,btVector3& pa,btVector3& pb) const
  179. //virtual void getEdge(int i,Edge& edge) const
  180. {
  181. int edgeVert0 = 0;
  182. int edgeVert1 = 0;
  183. switch (i)
  184. {
  185. case 0:
  186. edgeVert0 = 0;
  187. edgeVert1 = 1;
  188. break;
  189. case 1:
  190. edgeVert0 = 0;
  191. edgeVert1 = 2;
  192. break;
  193. case 2:
  194. edgeVert0 = 1;
  195. edgeVert1 = 3;
  196. break;
  197. case 3:
  198. edgeVert0 = 2;
  199. edgeVert1 = 3;
  200. break;
  201. case 4:
  202. edgeVert0 = 0;
  203. edgeVert1 = 4;
  204. break;
  205. case 5:
  206. edgeVert0 = 1;
  207. edgeVert1 = 5;
  208. break;
  209. case 6:
  210. edgeVert0 = 2;
  211. edgeVert1 = 6;
  212. break;
  213. case 7:
  214. edgeVert0 = 3;
  215. edgeVert1 = 7;
  216. break;
  217. case 8:
  218. edgeVert0 = 4;
  219. edgeVert1 = 5;
  220. break;
  221. case 9:
  222. edgeVert0 = 4;
  223. edgeVert1 = 6;
  224. break;
  225. case 10:
  226. edgeVert0 = 5;
  227. edgeVert1 = 7;
  228. break;
  229. case 11:
  230. edgeVert0 = 6;
  231. edgeVert1 = 7;
  232. break;
  233. default:
  234. btAssert(0);
  235. }
  236. getVertex(edgeVert0,pa );
  237. getVertex(edgeVert1,pb );
  238. }
  239. virtual bool isInside(const btVector3& pt,btScalar tolerance) const
  240. {
  241. btVector3 halfExtents = getHalfExtentsWithoutMargin();
  242. //btScalar minDist = 2*tolerance;
  243. bool result = (pt.x() <= (halfExtents.x()+tolerance)) &&
  244. (pt.x() >= (-halfExtents.x()-tolerance)) &&
  245. (pt.y() <= (halfExtents.y()+tolerance)) &&
  246. (pt.y() >= (-halfExtents.y()-tolerance)) &&
  247. (pt.z() <= (halfExtents.z()+tolerance)) &&
  248. (pt.z() >= (-halfExtents.z()-tolerance));
  249. return result;
  250. }
  251. //debugging
  252. virtual const char* getName()const
  253. {
  254. return "Box2d";
  255. }
  256. virtual int getNumPreferredPenetrationDirections() const
  257. {
  258. return 6;
  259. }
  260. virtual void getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const
  261. {
  262. switch (index)
  263. {
  264. case 0:
  265. penetrationVector.setValue(btScalar(1.),btScalar(0.),btScalar(0.));
  266. break;
  267. case 1:
  268. penetrationVector.setValue(btScalar(-1.),btScalar(0.),btScalar(0.));
  269. break;
  270. case 2:
  271. penetrationVector.setValue(btScalar(0.),btScalar(1.),btScalar(0.));
  272. break;
  273. case 3:
  274. penetrationVector.setValue(btScalar(0.),btScalar(-1.),btScalar(0.));
  275. break;
  276. case 4:
  277. penetrationVector.setValue(btScalar(0.),btScalar(0.),btScalar(1.));
  278. break;
  279. case 5:
  280. penetrationVector.setValue(btScalar(0.),btScalar(0.),btScalar(-1.));
  281. break;
  282. default:
  283. btAssert(0);
  284. }
  285. }
  286. };
  287. #endif //BT_OBB_BOX_2D_SHAPE_H