1
0

btSubSimplexConvexCast.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "btSubSimplexConvexCast.h"
  14. #include "bullet/BulletCollision//CollisionShapes/btConvexShape.h"
  15. #include "bullet/BulletCollision//CollisionShapes/btMinkowskiSumShape.h"
  16. #include "bullet/BulletCollision//NarrowPhaseCollision/btSimplexSolverInterface.h"
  17. #include "btPointCollector.h"
  18. #include "bullet/LinearMath/btTransformUtil.h"
  19. btSubsimplexConvexCast::btSubsimplexConvexCast (const btConvexShape* convexA,const btConvexShape* convexB,btSimplexSolverInterface* simplexSolver)
  20. :m_simplexSolver(simplexSolver),
  21. m_convexA(convexA),m_convexB(convexB)
  22. {
  23. }
  24. ///Typically the conservative advancement reaches solution in a few iterations, clip it to 32 for degenerate cases.
  25. ///See discussion about this here http://continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=565
  26. #ifdef BT_USE_DOUBLE_PRECISION
  27. #define MAX_ITERATIONS 64
  28. #else
  29. #define MAX_ITERATIONS 32
  30. #endif
  31. bool btSubsimplexConvexCast::calcTimeOfImpact(
  32. const btTransform& fromA,
  33. const btTransform& toA,
  34. const btTransform& fromB,
  35. const btTransform& toB,
  36. CastResult& result)
  37. {
  38. m_simplexSolver->reset();
  39. btVector3 linVelA,linVelB;
  40. linVelA = toA.getOrigin()-fromA.getOrigin();
  41. linVelB = toB.getOrigin()-fromB.getOrigin();
  42. btScalar lambda = btScalar(0.);
  43. btTransform interpolatedTransA = fromA;
  44. btTransform interpolatedTransB = fromB;
  45. ///take relative motion
  46. btVector3 r = (linVelA-linVelB);
  47. btVector3 v;
  48. btVector3 supVertexA = fromA(m_convexA->localGetSupportingVertex(-r*fromA.getBasis()));
  49. btVector3 supVertexB = fromB(m_convexB->localGetSupportingVertex(r*fromB.getBasis()));
  50. v = supVertexA-supVertexB;
  51. int maxIter = MAX_ITERATIONS;
  52. btVector3 n;
  53. n.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
  54. bool hasResult = false;
  55. btVector3 c;
  56. btScalar lastLambda = lambda;
  57. btScalar dist2 = v.length2();
  58. #ifdef BT_USE_DOUBLE_PRECISION
  59. btScalar epsilon = btScalar(0.0001);
  60. #else
  61. btScalar epsilon = btScalar(0.0001);
  62. #endif //BT_USE_DOUBLE_PRECISION
  63. btVector3 w,p;
  64. btScalar VdotR;
  65. while ( (dist2 > epsilon) && maxIter--)
  66. {
  67. supVertexA = interpolatedTransA(m_convexA->localGetSupportingVertex(-v*interpolatedTransA.getBasis()));
  68. supVertexB = interpolatedTransB(m_convexB->localGetSupportingVertex(v*interpolatedTransB.getBasis()));
  69. w = supVertexA-supVertexB;
  70. btScalar VdotW = v.dot(w);
  71. if (lambda > btScalar(1.0))
  72. {
  73. return false;
  74. }
  75. if ( VdotW > btScalar(0.))
  76. {
  77. VdotR = v.dot(r);
  78. if (VdotR >= -(SIMD_EPSILON*SIMD_EPSILON))
  79. return false;
  80. else
  81. {
  82. lambda = lambda - VdotW / VdotR;
  83. //interpolate to next lambda
  84. // x = s + lambda * r;
  85. interpolatedTransA.getOrigin().setInterpolate3(fromA.getOrigin(),toA.getOrigin(),lambda);
  86. interpolatedTransB.getOrigin().setInterpolate3(fromB.getOrigin(),toB.getOrigin(),lambda);
  87. //m_simplexSolver->reset();
  88. //check next line
  89. w = supVertexA-supVertexB;
  90. lastLambda = lambda;
  91. n = v;
  92. hasResult = true;
  93. }
  94. }
  95. ///Just like regular GJK only add the vertex if it isn't already (close) to current vertex, it would lead to divisions by zero and NaN etc.
  96. if (!m_simplexSolver->inSimplex(w))
  97. m_simplexSolver->addVertex( w, supVertexA , supVertexB);
  98. if (m_simplexSolver->closest(v))
  99. {
  100. dist2 = v.length2();
  101. hasResult = true;
  102. //todo: check this normal for validity
  103. //n=v;
  104. //printf("V=%f , %f, %f\n",v[0],v[1],v[2]);
  105. //printf("DIST2=%f\n",dist2);
  106. //printf("numverts = %i\n",m_simplexSolver->numVertices());
  107. } else
  108. {
  109. dist2 = btScalar(0.);
  110. }
  111. }
  112. //int numiter = MAX_ITERATIONS - maxIter;
  113. // printf("number of iterations: %d", numiter);
  114. //don't report a time of impact when moving 'away' from the hitnormal
  115. result.m_fraction = lambda;
  116. if (n.length2() >= (SIMD_EPSILON*SIMD_EPSILON))
  117. result.m_normal = n.normalized();
  118. else
  119. result.m_normal = btVector3(btScalar(0.0), btScalar(0.0), btScalar(0.0));
  120. //don't report time of impact for motion away from the contact normal (or causes minor penetration)
  121. if (result.m_normal.dot(r)>=-result.m_allowedPenetration)
  122. return false;
  123. btVector3 hitA,hitB;
  124. m_simplexSolver->compute_points(hitA,hitB);
  125. result.m_hitPoint=hitB;
  126. return true;
  127. }