Bullet Collision Detection & Physics Library
btConvexPlaneCollisionAlgorithm.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 
17 
23 
24 //#include <stdio.h>
25 
26 btConvexPlaneCollisionAlgorithm::btConvexPlaneCollisionAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,const btCollisionObjectWrapper* col0Wrap,const btCollisionObjectWrapper* col1Wrap, bool isSwapped, int numPerturbationIterations,int minimumPointsPerturbationThreshold)
28 m_ownManifold(false),
29 m_manifoldPtr(mf),
30 m_isSwapped(isSwapped),
31 m_numPerturbationIterations(numPerturbationIterations),
32 m_minimumPointsPerturbationThreshold(minimumPointsPerturbationThreshold)
33 {
34  const btCollisionObjectWrapper* convexObjWrap = m_isSwapped? col1Wrap : col0Wrap;
35  const btCollisionObjectWrapper* planeObjWrap = m_isSwapped? col0Wrap : col1Wrap;
36 
37  if (!m_manifoldPtr && m_dispatcher->needsCollision(convexObjWrap->getCollisionObject(),planeObjWrap->getCollisionObject()))
38  {
40  m_ownManifold = true;
41  }
42 }
43 
44 
46 {
47  if (m_ownManifold)
48  {
49  if (m_manifoldPtr)
51  }
52 }
53 
54 void btConvexPlaneCollisionAlgorithm::collideSingleContact (const btQuaternion& perturbeRot, const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
55 {
56  const btCollisionObjectWrapper* convexObjWrap = m_isSwapped? body1Wrap : body0Wrap;
57  const btCollisionObjectWrapper* planeObjWrap = m_isSwapped? body0Wrap: body1Wrap;
58 
59  btConvexShape* convexShape = (btConvexShape*) convexObjWrap->getCollisionShape();
60  btStaticPlaneShape* planeShape = (btStaticPlaneShape*) planeObjWrap->getCollisionShape();
61 
62  bool hasCollision = false;
63  const btVector3& planeNormal = planeShape->getPlaneNormal();
64  const btScalar& planeConstant = planeShape->getPlaneConstant();
65 
66  btTransform convexWorldTransform = convexObjWrap->getWorldTransform();
67  btTransform convexInPlaneTrans;
68  convexInPlaneTrans= planeObjWrap->getWorldTransform().inverse() * convexWorldTransform;
69  //now perturbe the convex-world transform
70  convexWorldTransform.getBasis()*=btMatrix3x3(perturbeRot);
71  btTransform planeInConvex;
72  planeInConvex= convexWorldTransform.inverse() * planeObjWrap->getWorldTransform();
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 = planeObjWrap->getWorldTransform() * vtxInPlaneProjected;
81 
82  hasCollision = distance < m_manifoldPtr->getContactBreakingThreshold();
84  if (hasCollision)
85  {
87  btVector3 normalOnSurfaceB = planeObjWrap->getWorldTransform().getBasis() * planeNormal;
88  btVector3 pOnB = vtxInPlaneWorld;
89  resultOut->addContactPoint(normalOnSurfaceB,pOnB,distance);
90  }
91 }
92 
93 
95 {
96  (void)dispatchInfo;
97  if (!m_manifoldPtr)
98  return;
99 
100  const btCollisionObjectWrapper* convexObjWrap = m_isSwapped? body1Wrap : body0Wrap;
101  const btCollisionObjectWrapper* planeObjWrap = m_isSwapped? body0Wrap: body1Wrap;
102 
103  btConvexShape* convexShape = (btConvexShape*) convexObjWrap->getCollisionShape();
104  btStaticPlaneShape* planeShape = (btStaticPlaneShape*) planeObjWrap->getCollisionShape();
105 
106  bool hasCollision = false;
107  const btVector3& planeNormal = planeShape->getPlaneNormal();
108  const btScalar& planeConstant = planeShape->getPlaneConstant();
109  btTransform planeInConvex;
110  planeInConvex= convexObjWrap->getWorldTransform().inverse() * planeObjWrap->getWorldTransform();
111  btTransform convexInPlaneTrans;
112  convexInPlaneTrans= planeObjWrap->getWorldTransform().inverse() * convexObjWrap->getWorldTransform();
113 
114  btVector3 vtx = convexShape->localGetSupportingVertex(planeInConvex.getBasis()*-planeNormal);
115  btVector3 vtxInPlane = convexInPlaneTrans(vtx);
116  btScalar distance = (planeNormal.dot(vtxInPlane) - planeConstant);
117 
118  btVector3 vtxInPlaneProjected = vtxInPlane - distance*planeNormal;
119  btVector3 vtxInPlaneWorld = planeObjWrap->getWorldTransform() * vtxInPlaneProjected;
120 
121  hasCollision = distance < m_manifoldPtr->getContactBreakingThreshold();
123  if (hasCollision)
124  {
126  btVector3 normalOnSurfaceB = planeObjWrap->getWorldTransform().getBasis() * planeNormal;
127  btVector3 pOnB = vtxInPlaneWorld;
128  resultOut->addContactPoint(normalOnSurfaceB,pOnB,distance);
129  }
130 
131  //the perturbation algorithm doesn't work well with implicit surfaces such as spheres, cylinder and cones:
132  //they keep on rolling forever because of the additional off-center contact points
133  //so only enable the feature for polyhedral shapes (btBoxShape, btConvexHullShape etc)
135  {
136  btVector3 v0,v1;
137  btPlaneSpace1(planeNormal,v0,v1);
138  //now perform 'm_numPerturbationIterations' collision queries with the perturbated collision objects
139 
140  const btScalar angleLimit = 0.125f * SIMD_PI;
141  btScalar perturbeAngle;
142  btScalar radius = convexShape->getAngularMotionDisc();
143  perturbeAngle = gContactBreakingThreshold / radius;
144  if ( perturbeAngle > angleLimit )
145  perturbeAngle = angleLimit;
146 
147  btQuaternion perturbeRot(v0,perturbeAngle);
148  for (int i=0;i<m_numPerturbationIterations;i++)
149  {
150  btScalar iterationAngle = i*(SIMD_2_PI/btScalar(m_numPerturbationIterations));
151  btQuaternion rotq(planeNormal,iterationAngle);
152  collideSingleContact(rotq.inverse()*perturbeRot*rotq,body0Wrap,body1Wrap,dispatchInfo,resultOut);
153  }
154  }
155 
156  if (m_ownManifold)
157  {
159  {
160  resultOut->refreshContactPoints();
161  }
162  }
163 }
164 
166 {
167  (void)resultOut;
168  (void)dispatchInfo;
169  (void)col0;
170  (void)col1;
171 
172  //not yet
173  return btScalar(1.);
174 }
virtual void releaseManifold(btPersistentManifold *manifold)=0
const btPersistentManifold * getPersistentManifold() const
btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping...
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const =0
btScalar getContactBreakingThreshold() const
virtual void processCollision(const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut)
void setPersistentManifold(btPersistentManifold *manifoldPtr)
const btVector3 & getPlaneNormal() const
void btPlaneSpace1(const T &n, T &p, T &q)
Definition: btVector3.h:1271
btManifoldResult is a helper class to manage contact results.
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:235
virtual btScalar calculateTimeOfImpact(btCollisionObject *body0, btCollisionObject *body1, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut)
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:31
virtual void addContactPoint(const btVector3 &normalOnBInWorld, const btVector3 &pointInWorld, btScalar depth)
#define SIMD_PI
Definition: btScalar.h:434
#define SIMD_2_PI
Definition: btScalar.h:435
const btTransform & getWorldTransform() const
btCollisionObject can be used to manage collision detection objects.
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
Definition: btTransform.h:112
virtual btPersistentManifold * getNewManifold(const btCollisionObject *b0, const btCollisionObject *b1)=0
btTransform inverse() const
Return the inverse of this transform.
Definition: btTransform.h:188
btConvexPlaneCollisionAlgorithm(btPersistentManifold *mf, const btCollisionAlgorithmConstructionInfo &ci, const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, bool isSwapped, int numPerturbationIterations, int minimumPointsPerturbationThreshold)
const btCollisionShape * getCollisionShape() const
virtual btScalar getAngularMotionDisc() const
getAngularMotionDisc returns the maximus radius needed for Conservative Advancement to handle time-of...
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
void collideSingleContact(const btQuaternion &perturbeRot, const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut)
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
bool isPolyhedral() const
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition: btMatrix3x3.h:48
The btQuaternion implements quaternion to perform linear algebra rotations in combination with btMatr...
Definition: btQuaternion.h:48
The btStaticPlaneShape simulates an infinite non-moving (static) collision plane. ...
virtual bool needsCollision(const btCollisionObject *body0, const btCollisionObject *body1)=0
btCollisionAlgorithm is an collision interface that is compatible with the Broadphase and btDispatche...
btScalar gContactBreakingThreshold
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:266
const btScalar & getPlaneConstant() const
const btCollisionObject * getCollisionObject() const