Bullet Collision Detection & Physics Library
btContinuousConvexCollision.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
21
22#include "btGjkPairDetector.h"
23#include "btPointCollector.h"
25
27 : m_simplexSolver(simplexSolver),
28 m_penetrationDepthSolver(penetrationDepthSolver),
29 m_convexA(convexA),
30 m_convexB1(convexB),
31 m_planeShape(0)
32{
33}
34
36 : m_simplexSolver(0),
37 m_penetrationDepthSolver(0),
38 m_convexA(convexA),
39 m_convexB1(0),
40 m_planeShape(plane)
41{
42}
43
46#define MAX_ITERATIONS 64
47
49{
50 if (m_convexB1)
51 {
52 m_simplexSolver->reset();
56 input.m_transformB = transB;
57 gjk.getClosestPoints(input, pointCollector, 0);
58 }
59 else
60 {
61 //convex versus plane
64
65 const btVector3& planeNormal = planeShape->getPlaneNormal();
66 const btScalar& planeConstant = planeShape->getPlaneConstant();
67
73
74 btVector3 vtx = convexShape->localGetSupportingVertex(planeInConvex.getBasis() * -planeNormal);
75
77 btScalar distance = (planeNormal.dot(vtxInPlane) - planeConstant);
78
82
83 pointCollector.addContactPoint(
86 distance);
87 }
88}
89
91 const btTransform& fromA,
92 const btTransform& toA,
93 const btTransform& fromB,
94 const btTransform& toB,
96{
101
104
107
109
111 return false;
112
114
115 btVector3 n;
116 n.setValue(btScalar(0.), btScalar(0.), btScalar(0.));
117 bool hasResult = false;
118 btVector3 c;
119
121 //btScalar epsilon = btScalar(0.001);
122
123 int numIter = 0;
124 //first solution, using GJK
125
126 btScalar radius = 0.001f;
127 // result.drawCoordSystem(sphereTr);
128
130
131 {
133
134 hasResult = pointCollector1.m_hasResult;
135 c = pointCollector1.m_pointInWorld;
136 }
137
138 if (hasResult)
139 {
140 btScalar dist;
141 dist = pointCollector1.m_distance + result.m_allowedPenetration;
142 n = pointCollector1.m_normalOnBInWorld;
145 return false;
146
147 //not close enough
148 while (dist > radius)
149 {
150 if (result.m_debugDrawer)
151 {
152 result.m_debugDrawer->drawSphere(c, 0.2f, btVector3(1, 1, 1));
153 }
155
157
158 //don't report time of impact for motion away from the contact normal (or causes minor penetration)
160 return false;
161
163
164 lambda += dLambda;
165
166 if (lambda > btScalar(1.) || lambda < btScalar(0.))
167 return false;
168
169 //todo: next check with relative epsilon
170 if (lambda <= lastLambda)
171 {
172 return false;
173 //n.setValue(0,0,0);
174 //break;
175 }
177
178 //interpolate to next lambda
180
184
185 if (result.m_debugDrawer)
186 {
187 result.m_debugDrawer->drawSphere(interpolatedTransA.getOrigin(), 0.2f, btVector3(1, 0, 0));
188 }
189
190 result.DebugDraw(lambda);
191
194
195 if (pointCollector.m_hasResult)
196 {
197 dist = pointCollector.m_distance + result.m_allowedPenetration;
198 c = pointCollector.m_pointInWorld;
199 n = pointCollector.m_normalOnBInWorld;
200 }
201 else
202 {
203 result.reportFailure(-1, numIter);
204 return false;
205 }
206
207 numIter++;
209 {
210 result.reportFailure(-2, numIter);
211 return false;
212 }
213 }
214
215 result.m_fraction = lambda;
216 result.m_normal = n;
217 result.m_hitPoint = c;
218 return true;
219 }
220
221 return false;
222}
#define MAX_ITERATIONS
This maximum should not be necessary.
const T & btMax(const T &a, const T &b)
Definition btMinMax.h:27
btScalar length(const btQuaternion &q)
Return the length of a quaternion.
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:314
#define SIMD_EPSILON
Definition btScalar.h:543
#define btSimplexSolverInterface
int getShapeType() const
virtual btScalar getAngularMotionDisc() const
getAngularMotionDisc returns the maximum radius needed for Conservative Advancement to handle time-of...
btContinuousConvexCollision(const btConvexShape *shapeA, const btConvexShape *shapeB, btSimplexSolverInterface *simplexSolver, btConvexPenetrationDepthSolver *penetrationDepthSolver)
void computeClosestPoints(const btTransform &transA, const btTransform &transB, struct btPointCollector &pointCollector)
const btStaticPlaneShape * m_planeShape
btConvexPenetrationDepthSolver * m_penetrationDepthSolver
btSimplexSolverInterface * m_simplexSolver
virtual bool calcTimeOfImpact(const btTransform &fromA, const btTransform &toA, const btTransform &fromB, const btTransform &toB, CastResult &result)
cast a convex against another convex object
ConvexPenetrationDepthSolver provides an interface for penetration depth calculation.
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
virtual btScalar getMargin() const =0
btGjkPairDetector uses GJK to implement the btDiscreteCollisionDetectorInterface
The btStaticPlaneShape simulates an infinite non-moving (static) collision plane.
static void integrateTransform(const btTransform &curTrans, const btVector3 &linvel, const btVector3 &angvel, btScalar timeStep, btTransform &predictedTransform)
static void calculateVelocity(const btTransform &transform0, const btTransform &transform1, btScalar timeStep, btVector3 &linVel, btVector3 &angVel)
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition btTransform.h:30
btTransform inverse() const
Return the inverse of this transform.
btVector3 can be used to represent 3D points and vectors.
Definition btVector3.h:82
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition btVector3.h:229
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition btVector3.h:640
RayResult stores the closest result alternatively, add a callback method to decide about closest/all ...