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
18 #include "btGjkConvexCast.h"
19 #include "BulletCollision/CollisionShapes/btSphereShape.h"
20 #include "btGjkPairDetector.h"
21 #include "btPointCollector.h"
22 #include "LinearMath/btTransformUtil.h"
23
24 #ifdef BT_USE_DOUBLE_PRECISION
25 #define MAX_ITERATIONS 64
26 #else
27 #define MAX_ITERATIONS 32
28 #endif
29
btGjkConvexCast(const btConvexShape * convexA,const btConvexShape * convexB,btSimplexSolverInterface * simplexSolver)30 btGjkConvexCast::btGjkConvexCast(const btConvexShape* convexA,const btConvexShape* convexB,btSimplexSolverInterface* simplexSolver)
31 :m_simplexSolver(simplexSolver),
32 m_convexA(convexA),
33 m_convexB(convexB)
34 {
35 }
36
calcTimeOfImpact(const btTransform & fromA,const btTransform & toA,const btTransform & fromB,const btTransform & toB,CastResult & result)37 bool btGjkConvexCast::calcTimeOfImpact(
38 const btTransform& fromA,
39 const btTransform& toA,
40 const btTransform& fromB,
41 const btTransform& toB,
42 CastResult& result)
43 {
44
45
46 m_simplexSolver->reset();
47
48 /// compute linear velocity for this interval, to interpolate
49 //assume no rotation/angular velocity, assert here?
50 btVector3 linVelA,linVelB;
51 linVelA = toA.getOrigin()-fromA.getOrigin();
52 linVelB = toB.getOrigin()-fromB.getOrigin();
53
54 btScalar radius = btScalar(0.001);
55 btScalar lambda = btScalar(0.);
56 btVector3 v(1,0,0);
57
58 int maxIter = MAX_ITERATIONS;
59
60 btVector3 n;
61 n.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
62 bool hasResult = false;
63 btVector3 c;
64 btVector3 r = (linVelA-linVelB);
65
66 btScalar lastLambda = lambda;
67 //btScalar epsilon = btScalar(0.001);
68
69 int numIter = 0;
70 //first solution, using GJK
71
72
73 btTransform identityTrans;
74 identityTrans.setIdentity();
75
76
77 // result.drawCoordSystem(sphereTr);
78
79 btPointCollector pointCollector;
80
81
82 btGjkPairDetector gjk(m_convexA,m_convexB,m_simplexSolver,0);//m_penetrationDepthSolver);
83 btGjkPairDetector::ClosestPointInput input;
84
85 //we don't use margins during CCD
86 // gjk.setIgnoreMargin(true);
87
88 input.m_transformA = fromA;
89 input.m_transformB = fromB;
90 gjk.getClosestPoints(input,pointCollector,0);
91
92 hasResult = pointCollector.m_hasResult;
93 c = pointCollector.m_pointInWorld;
94
95 if (hasResult)
96 {
97 btScalar dist;
98 dist = pointCollector.m_distance;
99 n = pointCollector.m_normalOnBInWorld;
100
101
102
103 //not close enough
104 while (dist > radius)
105 {
106 numIter++;
107 if (numIter > maxIter)
108 {
109 return false; //todo: report a failure
110 }
111 btScalar dLambda = btScalar(0.);
112
113 btScalar projectedLinearVelocity = r.dot(n);
114
115 dLambda = dist / (projectedLinearVelocity);
116
117 lambda = lambda - dLambda;
118
119 if (lambda > btScalar(1.))
120 return false;
121
122 if (lambda < btScalar(0.))
123 return false;
124
125 //todo: next check with relative epsilon
126 if (lambda <= lastLambda)
127 {
128 return false;
129 //n.setValue(0,0,0);
130 break;
131 }
132 lastLambda = lambda;
133
134 //interpolate to next lambda
135 result.DebugDraw( lambda );
136 input.m_transformA.getOrigin().setInterpolate3(fromA.getOrigin(),toA.getOrigin(),lambda);
137 input.m_transformB.getOrigin().setInterpolate3(fromB.getOrigin(),toB.getOrigin(),lambda);
138
139 gjk.getClosestPoints(input,pointCollector,0);
140 if (pointCollector.m_hasResult)
141 {
142 if (pointCollector.m_distance < btScalar(0.))
143 {
144 result.m_fraction = lastLambda;
145 n = pointCollector.m_normalOnBInWorld;
146 result.m_normal=n;
147 result.m_hitPoint = pointCollector.m_pointInWorld;
148 return true;
149 }
150 c = pointCollector.m_pointInWorld;
151 n = pointCollector.m_normalOnBInWorld;
152 dist = pointCollector.m_distance;
153 } else
154 {
155 //??
156 return false;
157 }
158
159 }
160
161 //is n normalized?
162 //don't report time of impact for motion away from the contact normal (or causes minor penetration)
163 if (n.dot(r)>=-result.m_allowedPenetration)
164 return false;
165
166 result.m_fraction = lambda;
167 result.m_normal = n;
168 result.m_hitPoint = c;
169 return true;
170 }
171
172 return false;
173
174
175 }
176
177