btRaycastCallback.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. //#include <stdio.h>
  14. #include "BulletCollision/CollisionShapes/btConvexShape.h"
  15. #include "BulletCollision/CollisionShapes/btTriangleShape.h"
  16. #include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
  17. #include "BulletCollision/NarrowPhaseCollision/btGjkConvexCast.h"
  18. #include "BulletCollision/NarrowPhaseCollision/btContinuousConvexCollision.h"
  19. #include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h"
  20. #include "btRaycastCallback.h"
  21. btTriangleRaycastCallback::btTriangleRaycastCallback(const btVector3& from, const btVector3& to, unsigned int flags)
  22. : m_from(from),
  23. m_to(to),
  24. //@BP Mod
  25. m_flags(flags),
  26. m_hitFraction(btScalar(1.))
  27. {
  28. }
  29. void btTriangleRaycastCallback::processTriangle(btVector3* triangle, int partId, int triangleIndex)
  30. {
  31. const btVector3& vert0 = triangle[0];
  32. const btVector3& vert1 = triangle[1];
  33. const btVector3& vert2 = triangle[2];
  34. btVector3 v10;
  35. v10 = vert1 - vert0;
  36. btVector3 v20;
  37. v20 = vert2 - vert0;
  38. btVector3 triangleNormal;
  39. triangleNormal = v10.cross(v20);
  40. const btScalar dist = vert0.dot(triangleNormal);
  41. btScalar dist_a = triangleNormal.dot(m_from);
  42. dist_a -= dist;
  43. btScalar dist_b = triangleNormal.dot(m_to);
  44. dist_b -= dist;
  45. if (dist_a * dist_b >= btScalar(0.0))
  46. {
  47. return; // same sign
  48. }
  49. if (((m_flags & kF_FilterBackfaces) != 0) && (dist_a <= btScalar(0.0)))
  50. {
  51. // Backface, skip check
  52. return;
  53. }
  54. const btScalar proj_length = dist_a - dist_b;
  55. const btScalar distance = (dist_a) / (proj_length);
  56. // Now we have the intersection point on the plane, we'll see if it's inside the triangle
  57. // Add an epsilon as a tolerance for the raycast,
  58. // in case the ray hits exacly on the edge of the triangle.
  59. // It must be scaled for the triangle size.
  60. if (distance < m_hitFraction)
  61. {
  62. btScalar edge_tolerance = triangleNormal.length2();
  63. edge_tolerance *= btScalar(-0.0001);
  64. btVector3 point;
  65. point.setInterpolate3(m_from, m_to, distance);
  66. {
  67. btVector3 v0p;
  68. v0p = vert0 - point;
  69. btVector3 v1p;
  70. v1p = vert1 - point;
  71. btVector3 cp0;
  72. cp0 = v0p.cross(v1p);
  73. if ((btScalar)(cp0.dot(triangleNormal)) >= edge_tolerance)
  74. {
  75. btVector3 v2p;
  76. v2p = vert2 - point;
  77. btVector3 cp1;
  78. cp1 = v1p.cross(v2p);
  79. if ((btScalar)(cp1.dot(triangleNormal)) >= edge_tolerance)
  80. {
  81. btVector3 cp2;
  82. cp2 = v2p.cross(v0p);
  83. if ((btScalar)(cp2.dot(triangleNormal)) >= edge_tolerance)
  84. {
  85. //@BP Mod
  86. // Triangle normal isn't normalized
  87. triangleNormal.normalize();
  88. //@BP Mod - Allow for unflipped normal when raycasting against backfaces
  89. if (((m_flags & kF_KeepUnflippedNormal) == 0) && (dist_a <= btScalar(0.0)))
  90. {
  91. m_hitFraction = reportHit(-triangleNormal, distance, partId, triangleIndex);
  92. }
  93. else
  94. {
  95. m_hitFraction = reportHit(triangleNormal, distance, partId, triangleIndex);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. btTriangleConvexcastCallback::btTriangleConvexcastCallback(const btConvexShape* convexShape, const btTransform& convexShapeFrom, const btTransform& convexShapeTo, const btTransform& triangleToWorld, const btScalar triangleCollisionMargin)
  104. {
  105. m_convexShape = convexShape;
  106. m_convexShapeFrom = convexShapeFrom;
  107. m_convexShapeTo = convexShapeTo;
  108. m_triangleToWorld = triangleToWorld;
  109. m_hitFraction = 1.0f;
  110. m_triangleCollisionMargin = triangleCollisionMargin;
  111. m_allowedPenetration = 0.f;
  112. }
  113. void btTriangleConvexcastCallback::processTriangle(btVector3* triangle, int partId, int triangleIndex)
  114. {
  115. btTriangleShape triangleShape(triangle[0], triangle[1], triangle[2]);
  116. triangleShape.setMargin(m_triangleCollisionMargin);
  117. btVoronoiSimplexSolver simplexSolver;
  118. btGjkEpaPenetrationDepthSolver gjkEpaPenetrationSolver;
  119. //#define USE_SUBSIMPLEX_CONVEX_CAST 1
  120. //if you reenable USE_SUBSIMPLEX_CONVEX_CAST see commented out code below
  121. #ifdef USE_SUBSIMPLEX_CONVEX_CAST
  122. btSubsimplexConvexCast convexCaster(m_convexShape, &triangleShape, &simplexSolver);
  123. #else
  124. //btGjkConvexCast convexCaster(m_convexShape,&triangleShape,&simplexSolver);
  125. btContinuousConvexCollision convexCaster(m_convexShape, &triangleShape, &simplexSolver, &gjkEpaPenetrationSolver);
  126. #endif //#USE_SUBSIMPLEX_CONVEX_CAST
  127. btConvexCast::CastResult castResult;
  128. castResult.m_fraction = btScalar(1.);
  129. castResult.m_allowedPenetration = m_allowedPenetration;
  130. if (convexCaster.calcTimeOfImpact(m_convexShapeFrom, m_convexShapeTo, m_triangleToWorld, m_triangleToWorld, castResult))
  131. {
  132. //add hit
  133. if (castResult.m_normal.length2() > btScalar(0.0001))
  134. {
  135. if (castResult.m_fraction < m_hitFraction)
  136. {
  137. /* btContinuousConvexCast's normal is already in world space */
  138. /*
  139. #ifdef USE_SUBSIMPLEX_CONVEX_CAST
  140. //rotate normal into worldspace
  141. castResult.m_normal = m_convexShapeFrom.getBasis() * castResult.m_normal;
  142. #endif //USE_SUBSIMPLEX_CONVEX_CAST
  143. */
  144. castResult.m_normal.normalize();
  145. reportHit(castResult.m_normal,
  146. castResult.m_hitPoint,
  147. castResult.m_fraction,
  148. partId,
  149. triangleIndex);
  150. }
  151. }
  152. }
  153. }