| [1963] | 1 | /* | 
|---|
|  | 2 | Bullet Continuous Collision Detection and Physics Library | 
|---|
|  | 3 | Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/ | 
|---|
|  | 4 |  | 
|---|
|  | 5 | This software is provided 'as-is', without any express or implied warranty. | 
|---|
|  | 6 | In no event will the authors be held liable for any damages arising from the use of this software. | 
|---|
|  | 7 | Permission is granted to anyone to use this software for any purpose, | 
|---|
|  | 8 | including commercial applications, and to alter it and redistribute it freely, | 
|---|
|  | 9 | subject to the following restrictions: | 
|---|
|  | 10 |  | 
|---|
|  | 11 | 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. | 
|---|
|  | 12 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. | 
|---|
|  | 13 | 3. This notice may not be removed or altered from any source distribution. | 
|---|
|  | 14 | */ | 
|---|
|  | 15 |  | 
|---|
|  | 16 |  | 
|---|
|  | 17 | #include "btSubSimplexConvexCast.h" | 
|---|
|  | 18 | #include "BulletCollision/CollisionShapes/btConvexShape.h" | 
|---|
|  | 19 |  | 
|---|
|  | 20 | #include "BulletCollision/CollisionShapes/btMinkowskiSumShape.h" | 
|---|
|  | 21 | #include "BulletCollision/NarrowPhaseCollision/btSimplexSolverInterface.h" | 
|---|
|  | 22 | #include "btPointCollector.h" | 
|---|
|  | 23 | #include "LinearMath/btTransformUtil.h" | 
|---|
|  | 24 |  | 
|---|
|  | 25 | btSubsimplexConvexCast::btSubsimplexConvexCast (const btConvexShape* convexA,const btConvexShape* convexB,btSimplexSolverInterface* simplexSolver) | 
|---|
|  | 26 | :m_simplexSolver(simplexSolver), | 
|---|
|  | 27 | m_convexA(convexA),m_convexB(convexB) | 
|---|
|  | 28 | { | 
|---|
|  | 29 | } | 
|---|
|  | 30 |  | 
|---|
|  | 31 | ///Typically the conservative advancement reaches solution in a few iterations, clip it to 32 for degenerate cases. | 
|---|
|  | 32 | ///See discussion about this here http://continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=565 | 
|---|
|  | 33 | #ifdef BT_USE_DOUBLE_PRECISION | 
|---|
|  | 34 | #define MAX_ITERATIONS 64 | 
|---|
|  | 35 | #else | 
|---|
|  | 36 | #define MAX_ITERATIONS 32 | 
|---|
|  | 37 | #endif | 
|---|
|  | 38 | bool    btSubsimplexConvexCast::calcTimeOfImpact( | 
|---|
|  | 39 | const btTransform& fromA, | 
|---|
|  | 40 | const btTransform& toA, | 
|---|
|  | 41 | const btTransform& fromB, | 
|---|
|  | 42 | const btTransform& toB, | 
|---|
|  | 43 | CastResult& result) | 
|---|
|  | 44 | { | 
|---|
|  | 45 |  | 
|---|
|  | 46 | m_simplexSolver->reset(); | 
|---|
|  | 47 |  | 
|---|
|  | 48 | btVector3 linVelA,linVelB; | 
|---|
|  | 49 | linVelA = toA.getOrigin()-fromA.getOrigin(); | 
|---|
|  | 50 | linVelB = toB.getOrigin()-fromB.getOrigin(); | 
|---|
|  | 51 |  | 
|---|
|  | 52 | btScalar lambda = btScalar(0.); | 
|---|
|  | 53 |  | 
|---|
|  | 54 | btTransform interpolatedTransA = fromA; | 
|---|
|  | 55 | btTransform interpolatedTransB = fromB; | 
|---|
|  | 56 |  | 
|---|
|  | 57 | ///take relative motion | 
|---|
|  | 58 | btVector3 r = (linVelA-linVelB); | 
|---|
|  | 59 | btVector3 v; | 
|---|
|  | 60 |  | 
|---|
|  | 61 | btVector3 supVertexA = fromA(m_convexA->localGetSupportingVertex(-r*fromA.getBasis())); | 
|---|
|  | 62 | btVector3 supVertexB = fromB(m_convexB->localGetSupportingVertex(r*fromB.getBasis())); | 
|---|
|  | 63 | v = supVertexA-supVertexB; | 
|---|
|  | 64 | int maxIter = MAX_ITERATIONS; | 
|---|
|  | 65 |  | 
|---|
|  | 66 | btVector3 n; | 
|---|
|  | 67 | n.setValue(btScalar(0.),btScalar(0.),btScalar(0.)); | 
|---|
|  | 68 | bool hasResult = false; | 
|---|
|  | 69 | btVector3 c; | 
|---|
|  | 70 |  | 
|---|
|  | 71 | btScalar lastLambda = lambda; | 
|---|
|  | 72 |  | 
|---|
|  | 73 |  | 
|---|
|  | 74 | btScalar dist2 = v.length2(); | 
|---|
|  | 75 | #ifdef BT_USE_DOUBLE_PRECISION | 
|---|
|  | 76 | btScalar epsilon = btScalar(0.0001); | 
|---|
|  | 77 | #else | 
|---|
|  | 78 | btScalar epsilon = btScalar(0.0001); | 
|---|
|  | 79 | #endif //BT_USE_DOUBLE_PRECISION | 
|---|
|  | 80 | btVector3       w,p; | 
|---|
|  | 81 | btScalar VdotR; | 
|---|
|  | 82 |  | 
|---|
|  | 83 | while ( (dist2 > epsilon) && maxIter--) | 
|---|
|  | 84 | { | 
|---|
|  | 85 | supVertexA = interpolatedTransA(m_convexA->localGetSupportingVertex(-v*interpolatedTransA.getBasis())); | 
|---|
|  | 86 | supVertexB = interpolatedTransB(m_convexB->localGetSupportingVertex(v*interpolatedTransB.getBasis())); | 
|---|
|  | 87 | w = supVertexA-supVertexB; | 
|---|
|  | 88 |  | 
|---|
|  | 89 | btScalar VdotW = v.dot(w); | 
|---|
|  | 90 |  | 
|---|
|  | 91 | if (lambda > btScalar(1.0)) | 
|---|
|  | 92 | { | 
|---|
|  | 93 | return false; | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
|  | 96 | if ( VdotW > btScalar(0.)) | 
|---|
|  | 97 | { | 
|---|
|  | 98 | VdotR = v.dot(r); | 
|---|
|  | 99 |  | 
|---|
|  | 100 | if (VdotR >= -(SIMD_EPSILON*SIMD_EPSILON)) | 
|---|
|  | 101 | return false; | 
|---|
|  | 102 | else | 
|---|
|  | 103 | { | 
|---|
|  | 104 | lambda = lambda - VdotW / VdotR; | 
|---|
|  | 105 | //interpolate to next lambda | 
|---|
|  | 106 | //      x = s + lambda * r; | 
|---|
|  | 107 | interpolatedTransA.getOrigin().setInterpolate3(fromA.getOrigin(),toA.getOrigin(),lambda); | 
|---|
|  | 108 | interpolatedTransB.getOrigin().setInterpolate3(fromB.getOrigin(),toB.getOrigin(),lambda); | 
|---|
|  | 109 | //m_simplexSolver->reset(); | 
|---|
|  | 110 | //check next line | 
|---|
|  | 111 | w = supVertexA-supVertexB; | 
|---|
|  | 112 | lastLambda = lambda; | 
|---|
|  | 113 | n = v; | 
|---|
|  | 114 | hasResult = true; | 
|---|
|  | 115 | } | 
|---|
|  | 116 | } | 
|---|
|  | 117 | m_simplexSolver->addVertex( w, supVertexA , supVertexB); | 
|---|
|  | 118 | if (m_simplexSolver->closest(v)) | 
|---|
|  | 119 | { | 
|---|
|  | 120 | dist2 = v.length2(); | 
|---|
|  | 121 | hasResult = true; | 
|---|
|  | 122 | //todo: check this normal for validity | 
|---|
|  | 123 | //n=v; | 
|---|
|  | 124 | //printf("V=%f , %f, %f\n",v[0],v[1],v[2]); | 
|---|
|  | 125 | //printf("DIST2=%f\n",dist2); | 
|---|
|  | 126 | //printf("numverts = %i\n",m_simplexSolver->numVertices()); | 
|---|
|  | 127 | } else | 
|---|
|  | 128 | { | 
|---|
|  | 129 | dist2 = btScalar(0.); | 
|---|
|  | 130 | } | 
|---|
|  | 131 | } | 
|---|
|  | 132 |  | 
|---|
|  | 133 | //int numiter = MAX_ITERATIONS - maxIter; | 
|---|
|  | 134 | //      printf("number of iterations: %d", numiter); | 
|---|
|  | 135 |  | 
|---|
|  | 136 | //don't report a time of impact when moving 'away' from the hitnormal | 
|---|
|  | 137 |  | 
|---|
|  | 138 |  | 
|---|
|  | 139 | result.m_fraction = lambda; | 
|---|
|  | 140 | if (n.length2() >= (SIMD_EPSILON*SIMD_EPSILON)) | 
|---|
|  | 141 | result.m_normal = n.normalized(); | 
|---|
|  | 142 | else | 
|---|
|  | 143 | result.m_normal = btVector3(btScalar(0.0), btScalar(0.0), btScalar(0.0)); | 
|---|
|  | 144 |  | 
|---|
|  | 145 | //don't report time of impact for motion away from the contact normal (or causes minor penetration) | 
|---|
|  | 146 | if (result.m_normal.dot(r)>=-result.m_allowedPenetration) | 
|---|
|  | 147 | return false; | 
|---|
|  | 148 |  | 
|---|
|  | 149 | btVector3 hitA,hitB; | 
|---|
|  | 150 | m_simplexSolver->compute_points(hitA,hitB); | 
|---|
|  | 151 | result.m_hitPoint=hitB; | 
|---|
|  | 152 | return true; | 
|---|
|  | 153 | } | 
|---|
|  | 154 |  | 
|---|
|  | 155 |  | 
|---|
|  | 156 |  | 
|---|
|  | 157 |  | 
|---|