btTriangleShape.h 4.5 KB

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