| 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 "btConvexPlaneCollisionAlgorithm.h" | 
|---|
| 17 |  | 
|---|
| 18 | #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h" | 
|---|
| 19 | #include "BulletCollision/CollisionDispatch/btCollisionObject.h" | 
|---|
| 20 | #include "BulletCollision/CollisionShapes/btConvexShape.h" | 
|---|
| 21 | #include "BulletCollision/CollisionShapes/btStaticPlaneShape.h" | 
|---|
| 22 |  | 
|---|
| 23 | //#include <stdio.h> | 
|---|
| 24 |  | 
|---|
| 25 | btConvexPlaneCollisionAlgorithm::btConvexPlaneCollisionAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* col0,btCollisionObject* col1, bool isSwapped) | 
|---|
| 26 | : btCollisionAlgorithm(ci), | 
|---|
| 27 | m_ownManifold(false), | 
|---|
| 28 | m_manifoldPtr(mf), | 
|---|
| 29 | m_isSwapped(isSwapped) | 
|---|
| 30 | { | 
|---|
| 31 |         btCollisionObject* convexObj = m_isSwapped? col1 : col0; | 
|---|
| 32 |         btCollisionObject* planeObj = m_isSwapped? col0 : col1; | 
|---|
| 33 |          | 
|---|
| 34 |         if (!m_manifoldPtr && m_dispatcher->needsCollision(convexObj,planeObj)) | 
|---|
| 35 |         { | 
|---|
| 36 |                 m_manifoldPtr = m_dispatcher->getNewManifold(convexObj,planeObj); | 
|---|
| 37 |                 m_ownManifold = true; | 
|---|
| 38 |         } | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 |  | 
|---|
| 42 | btConvexPlaneCollisionAlgorithm::~btConvexPlaneCollisionAlgorithm() | 
|---|
| 43 | { | 
|---|
| 44 |         if (m_ownManifold) | 
|---|
| 45 |         { | 
|---|
| 46 |                 if (m_manifoldPtr) | 
|---|
| 47 |                         m_dispatcher->releaseManifold(m_manifoldPtr); | 
|---|
| 48 |         } | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 | void btConvexPlaneCollisionAlgorithm::processCollision (btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut) | 
|---|
| 54 | { | 
|---|
| 55 |         (void)dispatchInfo; | 
|---|
| 56 |         (void)resultOut; | 
|---|
| 57 |         if (!m_manifoldPtr) | 
|---|
| 58 |                 return; | 
|---|
| 59 |  | 
|---|
| 60 |         btCollisionObject* convexObj = m_isSwapped? body1 : body0; | 
|---|
| 61 |         btCollisionObject* planeObj = m_isSwapped? body0: body1; | 
|---|
| 62 |  | 
|---|
| 63 |         btConvexShape* convexShape = (btConvexShape*) convexObj->getCollisionShape(); | 
|---|
| 64 |         btStaticPlaneShape* planeShape = (btStaticPlaneShape*) planeObj->getCollisionShape(); | 
|---|
| 65 |  | 
|---|
| 66 |         bool hasCollision = false; | 
|---|
| 67 |         const btVector3& planeNormal = planeShape->getPlaneNormal(); | 
|---|
| 68 |         const btScalar& planeConstant = planeShape->getPlaneConstant(); | 
|---|
| 69 |         btTransform planeInConvex; | 
|---|
| 70 |         planeInConvex= convexObj->getWorldTransform().inverse() * planeObj->getWorldTransform(); | 
|---|
| 71 |         btTransform convexInPlaneTrans; | 
|---|
| 72 |         convexInPlaneTrans= planeObj->getWorldTransform().inverse() * convexObj->getWorldTransform(); | 
|---|
| 73 |  | 
|---|
| 74 |         btVector3 vtx = convexShape->localGetSupportingVertex(planeInConvex.getBasis()*-planeNormal); | 
|---|
| 75 |         btVector3 vtxInPlane = convexInPlaneTrans(vtx); | 
|---|
| 76 |         btScalar distance = (planeNormal.dot(vtxInPlane) - planeConstant); | 
|---|
| 77 |  | 
|---|
| 78 |         btVector3 vtxInPlaneProjected = vtxInPlane - distance*planeNormal; | 
|---|
| 79 |         btVector3 vtxInPlaneWorld = planeObj->getWorldTransform() * vtxInPlaneProjected; | 
|---|
| 80 |  | 
|---|
| 81 |         hasCollision = distance < m_manifoldPtr->getContactBreakingThreshold(); | 
|---|
| 82 |         resultOut->setPersistentManifold(m_manifoldPtr); | 
|---|
| 83 |         if (hasCollision) | 
|---|
| 84 |         { | 
|---|
| 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); | 
|---|
| 89 |         } | 
|---|
| 90 |         if (m_ownManifold) | 
|---|
| 91 |         { | 
|---|
| 92 |                 if (m_manifoldPtr->getNumContacts()) | 
|---|
| 93 |                 { | 
|---|
| 94 |                         resultOut->refreshContactPoints(); | 
|---|
| 95 |                 } | 
|---|
| 96 |         } | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | btScalar btConvexPlaneCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* col0,btCollisionObject* col1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut) | 
|---|
| 100 | { | 
|---|
| 101 |         (void)resultOut; | 
|---|
| 102 |         (void)dispatchInfo; | 
|---|
| 103 |         (void)col0; | 
|---|
| 104 |         (void)col1; | 
|---|
| 105 |  | 
|---|
| 106 |         //not yet | 
|---|
| 107 |         return btScalar(1.); | 
|---|
| 108 | } | 
|---|