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

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btConvex2dConvex2dAlgorithm.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 #include "btConvex2dConvex2dAlgorithm.h"
17
18 //#include <stdio.h>
19 #include "BulletCollision/NarrowPhaseCollision/btDiscreteCollisionDetectorInterface.h"
20 #include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h"
21 #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
22 #include "BulletCollision/CollisionShapes/btConvexShape.h"
23 #include "BulletCollision/CollisionShapes/btCapsuleShape.h"
24
25
26 #include "BulletCollision/NarrowPhaseCollision/btGjkPairDetector.h"
27 #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
28 #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
29 #include "BulletCollision/CollisionShapes/btBoxShape.h"
30 #include "BulletCollision/CollisionDispatch/btManifoldResult.h"
31
32 #include "BulletCollision/NarrowPhaseCollision/btConvexPenetrationDepthSolver.h"
33 #include "BulletCollision/NarrowPhaseCollision/btContinuousConvexCollision.h"
34 #include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
35 #include "BulletCollision/NarrowPhaseCollision/btGjkConvexCast.h"
36
37
38
39 #include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
40 #include "BulletCollision/CollisionShapes/btSphereShape.h"
41
42 #include "BulletCollision/NarrowPhaseCollision/btMinkowskiPenetrationDepthSolver.h"
43
44 #include "BulletCollision/NarrowPhaseCollision/btGjkEpa2.h"
45 #include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h"
46 #include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
47
48 btConvex2dConvex2dAlgorithm::CreateFunc::CreateFunc(btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver)
49 {
50 m_numPerturbationIterations = 0;
51 m_minimumPointsPerturbationThreshold = 3;
52 m_simplexSolver = simplexSolver;
53 m_pdSolver = pdSolver;
54 }
55
56 btConvex2dConvex2dAlgorithm::CreateFunc::~CreateFunc()
57 {
58 }
59
60 btConvex2dConvex2dAlgorithm::btConvex2dConvex2dAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver,int numPerturbationIterations, int minimumPointsPerturbationThreshold)
61 : btActivatingCollisionAlgorithm(ci,body0Wrap,body1Wrap),
62 m_simplexSolver(simplexSolver),
63 m_pdSolver(pdSolver),
64 m_ownManifold (false),
65 m_manifoldPtr(mf),
66 m_lowLevelOfDetail(false),
67 m_numPerturbationIterations(numPerturbationIterations),
68 m_minimumPointsPerturbationThreshold(minimumPointsPerturbationThreshold)
69 {
70 (void)body0Wrap;
71 (void)body1Wrap;
72 }
73
74
75
76
77 btConvex2dConvex2dAlgorithm::~btConvex2dConvex2dAlgorithm()
78 {
79 if (m_ownManifold)
80 {
81 if (m_manifoldPtr)
82 m_dispatcher->releaseManifold(m_manifoldPtr);
83 }
84 }
85
86 void btConvex2dConvex2dAlgorithm ::setLowLevelOfDetail(bool useLowLevel)
87 {
88 m_lowLevelOfDetail = useLowLevel;
89 }
90
91
92
93 extern btScalar gContactBreakingThreshold;
94
95
96 //
97 // Convex-Convex collision algorithm
98 //
99 void btConvex2dConvex2dAlgorithm ::processCollision (const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
100 {
101
102 if (!m_manifoldPtr)
103 {
104 //swapped?
105 m_manifoldPtr = m_dispatcher->getNewManifold(body0Wrap->getCollisionObject(),body1Wrap->getCollisionObject());
106 m_ownManifold = true;
107 }
108 resultOut->setPersistentManifold(m_manifoldPtr);
109
110 //comment-out next line to test multi-contact generation
111 //resultOut->getPersistentManifold()->clearManifold();
112
113
114 const btConvexShape* min0 = static_cast<const btConvexShape*>(body0Wrap->getCollisionShape());
115 const btConvexShape* min1 = static_cast<const btConvexShape*>(body1Wrap->getCollisionShape());
116
117 btVector3 normalOnB;
118 btVector3 pointOnBWorld;
119
120 {
121
122
123 btGjkPairDetector::ClosestPointInput input;
124
125 btGjkPairDetector gjkPairDetector(min0,min1,m_simplexSolver,m_pdSolver);
126 //TODO: if (dispatchInfo.m_useContinuous)
127 gjkPairDetector.setMinkowskiA(min0);
128 gjkPairDetector.setMinkowskiB(min1);
129
130 {
131 input.m_maximumDistanceSquared = min0->getMargin() + min1->getMargin() + m_manifoldPtr->getContactBreakingThreshold();
132 input.m_maximumDistanceSquared*= input.m_maximumDistanceSquared;
133 }
134
135 input.m_transformA = body0Wrap->getWorldTransform();
136 input.m_transformB = body1Wrap->getWorldTransform();
137
138 gjkPairDetector.getClosestPoints(input,*resultOut,dispatchInfo.m_debugDraw);
139
140 btVector3 v0,v1;
141 btVector3 sepNormalWorldSpace;
142
143 }
144
145 if (m_ownManifold)
146 {
147 resultOut->refreshContactPoints();
148 }
149
150 }
151
152
153
154
155 btScalar btConvex2dConvex2dAlgorithm::calculateTimeOfImpact(btCollisionObject* col0,btCollisionObject* col1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
156 {
157 (void)resultOut;
158 (void)dispatchInfo;
160
163 btScalar resultFraction = btScalar(1.);
164
165
166 btScalar squareMot0 = (col0->getInterpolationWorldTransform().getOrigin() - col0->getWorldTransform().getOrigin()).length2();
167 btScalar squareMot1 = (col1->getInterpolationWorldTransform().getOrigin() - col1->getWorldTransform().getOrigin()).length2();
168
169 if (squareMot0 < col0->getCcdSquareMotionThreshold() &&
170 squareMot1 < col1->getCcdSquareMotionThreshold())
171 return resultFraction;
172
173
174 //An adhoc way of testing the Continuous Collision Detection algorithms
175 //One object is approximated as a sphere, to simplify things
176 //Starting in penetration should report no time of impact
177 //For proper CCD, better accuracy and handling of 'allowed' penetration should be added
178 //also the mainloop of the physics should have a kind of toi queue (something like Brian Mirtich's application of Timewarp for Rigidbodies)
179
180
182 {
183 btConvexShape* convex0 = static_cast<btConvexShape*>(col0->getCollisionShape());
184
185 btSphereShape sphere1(col1->getCcdSweptSphereRadius()); //todo: allow non-zero sphere sizes, for better approximation
186 btConvexCast::CastResult result;
187 btVoronoiSimplexSolver voronoiSimplex;
188 //SubsimplexConvexCast ccd0(&sphere,min0,&voronoiSimplex);
190 btGjkConvexCast ccd1( convex0 ,&sphere1,&voronoiSimplex);
191 //ContinuousConvexCollision ccd(min0,min1,&voronoiSimplex,0);
192 if (ccd1.calcTimeOfImpact(col0->getWorldTransform(),col0->getInterpolationWorldTransform(),
193 col1->getWorldTransform(),col1->getInterpolationWorldTransform(),result))
194 {
195
196 //store result.m_fraction in both bodies
197
198 if (col0->getHitFraction()> result.m_fraction)
199 col0->setHitFraction( result.m_fraction );
200
201 if (col1->getHitFraction() > result.m_fraction)
202 col1->setHitFraction( result.m_fraction);
203
204 if (resultFraction > result.m_fraction)
205 resultFraction = result.m_fraction;
206
207 }
208
209
210
211
212 }
213
215 {
216 btConvexShape* convex1 = static_cast<btConvexShape*>(col1->getCollisionShape());
217
218 btSphereShape sphere0(col0->getCcdSweptSphereRadius()); //todo: allow non-zero sphere sizes, for better approximation
219 btConvexCast::CastResult result;
220 btVoronoiSimplexSolver voronoiSimplex;
221 //SubsimplexConvexCast ccd0(&sphere,min0,&voronoiSimplex);
223 btGjkConvexCast ccd1(&sphere0,convex1,&voronoiSimplex);
224 //ContinuousConvexCollision ccd(min0,min1,&voronoiSimplex,0);
225 if (ccd1.calcTimeOfImpact(col0->getWorldTransform(),col0->getInterpolationWorldTransform(),
226 col1->getWorldTransform(),col1->getInterpolationWorldTransform(),result))
227 {
228
229 //store result.m_fraction in both bodies
230
231 if (col0->getHitFraction() > result.m_fraction)
232 col0->setHitFraction( result.m_fraction);
233
234 if (col1->getHitFraction() > result.m_fraction)
235 col1->setHitFraction( result.m_fraction);
236
237 if (resultFraction > result.m_fraction)
238 resultFraction = result.m_fraction;
239
240 }
241 }
242
243 return resultFraction;
244
245 }
246
btConvex2dConvex2dAlgorithm::CreateFunc::m_minimumPointsPerturbationThreshold
int m_minimumPointsPerturbationThreshold
Definition: btConvex2dConvex2dAlgorithm.h:79
btMinkowskiPenetrationDepthSolver.h
btDispatcher::releaseManifold
virtual void releaseManifold(btPersistentManifold *manifold)=0
btPersistentManifold
btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping...
Definition: btPersistentManifold.h:54
btConvex2dConvex2dAlgorithm::m_lowLevelOfDetail
bool m_lowLevelOfDetail
Definition: btConvex2dConvex2dAlgorithm.h:41
btCollisionObject::getCcdSweptSphereRadius
btScalar getCcdSweptSphereRadius() const
Swept sphere radius (0.0 by default), see btConvexConvexAlgorithm::
Definition: btCollisionObject.h:440
btCapsuleShape.h
btPersistentManifold::getContactBreakingThreshold
btScalar getContactBreakingThreshold() const
Definition: btPersistentManifold.cpp:240
btConvexPenetrationDepthSolver
ConvexPenetrationDepthSolver provides an interface for penetration depth calculation.
Definition: btConvexPenetrationDepthSolver.h:26
btManifoldResult.h
btGjkPairDetector::setMinkowskiB
void setMinkowskiB(const btConvexShape *minkB)
Definition: btGjkPairDetector.h:71
btBoxShape.h
btManifoldResult::setPersistentManifold
void setPersistentManifold(btPersistentManifold *manifoldPtr)
Definition: btManifoldResult.h:70
btConvex2dConvex2dAlgorithm::CreateFunc::~CreateFunc
virtual ~CreateFunc()
Definition: btConvex2dConvex2dAlgorithm.cpp:56
btActivatingCollisionAlgorithm
This class is not enabled yet (work-in-progress) to more aggressively activate objects.
Definition: btActivatingCollisionAlgorithm.h:22
btCollisionObject::setHitFraction
void setHitFraction(btScalar hitFraction)
Definition: btCollisionObject.h:423
btManifoldResult::refreshContactPoints
void refreshContactPoints()
Definition: btManifoldResult.h:99
btGjkPairDetector::setMinkowskiA
void setMinkowskiA(const btConvexShape *minkA)
Definition: btGjkPairDetector.h:66
btConvex2dConvex2dAlgorithm::calculateTimeOfImpact
virtual btScalar calculateTimeOfImpact(btCollisionObject *body0, btCollisionObject *body1, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut)
Definition: btConvex2dConvex2dAlgorithm.cpp:155
btSphereShape
The btSphereShape implements an implicit sphere, centered around a local origin with radius...
Definition: btSphereShape.h:22
btManifoldResult
btManifoldResult is a helper class to manage contact results.
Definition: btManifoldResult.h:39
btDispatcherInfo::m_debugDraw
class btIDebugDraw * m_debugDraw
Definition: btDispatcher.h:58
btDiscreteCollisionDetectorInterface::ClosestPointInput::m_transformA
btTransform m_transformA
Definition: btDiscreteCollisionDetectorInterface.h:49
btConvex2dConvex2dAlgorithm::m_manifoldPtr
btPersistentManifold * m_manifoldPtr
Definition: btConvex2dConvex2dAlgorithm.h:40
btBroadphaseInterface.h
btConvexShape.h
btCollisionAlgorithm::m_dispatcher
btDispatcher * m_dispatcher
Definition: btCollisionAlgorithm.h:60
btGjkPairDetector.h
btDiscreteCollisionDetectorInterface::ClosestPointInput::m_transformB
btTransform m_transformB
Definition: btDiscreteCollisionDetectorInterface.h:50
btCollisionObject::getCcdSquareMotionThreshold
btScalar getCcdSquareMotionThreshold() const
Definition: btCollisionObject.h:456
btConvexShape
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:31
btCollisionObject::getInterpolationWorldTransform
const btTransform & getInterpolationWorldTransform() const
Definition: btCollisionObject.h:360
btGjkConvexCast
GjkConvexCast performs a raycast on a convex object using support mapping.
Definition: btGjkConvexCast.h:30
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
btCollisionObject::getWorldTransform
btTransform & getWorldTransform()
Definition: btCollisionObject.h:327
btDiscreteCollisionDetectorInterface::ClosestPointInput::m_maximumDistanceSquared
btScalar m_maximumDistanceSquared
Definition: btDiscreteCollisionDetectorInterface.h:51
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
btVoronoiSimplexSolver
btVoronoiSimplexSolver is an implementation of the closest point distance algorithm from a 1-4 points...
Definition: btVoronoiSimplexSolver.h:95
btGjkConvexCast.h
btConvex2dConvex2dAlgorithm::CreateFunc::m_numPerturbationIterations
int m_numPerturbationIterations
Definition: btConvex2dConvex2dAlgorithm.h:78
btCollisionObjectWrapper::getWorldTransform
const btTransform & getWorldTransform() const
Definition: btCollisionObjectWrapper.h:38
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition: btCollisionObject.h:49
btBroadphaseProxy.h
btDispatcher::getNewManifold
virtual btPersistentManifold * getNewManifold(const btCollisionObject *b0, const btCollisionObject *b1)=0
btContinuousConvexCollision.h
btCollisionObjectWrapper.h
btGjkEpa2.h
btConvexShape::getMargin
virtual btScalar getMargin() const =0
btCollisionObjectWrapper::getCollisionShape
const btCollisionShape * getCollisionShape() const
Definition: btCollisionObjectWrapper.h:40
btCollisionObject::getHitFraction
btScalar getHitFraction() const
Definition: btCollisionObject.h:418
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btConvex2dConvex2dAlgorithm::m_numPerturbationIterations
int m_numPerturbationIterations
Definition: btConvex2dConvex2dAlgorithm.h:43
btConvex2dConvex2dAlgorithm::m_simplexSolver
btSimplexSolverInterface * m_simplexSolver
Definition: btConvex2dConvex2dAlgorithm.h:35
btCollisionDispatcher.h
btSubSimplexConvexCast.h
btConvex2dConvex2dAlgorithm::CreateFunc::CreateFunc
CreateFunc(btSimplexSolverInterface *simplexSolver, btConvexPenetrationDepthSolver *pdSolver)
Definition: btConvex2dConvex2dAlgorithm.cpp:48
btConvexPenetrationDepthSolver.h
btCollisionObject.h
btGjkPairDetector
btGjkPairDetector uses GJK to implement the btDiscreteCollisionDetectorInterface
Definition: btGjkPairDetector.h:30
btVoronoiSimplexSolver.h
btConvex2dConvex2dAlgorithm::btConvex2dConvex2dAlgorithm
btConvex2dConvex2dAlgorithm(btPersistentManifold *mf, const btCollisionAlgorithmConstructionInfo &ci, const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, btSimplexSolverInterface *simplexSolver, btConvexPenetrationDepthSolver *pdSolver, int numPerturbationIterations, int minimumPointsPerturbationThreshold)
Definition: btConvex2dConvex2dAlgorithm.cpp:60
btConvex2dConvex2dAlgorithm::processCollision
virtual void processCollision(const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut)
Definition: btConvex2dConvex2dAlgorithm.cpp:99
btConvex2dConvex2dAlgorithm::~btConvex2dConvex2dAlgorithm
virtual ~btConvex2dConvex2dAlgorithm()
Definition: btConvex2dConvex2dAlgorithm.cpp:77
btConvex2dConvex2dAlgorithm::CreateFunc::m_simplexSolver
btSimplexSolverInterface * m_simplexSolver
Definition: btConvex2dConvex2dAlgorithm.h:77
btConvex2dConvex2dAlgorithm::CreateFunc::m_pdSolver
btConvexPenetrationDepthSolver * m_pdSolver
Definition: btConvex2dConvex2dAlgorithm.h:76
btDispatcherInfo
Definition: btDispatcher.h:30
btDiscreteCollisionDetectorInterface::ClosestPointInput
Definition: btDiscreteCollisionDetectorInterface.h:42
btCollisionAlgorithmConstructionInfo
Definition: btCollisionAlgorithm.h:32
btSphereShape.h
btCollisionObject::getCollisionShape
const btCollisionShape * getCollisionShape() const
Definition: btCollisionObject.h:215
gContactBreakingThreshold
btScalar gContactBreakingThreshold
Definition: btPersistentManifold.cpp:21
btConvex2dConvex2dAlgorithm::setLowLevelOfDetail
void setLowLevelOfDetail(bool useLowLevel)
Definition: btConvex2dConvex2dAlgorithm.cpp:86
btCollisionObjectWrapper
Definition: btCollisionObjectWrapper.h:17
btDiscreteCollisionDetectorInterface.h
btConvex2dConvex2dAlgorithm::m_minimumPointsPerturbationThreshold
int m_minimumPointsPerturbationThreshold
Definition: btConvex2dConvex2dAlgorithm.h:44
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:278
btConvex2dConvex2dAlgorithm.h
btConvex2dConvex2dAlgorithm::m_pdSolver
btConvexPenetrationDepthSolver * m_pdSolver
Definition: btConvex2dConvex2dAlgorithm.h:36
btGjkPairDetector::getClosestPoints
virtual void getClosestPoints(const ClosestPointInput &input, Result &output, class btIDebugDraw *debugDraw, bool swapResults=false)
Definition: btGjkPairDetector.cpp:73
btCollisionObjectWrapper::getCollisionObject
const btCollisionObject * getCollisionObject() const
Definition: btCollisionObjectWrapper.h:39
btGjkEpaPenetrationDepthSolver.h
btConvex2dConvex2dAlgorithm::m_ownManifold
bool m_ownManifold
Definition: btConvex2dConvex2dAlgorithm.h:39