2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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:
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.
16 #include "btConvexPlaneCollisionAlgorithm.h"
18 #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
19 #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
20 #include "BulletCollision/CollisionShapes/btConvexShape.h"
21 #include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
25 btConvexPlaneCollisionAlgorithm::btConvexPlaneCollisionAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* col0,btCollisionObject* col1, bool isSwapped, int numPerturbationIterations,int minimumPointsPerturbationThreshold)
26 : btCollisionAlgorithm(ci),
29 m_isSwapped(isSwapped),
30 m_numPerturbationIterations(numPerturbationIterations),
31 m_minimumPointsPerturbationThreshold(minimumPointsPerturbationThreshold)
33 btCollisionObject* convexObj = m_isSwapped? col1 : col0;
34 btCollisionObject* planeObj = m_isSwapped? col0 : col1;
36 if (!m_manifoldPtr && m_dispatcher->needsCollision(convexObj,planeObj))
38 m_manifoldPtr = m_dispatcher->getNewManifold(convexObj,planeObj);
44 btConvexPlaneCollisionAlgorithm::~btConvexPlaneCollisionAlgorithm()
49 m_dispatcher->releaseManifold(m_manifoldPtr);
53 void btConvexPlaneCollisionAlgorithm::collideSingleContact (const btQuaternion& perturbeRot, btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
55 btCollisionObject* convexObj = m_isSwapped? body1 : body0;
56 btCollisionObject* planeObj = m_isSwapped? body0: body1;
58 btConvexShape* convexShape = (btConvexShape*) convexObj->getCollisionShape();
59 btStaticPlaneShape* planeShape = (btStaticPlaneShape*) planeObj->getCollisionShape();
61 bool hasCollision = false;
62 const btVector3& planeNormal = planeShape->getPlaneNormal();
63 const btScalar& planeConstant = planeShape->getPlaneConstant();
65 btTransform convexWorldTransform = convexObj->getWorldTransform();
66 btTransform convexInPlaneTrans;
67 convexInPlaneTrans= planeObj->getWorldTransform().inverse() * convexWorldTransform;
68 //now perturbe the convex-world transform
69 convexWorldTransform.getBasis()*=btMatrix3x3(perturbeRot);
70 btTransform planeInConvex;
71 planeInConvex= convexWorldTransform.inverse() * planeObj->getWorldTransform();
73 btVector3 vtx = convexShape->localGetSupportingVertex(planeInConvex.getBasis()*-planeNormal);
75 btVector3 vtxInPlane = convexInPlaneTrans(vtx);
76 btScalar distance = (planeNormal.dot(vtxInPlane) - planeConstant);
78 btVector3 vtxInPlaneProjected = vtxInPlane - distance*planeNormal;
79 btVector3 vtxInPlaneWorld = planeObj->getWorldTransform() * vtxInPlaneProjected;
81 hasCollision = distance < m_manifoldPtr->getContactBreakingThreshold();
82 resultOut->setPersistentManifold(m_manifoldPtr);
85 /// report a contact. internally this will be kept persistent, and contact reduction is done
86 btVector3 normalOnSurfaceB = planeObj->getWorldTransform().getBasis() * planeNormal;
87 btVector3 pOnB = vtxInPlaneWorld;
88 resultOut->addContactPoint(normalOnSurfaceB,pOnB,distance);
93 void btConvexPlaneCollisionAlgorithm::processCollision (btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
99 btCollisionObject* convexObj = m_isSwapped? body1 : body0;
100 btCollisionObject* planeObj = m_isSwapped? body0: body1;
102 btConvexShape* convexShape = (btConvexShape*) convexObj->getCollisionShape();
103 btStaticPlaneShape* planeShape = (btStaticPlaneShape*) planeObj->getCollisionShape();
105 bool hasCollision = false;
106 const btVector3& planeNormal = planeShape->getPlaneNormal();
107 const btScalar& planeConstant = planeShape->getPlaneConstant();
108 btTransform planeInConvex;
109 planeInConvex= convexObj->getWorldTransform().inverse() * planeObj->getWorldTransform();
110 btTransform convexInPlaneTrans;
111 convexInPlaneTrans= planeObj->getWorldTransform().inverse() * convexObj->getWorldTransform();
113 btVector3 vtx = convexShape->localGetSupportingVertex(planeInConvex.getBasis()*-planeNormal);
114 btVector3 vtxInPlane = convexInPlaneTrans(vtx);
115 btScalar distance = (planeNormal.dot(vtxInPlane) - planeConstant);
117 btVector3 vtxInPlaneProjected = vtxInPlane - distance*planeNormal;
118 btVector3 vtxInPlaneWorld = planeObj->getWorldTransform() * vtxInPlaneProjected;
120 hasCollision = distance < m_manifoldPtr->getContactBreakingThreshold();
121 resultOut->setPersistentManifold(m_manifoldPtr);
124 /// report a contact. internally this will be kept persistent, and contact reduction is done
125 btVector3 normalOnSurfaceB = planeObj->getWorldTransform().getBasis() * planeNormal;
126 btVector3 pOnB = vtxInPlaneWorld;
127 resultOut->addContactPoint(normalOnSurfaceB,pOnB,distance);
130 //the perturbation algorithm doesn't work well with implicit surfaces such as spheres, cylinder and cones:
131 //they keep on rolling forever because of the additional off-center contact points
132 //so only enable the feature for polyhedral shapes (btBoxShape, btConvexHullShape etc)
133 if (convexShape->isPolyhedral() && resultOut->getPersistentManifold()->getNumContacts()<m_minimumPointsPerturbationThreshold)
136 btPlaneSpace1(planeNormal,v0,v1);
137 //now perform 'm_numPerturbationIterations' collision queries with the perturbated collision objects
139 const btScalar angleLimit = 0.125f * SIMD_PI;
140 btScalar perturbeAngle;
141 btScalar radius = convexShape->getAngularMotionDisc();
142 perturbeAngle = gContactBreakingThreshold / radius;
143 if ( perturbeAngle > angleLimit )
144 perturbeAngle = angleLimit;
146 btQuaternion perturbeRot(v0,perturbeAngle);
147 for (int i=0;i<m_numPerturbationIterations;i++)
149 btScalar iterationAngle = i*(SIMD_2_PI/btScalar(m_numPerturbationIterations));
150 btQuaternion rotq(planeNormal,iterationAngle);
151 collideSingleContact(rotq.inverse()*perturbeRot*rotq,body0,body1,dispatchInfo,resultOut);
157 if (m_manifoldPtr->getNumContacts())
159 resultOut->refreshContactPoints();
164 btScalar btConvexPlaneCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* col0,btCollisionObject* col1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)