2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
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.
16 #include "btGjkConvexCast.h"
17 #include "BulletCollision/CollisionShapes/btSphereShape.h"
18 #include "btGjkPairDetector.h"
19 #include "btPointCollector.h"
20 #include "LinearMath/btTransformUtil.h"
22 #ifdef BT_USE_DOUBLE_PRECISION
23 #define MAX_ITERATIONS 64
25 #define MAX_ITERATIONS 32
28 btGjkConvexCast::btGjkConvexCast(const btConvexShape* convexA, const btConvexShape* convexB, btSimplexSolverInterface* simplexSolver)
29 : m_simplexSolver(simplexSolver),
35 bool btGjkConvexCast::calcTimeOfImpact(
36 const btTransform& fromA,
37 const btTransform& toA,
38 const btTransform& fromB,
39 const btTransform& toB,
42 m_simplexSolver->reset();
44 /// compute linear velocity for this interval, to interpolate
45 //assume no rotation/angular velocity, assert here?
46 btVector3 linVelA, linVelB;
47 linVelA = toA.getOrigin() - fromA.getOrigin();
48 linVelB = toB.getOrigin() - fromB.getOrigin();
50 btScalar radius = btScalar(0.001);
51 btScalar lambda = btScalar(0.);
54 int maxIter = MAX_ITERATIONS;
57 n.setValue(btScalar(0.), btScalar(0.), btScalar(0.));
58 bool hasResult = false;
60 btVector3 r = (linVelA - linVelB);
62 btScalar lastLambda = lambda;
63 //btScalar epsilon = btScalar(0.001);
66 //first solution, using GJK
68 btTransform identityTrans;
69 identityTrans.setIdentity();
71 // result.drawCoordSystem(sphereTr);
73 btPointCollector pointCollector;
75 btGjkPairDetector gjk(m_convexA, m_convexB, m_simplexSolver, 0); //m_penetrationDepthSolver);
76 btGjkPairDetector::ClosestPointInput input;
78 //we don't use margins during CCD
79 // gjk.setIgnoreMargin(true);
81 input.m_transformA = fromA;
82 input.m_transformB = fromB;
83 gjk.getClosestPoints(input, pointCollector, 0);
85 hasResult = pointCollector.m_hasResult;
86 c = pointCollector.m_pointInWorld;
91 dist = pointCollector.m_distance;
92 n = pointCollector.m_normalOnBInWorld;
98 if (numIter > maxIter)
100 return false; //todo: report a failure
102 btScalar dLambda = btScalar(0.);
104 btScalar projectedLinearVelocity = r.dot(n);
106 dLambda = dist / (projectedLinearVelocity);
108 lambda = lambda - dLambda;
110 if (lambda > btScalar(1.))
113 if (lambda < btScalar(0.))
116 //todo: next check with relative epsilon
117 if (lambda <= lastLambda)
125 //interpolate to next lambda
126 result.DebugDraw(lambda);
127 input.m_transformA.getOrigin().setInterpolate3(fromA.getOrigin(), toA.getOrigin(), lambda);
128 input.m_transformB.getOrigin().setInterpolate3(fromB.getOrigin(), toB.getOrigin(), lambda);
130 gjk.getClosestPoints(input, pointCollector, 0);
131 if (pointCollector.m_hasResult)
133 if (pointCollector.m_distance < btScalar(0.))
135 result.m_fraction = lastLambda;
136 n = pointCollector.m_normalOnBInWorld;
138 result.m_hitPoint = pointCollector.m_pointInWorld;
141 c = pointCollector.m_pointInWorld;
142 n = pointCollector.m_normalOnBInWorld;
143 dist = pointCollector.m_distance;
153 //don't report time of impact for motion away from the contact normal (or causes minor penetration)
154 if (n.dot(r) >= -result.m_allowedPenetration)
157 result.m_fraction = lambda;
159 result.m_hitPoint = c;