Bullet Collision Detection & Physics Library
btContinuousConvexCollision.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 
22 
23 #include "btGjkPairDetector.h"
24 #include "btPointCollector.h"
26 
27 
28 
30 :m_simplexSolver(simplexSolver),
31 m_penetrationDepthSolver(penetrationDepthSolver),
32 m_convexA(convexA),m_convexB1(convexB),m_planeShape(0)
33 {
34 }
35 
36 
38 :m_simplexSolver(0),
40 m_convexA(convexA),m_convexB1(0),m_planeShape(plane)
41 {
42 }
43 
44 
47 #define MAX_ITERATIONS 64
48 
50 {
51  if (m_convexB1)
52  {
53  m_simplexSolver->reset();
56  input.m_transformA = transA;
57  input.m_transformB = transB;
58  gjk.getClosestPoints(input,pointCollector,0);
59  } else
60  {
61  //convex versus plane
62  const btConvexShape* convexShape = m_convexA;
63  const btStaticPlaneShape* planeShape = m_planeShape;
64 
65  const btVector3& planeNormal = planeShape->getPlaneNormal();
66  const btScalar& planeConstant = planeShape->getPlaneConstant();
67 
68  btTransform convexWorldTransform = transA;
69  btTransform convexInPlaneTrans;
70  convexInPlaneTrans= transB.inverse() * convexWorldTransform;
71  btTransform planeInConvex;
72  planeInConvex= convexWorldTransform.inverse() * transB;
73 
74  btVector3 vtx = convexShape->localGetSupportingVertex(planeInConvex.getBasis()*-planeNormal);
75 
76  btVector3 vtxInPlane = convexInPlaneTrans(vtx);
77  btScalar distance = (planeNormal.dot(vtxInPlane) - planeConstant);
78 
79  btVector3 vtxInPlaneProjected = vtxInPlane - distance*planeNormal;
80  btVector3 vtxInPlaneWorld = transB * vtxInPlaneProjected;
81  btVector3 normalOnSurfaceB = transB.getBasis() * planeNormal;
82 
83  pointCollector.addContactPoint(
84  normalOnSurfaceB,
85  vtxInPlaneWorld,
86  distance);
87  }
88 }
89 
91  const btTransform& fromA,
92  const btTransform& toA,
93  const btTransform& fromB,
94  const btTransform& toB,
95  CastResult& result)
96 {
97 
98 
100  btVector3 linVelA,angVelA,linVelB,angVelB;
101  btTransformUtil::calculateVelocity(fromA,toA,btScalar(1.),linVelA,angVelA);
102  btTransformUtil::calculateVelocity(fromB,toB,btScalar(1.),linVelB,angVelB);
103 
104 
105  btScalar boundingRadiusA = m_convexA->getAngularMotionDisc();
106  btScalar boundingRadiusB = m_convexB1?m_convexB1->getAngularMotionDisc():0.f;
107 
108  btScalar maxAngularProjectedVelocity = angVelA.length() * boundingRadiusA + angVelB.length() * boundingRadiusB;
109  btVector3 relLinVel = (linVelB-linVelA);
110 
111  btScalar relLinVelocLength = (linVelB-linVelA).length();
112 
113  if ((relLinVelocLength+maxAngularProjectedVelocity) == 0.f)
114  return false;
115 
116  btScalar lambda = btScalar(0.);
117 
118  btVector3 n;
119  n.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
120  bool hasResult = false;
121  btVector3 c;
122 
123  btScalar lastLambda = lambda;
124  //btScalar epsilon = btScalar(0.001);
125 
126  int numIter = 0;
127  //first solution, using GJK
128 
129 
130  btScalar radius = 0.001f;
131 // result.drawCoordSystem(sphereTr);
132 
133  btPointCollector pointCollector1;
134 
135  {
136  computeClosestPoints(fromA,fromB,pointCollector1);
137 
138  hasResult = pointCollector1.m_hasResult;
139  c = pointCollector1.m_pointInWorld;
140  }
141 
142  if (hasResult)
143  {
144  btScalar dist;
145  dist = pointCollector1.m_distance + result.m_allowedPenetration;
146  n = pointCollector1.m_normalOnBInWorld;
147  btScalar projectedLinearVelocity = relLinVel.dot(n);
148  if ((projectedLinearVelocity+ maxAngularProjectedVelocity)<=SIMD_EPSILON)
149  return false;
150 
151  //not close enough
152  while (dist > radius)
153  {
154  if (result.m_debugDrawer)
155  {
156  result.m_debugDrawer->drawSphere(c,0.2f,btVector3(1,1,1));
157  }
158  btScalar dLambda = btScalar(0.);
159 
160  projectedLinearVelocity = relLinVel.dot(n);
161 
162 
163  //don't report time of impact for motion away from the contact normal (or causes minor penetration)
164  if ((projectedLinearVelocity+ maxAngularProjectedVelocity)<=SIMD_EPSILON)
165  return false;
166 
167  dLambda = dist / (projectedLinearVelocity+ maxAngularProjectedVelocity);
168 
169  lambda += dLambda;
170 
171  if (lambda > btScalar(1.) || lambda < btScalar(0.))
172  return false;
173 
174  //todo: next check with relative epsilon
175  if (lambda <= lastLambda)
176  {
177  return false;
178  //n.setValue(0,0,0);
179  //break;
180  }
181  lastLambda = lambda;
182 
183  //interpolate to next lambda
184  btTransform interpolatedTransA,interpolatedTransB,relativeTrans;
185 
186  btTransformUtil::integrateTransform(fromA,linVelA,angVelA,lambda,interpolatedTransA);
187  btTransformUtil::integrateTransform(fromB,linVelB,angVelB,lambda,interpolatedTransB);
188  relativeTrans = interpolatedTransB.inverseTimes(interpolatedTransA);
189 
190  if (result.m_debugDrawer)
191  {
192  result.m_debugDrawer->drawSphere(interpolatedTransA.getOrigin(),0.2f,btVector3(1,0,0));
193  }
194 
195  result.DebugDraw( lambda );
196 
197  btPointCollector pointCollector;
198  computeClosestPoints(interpolatedTransA,interpolatedTransB,pointCollector);
199 
200  if (pointCollector.m_hasResult)
201  {
202  dist = pointCollector.m_distance+result.m_allowedPenetration;
203  c = pointCollector.m_pointInWorld;
204  n = pointCollector.m_normalOnBInWorld;
205  } else
206  {
207  result.reportFailure(-1, numIter);
208  return false;
209  }
210 
211  numIter++;
212  if (numIter > MAX_ITERATIONS)
213  {
214  result.reportFailure(-2, numIter);
215  return false;
216  }
217  }
218 
219  result.m_fraction = lambda;
220  result.m_normal = n;
221  result.m_hitPoint = c;
222  return true;
223  }
224 
225  return false;
226 }
227 
#define SIMD_EPSILON
Definition: btScalar.h:521
void computeClosestPoints(const btTransform &transA, const btTransform &transB, struct btPointCollector &pointCollector)
btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:906
int getShapeType() const
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const =0
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:652
ConvexPenetrationDepthSolver provides an interface for penetration depth calculation.
const btVector3 & getPlaneNormal() const
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:235
btTransform inverseTimes(const btTransform &t) const
Return the inverse of this transform times the other transform.
Definition: btTransform.h:230
#define MAX_ITERATIONS
This maximum should not be necessary.
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:31
virtual void DebugDraw(btScalar fraction)
Definition: btConvexCast.h:40
btIDebugDraw * m_debugDrawer
Definition: btConvexCast.h:58
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
btSimplexSolverInterface * m_simplexSolver
btVector3 m_pointInWorld
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
Definition: btTransform.h:112
btScalar length() const
Return the length of the vector.
Definition: btVector3.h:263
btTransform inverse() const
Return the inverse of this transform.
Definition: btTransform.h:188
virtual btScalar getMargin() const =0
btConvexPenetrationDepthSolver * m_penetrationDepthSolver
virtual btScalar getAngularMotionDisc() const
getAngularMotionDisc returns the maximum radius needed for Conservative Advancement to handle time-of...
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
static void integrateTransform(const btTransform &curTrans, const btVector3 &linvel, const btVector3 &angvel, btScalar timeStep, btTransform &predictedTransform)
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
virtual void addContactPoint(const btVector3 &normalOnBInWorld, const btVector3 &pointInWorld, btScalar depth)
virtual bool calcTimeOfImpact(const btTransform &fromA, const btTransform &toA, const btTransform &fromB, const btTransform &toB, CastResult &result)
cast a convex against another convex object
btGjkPairDetector uses GJK to implement the btDiscreteCollisionDetectorInterface
btContinuousConvexCollision(const btConvexShape *shapeA, const btConvexShape *shapeB, btSimplexSolverInterface *simplexSolver, btConvexPenetrationDepthSolver *penetrationDepthSolver)
The btStaticPlaneShape simulates an infinite non-moving (static) collision plane. ...
static void calculateVelocity(const btTransform &transform0, const btTransform &transform1, btScalar timeStep, btVector3 &linVel, btVector3 &angVel)
virtual void drawSphere(btScalar radius, const btTransform &transform, const btVector3 &color)
Definition: btIDebugDraw.h:93
virtual void reportFailure(int errNo, int numIterations)
Definition: btConvexCast.h:42
btVector3 m_normalOnBInWorld
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
const btScalar & getPlaneConstant() const
const btStaticPlaneShape * m_planeShape