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
btSubsimplexConvexCast(const btConvexShape * convexA,const btConvexShape * convexB,btSimplexSolverInterface * simplexSolver)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
calcTimeOfImpact(const btTransform & fromA,const btTransform & toA,const btTransform & fromB,const btTransform & toB,CastResult & result)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
69 btVector3 c;
70
71
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
113 n = v;
114
115 }
116 }
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);
120
121 if (m_simplexSolver->closest(v))
122 {
123 dist2 = v.length2();
124
125 //todo: check this normal for validity
126 //n=v;
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());
130 } else
131 {
132 dist2 = btScalar(0.);
133 }
134 }
135
136 //int numiter = MAX_ITERATIONS - maxIter;
137 // printf("number of iterations: %d", numiter);
138
139 //don't report a time of impact when moving 'away' from the hitnormal
140
141
142 result.m_fraction = lambda;
143 if (n.length2() >= (SIMD_EPSILON*SIMD_EPSILON))
144 result.m_normal = n.normalized();
145 else
146 result.m_normal = btVector3(btScalar(0.0), btScalar(0.0), btScalar(0.0));
147
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)
150 return false;
151
152 btVector3 hitA,hitB;
153 m_simplexSolver->compute_points(hitA,hitB);
154 result.m_hitPoint=hitB;
155 return true;
156 }
157
158
159
160
161