btRaycastCallback.cpp 5.9 KB

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