btConvex2dConvex2dAlgorithm.cpp 8.8 KB

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