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.
18
#include "btGjkConvexCast.h"
19
#include "BulletCollision/CollisionShapes/btSphereShape.h"
20
#include "btGjkPairDetector.h"
21
#include "btPointCollector.h"
22
#include "LinearMath/btTransformUtil.h"
24
#ifdef BT_USE_DOUBLE_PRECISION
25
#define MAX_ITERATIONS 64
27
#define MAX_ITERATIONS 32
30
btGjkConvexCast::btGjkConvexCast(const btConvexShape* convexA,const btConvexShape* convexB,btSimplexSolverInterface* simplexSolver)
31
:m_simplexSolver(simplexSolver),
37
bool btGjkConvexCast::calcTimeOfImpact(
38
const btTransform& fromA,
39
const btTransform& toA,
40
const btTransform& fromB,
41
const btTransform& toB,
46
m_simplexSolver->reset();
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();
54
btScalar radius = btScalar(0.001);
55
btScalar lambda = btScalar(0.);
58
int maxIter = MAX_ITERATIONS;
61
n.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
62
bool hasResult = false;
64
btVector3 r = (linVelA-linVelB);
66
btScalar lastLambda = lambda;
67
//btScalar epsilon = btScalar(0.001);
70
//first solution, using GJK
73
btTransform identityTrans;
74
identityTrans.setIdentity();
77
// result.drawCoordSystem(sphereTr);
79
btPointCollector pointCollector;
82
btGjkPairDetector gjk(m_convexA,m_convexB,m_simplexSolver,0);//m_penetrationDepthSolver);
83
btGjkPairDetector::ClosestPointInput input;
85
//we don't use margins during CCD
86
// gjk.setIgnoreMargin(true);
88
input.m_transformA = fromA;
89
input.m_transformB = fromB;
90
gjk.getClosestPoints(input,pointCollector,0);
92
hasResult = pointCollector.m_hasResult;
93
c = pointCollector.m_pointInWorld;
98
dist = pointCollector.m_distance;
99
n = pointCollector.m_normalOnBInWorld;
104
while (dist > radius)
107
if (numIter > maxIter)
109
return false; //todo: report a failure
111
btScalar dLambda = btScalar(0.);
113
btScalar projectedLinearVelocity = r.dot(n);
115
dLambda = dist / (projectedLinearVelocity);
117
lambda = lambda - dLambda;
119
if (lambda > btScalar(1.))
122
if (lambda < btScalar(0.))
125
//todo: next check with relative epsilon
126
if (lambda <= lastLambda)
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);
139
gjk.getClosestPoints(input,pointCollector,0);
140
if (pointCollector.m_hasResult)
142
if (pointCollector.m_distance < btScalar(0.))
144
result.m_fraction = lastLambda;
145
n = pointCollector.m_normalOnBInWorld;
147
result.m_hitPoint = pointCollector.m_pointInWorld;
150
c = pointCollector.m_pointInWorld;
151
n = pointCollector.m_normalOnBInWorld;
152
dist = pointCollector.m_distance;
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)
166
result.m_fraction = lambda;
168
result.m_hitPoint = c;