Bullet Collision Detection & Physics Library: btGjkConvexCast.cpp Source File

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