2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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:
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.
17 #include "btSubSimplexConvexCast.h"
18 #include "BulletCollision/CollisionShapes/btConvexShape.h"
20 #include "BulletCollision/CollisionShapes/btMinkowskiSumShape.h"
21 #include "BulletCollision/NarrowPhaseCollision/btSimplexSolverInterface.h"
22 #include "btPointCollector.h"
23 #include "LinearMath/btTransformUtil.h"
25 btSubsimplexConvexCast::btSubsimplexConvexCast (const btConvexShape* convexA,const btConvexShape* convexB,btSimplexSolverInterface* simplexSolver)
26 :m_simplexSolver(simplexSolver),
27 m_convexA(convexA),m_convexB(convexB)
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
36 #define MAX_ITERATIONS 32
38 bool btSubsimplexConvexCast::calcTimeOfImpact(
39 const btTransform& fromA,
40 const btTransform& toA,
41 const btTransform& fromB,
42 const btTransform& toB,
46 m_simplexSolver->reset();
48 btVector3 linVelA,linVelB;
49 linVelA = toA.getOrigin()-fromA.getOrigin();
50 linVelB = toB.getOrigin()-fromB.getOrigin();
52 btScalar lambda = btScalar(0.);
54 btTransform interpolatedTransA = fromA;
55 btTransform interpolatedTransB = fromB;
57 ///take relative motion
58 btVector3 r = (linVelA-linVelB);
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;
67 n.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
68 bool hasResult = false;
71 btScalar lastLambda = lambda;
74 btScalar dist2 = v.length2();
75 #ifdef BT_USE_DOUBLE_PRECISION
76 btScalar epsilon = btScalar(0.0001);
78 btScalar epsilon = btScalar(0.0001);
79 #endif //BT_USE_DOUBLE_PRECISION
83 while ( (dist2 > epsilon) && maxIter--)
85 supVertexA = interpolatedTransA(m_convexA->localGetSupportingVertex(-v*interpolatedTransA.getBasis()));
86 supVertexB = interpolatedTransB(m_convexB->localGetSupportingVertex(v*interpolatedTransB.getBasis()));
87 w = supVertexA-supVertexB;
89 btScalar VdotW = v.dot(w);
91 if (lambda > btScalar(1.0))
96 if ( VdotW > btScalar(0.))
100 if (VdotR >= -(SIMD_EPSILON*SIMD_EPSILON))
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();
111 w = supVertexA-supVertexB;
117 ///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.
118 if (!m_simplexSolver->inSimplex(w))
119 m_simplexSolver->addVertex( w, supVertexA , supVertexB);
121 if (m_simplexSolver->closest(v))
125 //todo: check this normal for validity
127 //printf("V=%f , %f, %f\n",v[0],v[1],v[2]);
128 //printf("DIST2=%f\n",dist2);
129 //printf("numverts = %i\n",m_simplexSolver->numVertices());
132 dist2 = btScalar(0.);
136 //int numiter = MAX_ITERATIONS - maxIter;
137 // printf("number of iterations: %d", numiter);
139 //don't report a time of impact when moving 'away' from the hitnormal
142 result.m_fraction = lambda;
143 if (n.length2() >= (SIMD_EPSILON*SIMD_EPSILON))
144 result.m_normal = n.normalized();
146 result.m_normal = btVector3(btScalar(0.0), btScalar(0.0), btScalar(0.0));
148 //don't report time of impact for motion away from the contact normal (or causes minor penetration)
149 if (result.m_normal.dot(r)>=-result.m_allowedPenetration)
153 m_simplexSolver->compute_points(hitA,hitB);
154 result.m_hitPoint=hitB;