btTriangleShapeEx.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*! \file btGImpactShape.h
  2. \author Francisco Leon Najera
  3. */
  4. /*
  5. This source file is part of GIMPACT Library.
  6. For the latest info, see http://gimpact.sourceforge.net/
  7. Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
  8. email: projectileman@yahoo.com
  9. This software is provided 'as-is', without any express or implied warranty.
  10. In no event will the authors be held liable for any damages arising from the use of this software.
  11. Permission is granted to anyone to use this software for any purpose,
  12. including commercial applications, and to alter it and redistribute it freely,
  13. subject to the following restrictions:
  14. 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.
  15. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef GIMPACT_TRIANGLE_SHAPE_EX_H
  19. #define GIMPACT_TRIANGLE_SHAPE_EX_H
  20. #include "BulletCollision/CollisionShapes/btCollisionShape.h"
  21. #include "BulletCollision/CollisionShapes/btTriangleShape.h"
  22. #include "btBoxCollision.h"
  23. #include "btClipPolygon.h"
  24. #include "btGeometryOperations.h"
  25. #define MAX_TRI_CLIPPING 16
  26. //! Structure for collision
  27. struct GIM_TRIANGLE_CONTACT
  28. {
  29. btScalar m_penetration_depth;
  30. int m_point_count;
  31. btVector4 m_separating_normal;
  32. btVector3 m_points[MAX_TRI_CLIPPING];
  33. SIMD_FORCE_INLINE void copy_from(const GIM_TRIANGLE_CONTACT& other)
  34. {
  35. m_penetration_depth = other.m_penetration_depth;
  36. m_separating_normal = other.m_separating_normal;
  37. m_point_count = other.m_point_count;
  38. int i = m_point_count;
  39. while(i--)
  40. {
  41. m_points[i] = other.m_points[i];
  42. }
  43. }
  44. GIM_TRIANGLE_CONTACT()
  45. {
  46. }
  47. GIM_TRIANGLE_CONTACT(const GIM_TRIANGLE_CONTACT& other)
  48. {
  49. copy_from(other);
  50. }
  51. //! classify points that are closer
  52. void merge_points(const btVector4 & plane,
  53. btScalar margin, const btVector3 * points, int point_count);
  54. };
  55. class btPrimitiveTriangle
  56. {
  57. public:
  58. btVector3 m_vertices[3];
  59. btVector4 m_plane;
  60. btScalar m_margin;
  61. btScalar m_dummy;
  62. btPrimitiveTriangle():m_margin(0.01f)
  63. {
  64. }
  65. SIMD_FORCE_INLINE void buildTriPlane()
  66. {
  67. btVector3 normal = (m_vertices[1]-m_vertices[0]).cross(m_vertices[2]-m_vertices[0]);
  68. normal.normalize();
  69. m_plane.setValue(normal[0],normal[1],normal[2],m_vertices[0].dot(normal));
  70. }
  71. //! Test if triangles could collide
  72. bool overlap_test_conservative(const btPrimitiveTriangle& other);
  73. //! Calcs the plane which is paralele to the edge and perpendicular to the triangle plane
  74. /*!
  75. \pre this triangle must have its plane calculated.
  76. */
  77. SIMD_FORCE_INLINE void get_edge_plane(int edge_index, btVector4 &plane) const
  78. {
  79. const btVector3 & e0 = m_vertices[edge_index];
  80. const btVector3 & e1 = m_vertices[(edge_index+1)%3];
  81. bt_edge_plane(e0,e1,m_plane,plane);
  82. }
  83. void applyTransform(const btTransform& t)
  84. {
  85. m_vertices[0] = t(m_vertices[0]);
  86. m_vertices[1] = t(m_vertices[1]);
  87. m_vertices[2] = t(m_vertices[2]);
  88. }
  89. //! Clips the triangle against this
  90. /*!
  91. \pre clipped_points must have MAX_TRI_CLIPPING size, and this triangle must have its plane calculated.
  92. \return the number of clipped points
  93. */
  94. int clip_triangle(btPrimitiveTriangle & other, btVector3 * clipped_points );
  95. //! Find collision using the clipping method
  96. /*!
  97. \pre this triangle and other must have their triangles calculated
  98. */
  99. bool find_triangle_collision_clip_method(btPrimitiveTriangle & other, GIM_TRIANGLE_CONTACT & contacts);
  100. };
  101. //! Helper class for colliding Bullet Triangle Shapes
  102. /*!
  103. This class implements a better getAabb method than the previous btTriangleShape class
  104. */
  105. class btTriangleShapeEx: public btTriangleShape
  106. {
  107. public:
  108. btTriangleShapeEx():btTriangleShape(btVector3(0,0,0),btVector3(0,0,0),btVector3(0,0,0))
  109. {
  110. }
  111. btTriangleShapeEx(const btVector3& p0,const btVector3& p1,const btVector3& p2): btTriangleShape(p0,p1,p2)
  112. {
  113. }
  114. btTriangleShapeEx(const btTriangleShapeEx & other): btTriangleShape(other.m_vertices1[0],other.m_vertices1[1],other.m_vertices1[2])
  115. {
  116. }
  117. virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax)const
  118. {
  119. btVector3 tv0 = t(m_vertices1[0]);
  120. btVector3 tv1 = t(m_vertices1[1]);
  121. btVector3 tv2 = t(m_vertices1[2]);
  122. btAABB trianglebox(tv0,tv1,tv2,m_collisionMargin);
  123. aabbMin = trianglebox.m_min;
  124. aabbMax = trianglebox.m_max;
  125. }
  126. void applyTransform(const btTransform& t)
  127. {
  128. m_vertices1[0] = t(m_vertices1[0]);
  129. m_vertices1[1] = t(m_vertices1[1]);
  130. m_vertices1[2] = t(m_vertices1[2]);
  131. }
  132. SIMD_FORCE_INLINE void buildTriPlane(btVector4 & plane) const
  133. {
  134. btVector3 normal = (m_vertices1[1]-m_vertices1[0]).cross(m_vertices1[2]-m_vertices1[0]);
  135. normal.normalize();
  136. plane.setValue(normal[0],normal[1],normal[2],m_vertices1[0].dot(normal));
  137. }
  138. bool overlap_test_conservative(const btTriangleShapeEx& other);
  139. };
  140. #endif //GIMPACT_TRIANGLE_MESH_SHAPE_H