Bullet Collision Detection & Physics Library
btGjkConvexCast.cpp
Go to the documentation of this file.
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"
20 #include "btGjkPairDetector.h"
21 #include "btPointCollector.h"
23 
24 #ifdef BT_USE_DOUBLE_PRECISION
25 #define MAX_ITERATIONS 64
26 #else
27 #define MAX_ITERATIONS 32
28 #endif
29 
31 :m_simplexSolver(simplexSolver),
32 m_convexA(convexA),
33 m_convexB(convexB)
34 {
35 }
36 
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 
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);
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 
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:640
float dist(const Point3 &pnt0, const Point3 &pnt1)
void setIdentity()
Set this transformation to the identity.
Definition: btTransform.h:172
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:235
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:31
virtual bool calcTimeOfImpact(const btTransform &fromA, const btTransform &toA, const btTransform &fromB, const btTransform &toB, CastResult &result)
cast a convex against another convex object
virtual void DebugDraw(btScalar fraction)
Definition: btConvexCast.h:40
RayResult stores the closest result alternatively, add a callback method to decide about closest/all ...
Definition: btConvexCast.h:36
btVector3 & getOrigin()
Return the origin vector translation.
Definition: btTransform.h:117
#define btSimplexSolverInterface
btVector3 m_pointInWorld
const btConvexShape * m_convexA
btGjkConvexCast(const btConvexShape *convexA, const btConvexShape *convexB, btSimplexSolverInterface *simplexSolver)
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btSimplexSolverInterface * m_simplexSolver
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
btGjkPairDetector uses GJK to implement the btDiscreteCollisionDetectorInterface
#define MAX_ITERATIONS
const btConvexShape * m_convexB
void setInterpolate3(const btVector3 &v0, const btVector3 &v1, btScalar rt)
Definition: btVector3.h:491
btVector3 m_normalOnBInWorld
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:266
virtual void getClosestPoints(const ClosestPointInput &input, Result &output, class btIDebugDraw *debugDraw, bool swapResults=false)