btBox2dShape.h 9.6 KB

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