btConvex2dConvex2dAlgorithm.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans https://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. #include "btConvex2dConvex2dAlgorithm.h"
  14. //#include <stdio.h>
  15. #include "BulletCollision/NarrowPhaseCollision/btDiscreteCollisionDetectorInterface.h"
  16. #include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h"
  17. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  18. #include "BulletCollision/CollisionShapes/btConvexShape.h"
  19. #include "BulletCollision/CollisionShapes/btCapsuleShape.h"
  20. #include "BulletCollision/NarrowPhaseCollision/btGjkPairDetector.h"
  21. #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
  22. #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
  23. #include "BulletCollision/CollisionShapes/btBoxShape.h"
  24. #include "BulletCollision/CollisionDispatch/btManifoldResult.h"
  25. #include "BulletCollision/NarrowPhaseCollision/btConvexPenetrationDepthSolver.h"
  26. #include "BulletCollision/NarrowPhaseCollision/btContinuousConvexCollision.h"
  27. #include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
  28. #include "BulletCollision/NarrowPhaseCollision/btGjkConvexCast.h"
  29. #include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
  30. #include "BulletCollision/CollisionShapes/btSphereShape.h"
  31. #include "BulletCollision/NarrowPhaseCollision/btMinkowskiPenetrationDepthSolver.h"
  32. #include "BulletCollision/NarrowPhaseCollision/btGjkEpa2.h"
  33. #include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h"
  34. #include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
  35. btConvex2dConvex2dAlgorithm::CreateFunc::CreateFunc(btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver)
  36. {
  37. m_simplexSolver = simplexSolver;
  38. m_pdSolver = pdSolver;
  39. }
  40. btConvex2dConvex2dAlgorithm::CreateFunc::~CreateFunc()
  41. {
  42. }
  43. btConvex2dConvex2dAlgorithm::btConvex2dConvex2dAlgorithm(btPersistentManifold* mf, const btCollisionAlgorithmConstructionInfo& ci, const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap, btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver, int /* numPerturbationIterations */, int /* minimumPointsPerturbationThreshold */)
  44. : btActivatingCollisionAlgorithm(ci, body0Wrap, body1Wrap),
  45. m_simplexSolver(simplexSolver),
  46. m_pdSolver(pdSolver),
  47. m_ownManifold(false),
  48. m_manifoldPtr(mf),
  49. m_lowLevelOfDetail(false)
  50. {
  51. (void)body0Wrap;
  52. (void)body1Wrap;
  53. }
  54. btConvex2dConvex2dAlgorithm::~btConvex2dConvex2dAlgorithm()
  55. {
  56. if (m_ownManifold)
  57. {
  58. if (m_manifoldPtr)
  59. m_dispatcher->releaseManifold(m_manifoldPtr);
  60. }
  61. }
  62. void btConvex2dConvex2dAlgorithm ::setLowLevelOfDetail(bool useLowLevel)
  63. {
  64. m_lowLevelOfDetail = useLowLevel;
  65. }
  66. extern btScalar gContactBreakingThreshold;
  67. //
  68. // Convex-Convex collision algorithm
  69. //
  70. void btConvex2dConvex2dAlgorithm ::processCollision(const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap, const btDispatcherInfo& dispatchInfo, btManifoldResult* resultOut)
  71. {
  72. if (!m_manifoldPtr)
  73. {
  74. //swapped?
  75. m_manifoldPtr = m_dispatcher->getNewManifold(body0Wrap->getCollisionObject(), body1Wrap->getCollisionObject());
  76. m_ownManifold = true;
  77. }
  78. resultOut->setPersistentManifold(m_manifoldPtr);
  79. //comment-out next line to test multi-contact generation
  80. //resultOut->getPersistentManifold()->clearManifold();
  81. const btConvexShape* min0 = static_cast<const btConvexShape*>(body0Wrap->getCollisionShape());
  82. const btConvexShape* min1 = static_cast<const btConvexShape*>(body1Wrap->getCollisionShape());
  83. btVector3 normalOnB;
  84. btVector3 pointOnBWorld;
  85. {
  86. btGjkPairDetector::ClosestPointInput input;
  87. btGjkPairDetector gjkPairDetector(min0, min1, m_simplexSolver, m_pdSolver);
  88. //TODO: if (dispatchInfo.m_useContinuous)
  89. gjkPairDetector.setMinkowskiA(min0);
  90. gjkPairDetector.setMinkowskiB(min1);
  91. {
  92. input.m_maximumDistanceSquared = min0->getMargin() + min1->getMargin() + m_manifoldPtr->getContactBreakingThreshold();
  93. input.m_maximumDistanceSquared *= input.m_maximumDistanceSquared;
  94. }
  95. input.m_transformA = body0Wrap->getWorldTransform();
  96. input.m_transformB = body1Wrap->getWorldTransform();
  97. gjkPairDetector.getClosestPoints(input, *resultOut, dispatchInfo.m_debugDraw);
  98. btVector3 v0, v1;
  99. btVector3 sepNormalWorldSpace;
  100. }
  101. if (m_ownManifold)
  102. {
  103. resultOut->refreshContactPoints();
  104. }
  105. }
  106. btScalar btConvex2dConvex2dAlgorithm::calculateTimeOfImpact(btCollisionObject* col0, btCollisionObject* col1, const btDispatcherInfo& dispatchInfo, btManifoldResult* resultOut)
  107. {
  108. (void)resultOut;
  109. (void)dispatchInfo;
  110. ///Rather then checking ALL pairs, only calculate TOI when motion exceeds threshold
  111. ///Linear motion for one of objects needs to exceed m_ccdSquareMotionThreshold
  112. ///col0->m_worldTransform,
  113. btScalar resultFraction = btScalar(1.);
  114. btScalar squareMot0 = (col0->getInterpolationWorldTransform().getOrigin() - col0->getWorldTransform().getOrigin()).length2();
  115. btScalar squareMot1 = (col1->getInterpolationWorldTransform().getOrigin() - col1->getWorldTransform().getOrigin()).length2();
  116. if (squareMot0 < col0->getCcdSquareMotionThreshold() &&
  117. squareMot1 < col1->getCcdSquareMotionThreshold())
  118. return resultFraction;
  119. //An adhoc way of testing the Continuous Collision Detection algorithms
  120. //One object is approximated as a sphere, to simplify things
  121. //Starting in penetration should report no time of impact
  122. //For proper CCD, better accuracy and handling of 'allowed' penetration should be added
  123. //also the mainloop of the physics should have a kind of toi queue (something like Brian Mirtich's application of Timewarp for Rigidbodies)
  124. /// Convex0 against sphere for Convex1
  125. {
  126. btConvexShape* convex0 = static_cast<btConvexShape*>(col0->getCollisionShape());
  127. btSphereShape sphere1(col1->getCcdSweptSphereRadius()); //todo: allow non-zero sphere sizes, for better approximation
  128. btConvexCast::CastResult result;
  129. btVoronoiSimplexSolver voronoiSimplex;
  130. //SubsimplexConvexCast ccd0(&sphere,min0,&voronoiSimplex);
  131. ///Simplification, one object is simplified as a sphere
  132. btGjkConvexCast ccd1(convex0, &sphere1, &voronoiSimplex);
  133. //ContinuousConvexCollision ccd(min0,min1,&voronoiSimplex,0);
  134. if (ccd1.calcTimeOfImpact(col0->getWorldTransform(), col0->getInterpolationWorldTransform(),
  135. col1->getWorldTransform(), col1->getInterpolationWorldTransform(), result))
  136. {
  137. //store result.m_fraction in both bodies
  138. if (col0->getHitFraction() > result.m_fraction)
  139. col0->setHitFraction(result.m_fraction);
  140. if (col1->getHitFraction() > result.m_fraction)
  141. col1->setHitFraction(result.m_fraction);
  142. if (resultFraction > result.m_fraction)
  143. resultFraction = result.m_fraction;
  144. }
  145. }
  146. /// Sphere (for convex0) against Convex1
  147. {
  148. btConvexShape* convex1 = static_cast<btConvexShape*>(col1->getCollisionShape());
  149. btSphereShape sphere0(col0->getCcdSweptSphereRadius()); //todo: allow non-zero sphere sizes, for better approximation
  150. btConvexCast::CastResult result;
  151. btVoronoiSimplexSolver voronoiSimplex;
  152. //SubsimplexConvexCast ccd0(&sphere,min0,&voronoiSimplex);
  153. ///Simplification, one object is simplified as a sphere
  154. btGjkConvexCast ccd1(&sphere0, convex1, &voronoiSimplex);
  155. //ContinuousConvexCollision ccd(min0,min1,&voronoiSimplex,0);
  156. if (ccd1.calcTimeOfImpact(col0->getWorldTransform(), col0->getInterpolationWorldTransform(),
  157. col1->getWorldTransform(), col1->getInterpolationWorldTransform(), result))
  158. {
  159. //store result.m_fraction in both bodies
  160. if (col0->getHitFraction() > result.m_fraction)
  161. col0->setHitFraction(result.m_fraction);
  162. if (col1->getHitFraction() > result.m_fraction)
  163. col1->setHitFraction(result.m_fraction);
  164. if (resultFraction > result.m_fraction)
  165. resultFraction = result.m_fraction;
  166. }
  167. }
  168. return resultFraction;
  169. }