btTriangleShape.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_TRIANGLE_MINKOWSKI_H
  14. #define BT_OBB_TRIANGLE_MINKOWSKI_H
  15. #include "btConvexShape.h"
  16. #include "btBoxShape.h"
  17. ATTRIBUTE_ALIGNED16(class)
  18. btTriangleShape : public btPolyhedralConvexShape
  19. {
  20. public:
  21. BT_DECLARE_ALIGNED_ALLOCATOR();
  22. btVector3 m_vertices1[3];
  23. virtual int getNumVertices() const
  24. {
  25. return 3;
  26. }
  27. btVector3& getVertexPtr(int index)
  28. {
  29. return m_vertices1[index];
  30. }
  31. const btVector3& getVertexPtr(int index) const
  32. {
  33. return m_vertices1[index];
  34. }
  35. virtual void getVertex(int index, btVector3& vert) const
  36. {
  37. vert = m_vertices1[index];
  38. }
  39. virtual int getNumEdges() const
  40. {
  41. return 3;
  42. }
  43. virtual void getEdge(int i, btVector3& pa, btVector3& pb) const
  44. {
  45. getVertex(i, pa);
  46. getVertex((i + 1) % 3, pb);
  47. }
  48. virtual void getAabb(const btTransform& t, btVector3& aabbMin, btVector3& aabbMax) const
  49. {
  50. // btAssert(0);
  51. getAabbSlow(t, aabbMin, aabbMax);
  52. }
  53. btVector3 localGetSupportingVertexWithoutMargin(const btVector3& dir) const
  54. {
  55. btVector3 dots = dir.dot3(m_vertices1[0], m_vertices1[1], m_vertices1[2]);
  56. return m_vertices1[dots.maxAxis()];
  57. }
  58. virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
  59. {
  60. for (int i = 0; i < numVectors; i++)
  61. {
  62. const btVector3& dir = vectors[i];
  63. btVector3 dots = dir.dot3(m_vertices1[0], m_vertices1[1], m_vertices1[2]);
  64. supportVerticesOut[i] = m_vertices1[dots.maxAxis()];
  65. }
  66. }
  67. btTriangleShape() : btPolyhedralConvexShape()
  68. {
  69. m_shapeType = TRIANGLE_SHAPE_PROXYTYPE;
  70. }
  71. btTriangleShape(const btVector3& p0, const btVector3& p1, const btVector3& p2) : btPolyhedralConvexShape()
  72. {
  73. m_shapeType = TRIANGLE_SHAPE_PROXYTYPE;
  74. m_vertices1[0] = p0;
  75. m_vertices1[1] = p1;
  76. m_vertices1[2] = p2;
  77. }
  78. virtual void getPlane(btVector3 & planeNormal, btVector3 & planeSupport, int i) const
  79. {
  80. getPlaneEquation(i, planeNormal, planeSupport);
  81. }
  82. virtual int getNumPlanes() const
  83. {
  84. return 1;
  85. }
  86. void calcNormal(btVector3 & normal) const
  87. {
  88. normal = (m_vertices1[1] - m_vertices1[0]).cross(m_vertices1[2] - m_vertices1[0]);
  89. normal.normalize();
  90. }
  91. virtual void getPlaneEquation(int i, btVector3& planeNormal, btVector3& planeSupport) const
  92. {
  93. (void)i;
  94. calcNormal(planeNormal);
  95. planeSupport = m_vertices1[0];
  96. }
  97. virtual void calculateLocalInertia(btScalar mass, btVector3 & inertia) const
  98. {
  99. (void)mass;
  100. btAssert(0);
  101. inertia.setValue(btScalar(0.), btScalar(0.), btScalar(0.));
  102. }
  103. virtual bool isInside(const btVector3& pt, btScalar tolerance) const
  104. {
  105. btVector3 normal;
  106. calcNormal(normal);
  107. //distance to plane
  108. btScalar dist = pt.dot(normal);
  109. btScalar planeconst = m_vertices1[0].dot(normal);
  110. dist -= planeconst;
  111. if (dist >= -tolerance && dist <= tolerance)
  112. {
  113. //inside check on edge-planes
  114. int i;
  115. for (i = 0; i < 3; i++)
  116. {
  117. btVector3 pa, pb;
  118. getEdge(i, pa, pb);
  119. btVector3 edge = pb - pa;
  120. btVector3 edgeNormal = edge.cross(normal);
  121. edgeNormal.normalize();
  122. btScalar dist = pt.dot(edgeNormal);
  123. btScalar edgeConst = pa.dot(edgeNormal);
  124. dist -= edgeConst;
  125. if (dist < -tolerance)
  126. return false;
  127. }
  128. return true;
  129. }
  130. return false;
  131. }
  132. //debugging
  133. virtual const char* getName() const
  134. {
  135. return "Triangle";
  136. }
  137. virtual int getNumPreferredPenetrationDirections() const
  138. {
  139. return 2;
  140. }
  141. virtual void getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const
  142. {
  143. calcNormal(penetrationVector);
  144. if (index)
  145. penetrationVector *= btScalar(-1.);
  146. }
  147. };
  148. #endif //BT_OBB_TRIANGLE_MINKOWSKI_H