Bullet Collision Detection & Physics Library
btConvexPlaneCollisionAlgorithm.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
17
23
24//#include <stdio.h>
25
28 m_ownManifold(false),
29 m_manifoldPtr(mf),
30 m_isSwapped(isSwapped),
31 m_numPerturbationIterations(numPerturbationIterations),
32 m_minimumPointsPerturbationThreshold(minimumPointsPerturbationThreshold)
33{
36
37 if (!m_manifoldPtr && m_dispatcher->needsCollision(convexObjWrap->getCollisionObject(), planeObjWrap->getCollisionObject()))
38 {
39 m_manifoldPtr = m_dispatcher->getNewManifold(convexObjWrap->getCollisionObject(), planeObjWrap->getCollisionObject());
40 m_ownManifold = true;
41 }
42}
43
45{
46 if (m_ownManifold)
47 {
48 if (m_manifoldPtr)
50 }
51}
52
54{
55 const btCollisionObjectWrapper* convexObjWrap = m_isSwapped ? body1Wrap : body0Wrap;
56 const btCollisionObjectWrapper* planeObjWrap = m_isSwapped ? body0Wrap : body1Wrap;
57
58 btConvexShape* convexShape = (btConvexShape*)convexObjWrap->getCollisionShape();
60
61 bool hasCollision = false;
62 const btVector3& planeNormal = planeShape->getPlaneNormal();
63 const btScalar& planeConstant = planeShape->getPlaneConstant();
64
65 btTransform convexWorldTransform = convexObjWrap->getWorldTransform();
68 //now perturbe the convex-world transform
71 planeInConvex = convexWorldTransform.inverse() * planeObjWrap->getWorldTransform();
72
73 btVector3 vtx = convexShape->localGetSupportingVertex(planeInConvex.getBasis() * -planeNormal);
74
76 btScalar distance = (planeNormal.dot(vtxInPlane) - planeConstant);
77
80
82 resultOut->setPersistentManifold(m_manifoldPtr);
83 if (hasCollision)
84 {
86 btVector3 normalOnSurfaceB = planeObjWrap->getWorldTransform().getBasis() * planeNormal;
88 resultOut->addContactPoint(normalOnSurfaceB, pOnB, distance);
89 }
90}
91
93{
95 if (!m_manifoldPtr)
96 return;
97
98 const btCollisionObjectWrapper* convexObjWrap = m_isSwapped ? body1Wrap : body0Wrap;
99 const btCollisionObjectWrapper* planeObjWrap = m_isSwapped ? body0Wrap : body1Wrap;
100
101 btConvexShape* convexShape = (btConvexShape*)convexObjWrap->getCollisionShape();
103
104 bool hasCollision = false;
105 const btVector3& planeNormal = planeShape->getPlaneNormal();
106 const btScalar& planeConstant = planeShape->getPlaneConstant();
108 planeInConvex = convexObjWrap->getWorldTransform().inverse() * planeObjWrap->getWorldTransform();
110 convexInPlaneTrans = planeObjWrap->getWorldTransform().inverse() * convexObjWrap->getWorldTransform();
111
112 btVector3 vtx = convexShape->localGetSupportingVertex(planeInConvex.getBasis() * -planeNormal);
114 btScalar distance = (planeNormal.dot(vtxInPlane) - planeConstant);
115
118
119 hasCollision = distance < m_manifoldPtr->getContactBreakingThreshold()+ resultOut->m_closestPointDistanceThreshold;
120 resultOut->setPersistentManifold(m_manifoldPtr);
121 if (hasCollision)
122 {
124 btVector3 normalOnSurfaceB = planeObjWrap->getWorldTransform().getBasis() * planeNormal;
126 resultOut->addContactPoint(normalOnSurfaceB, pOnB, distance);
127 }
128
129 //the perturbation algorithm doesn't work well with implicit surfaces such as spheres, cylinder and cones:
130 //they keep on rolling forever because of the additional off-center contact points
131 //so only enable the feature for polyhedral shapes (btBoxShape, btConvexHullShape etc)
132 if (convexShape->isPolyhedral() && resultOut->getPersistentManifold()->getNumContacts() < m_minimumPointsPerturbationThreshold)
133 {
134 btVector3 v0, v1;
135 btPlaneSpace1(planeNormal, v0, v1);
136 //now perform 'm_numPerturbationIterations' collision queries with the perturbated collision objects
137
138 const btScalar angleLimit = 0.125f * SIMD_PI;
140 btScalar radius = convexShape->getAngularMotionDisc();
144
146 for (int i = 0; i < m_numPerturbationIterations; i++)
147 {
150 collideSingleContact(rotq.inverse() * perturbeRot * rotq, body0Wrap, body1Wrap, dispatchInfo, resultOut);
151 }
152 }
153
154 if (m_ownManifold)
155 {
157 {
158 resultOut->refreshContactPoints();
159 }
160 }
161}
162
164{
167 (void)col0;
168 (void)col1;
169
170 //not yet
171 return btScalar(1.);
172}
btScalar gContactBreakingThreshold
const T & btMax(const T &a, const T &b)
Definition btMinMax.h:27
#define SIMD_PI
Definition btScalar.h:526
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:314
#define SIMD_2_PI
Definition btScalar.h:527
void btPlaneSpace1(const T &n, T &p, T &q)
Definition btVector3.h:1251
btCollisionAlgorithm is an collision interface that is compatible with the Broadphase and btDispatche...
btCollisionObject can be used to manage collision detection objects.
btConvexPlaneCollisionAlgorithm(btPersistentManifold *mf, const btCollisionAlgorithmConstructionInfo &ci, const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, bool isSwapped, int numPerturbationIterations, int minimumPointsPerturbationThreshold)
void collideSingleContact(const btQuaternion &perturbeRot, const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut)
virtual void processCollision(const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut)
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...
virtual void releaseManifold(btPersistentManifold *manifold)=0
virtual bool needsCollision(const btCollisionObject *body0, const btCollisionObject *body1)=0
virtual btPersistentManifold * getNewManifold(const btCollisionObject *b0, const btCollisionObject *b1)=0
btManifoldResult is a helper class to manage contact results.
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition btMatrix3x3.h:50
btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping...
btScalar getContactBreakingThreshold() const
The btQuaternion implements quaternion to perform linear algebra rotations in combination with btMatr...
The btStaticPlaneShape simulates an infinite non-moving (static) collision plane.
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.
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
btVector3 can be used to represent 3D points and vectors.
Definition btVector3.h:82