Back to index

supertuxkart  0.5+dfsg1
btConvexPlaneCollisionAlgorithm.cpp
Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 #include "btConvexPlaneCollisionAlgorithm.h"
00017 
00018 #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
00019 #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
00020 #include "BulletCollision/CollisionShapes/btConvexShape.h"
00021 #include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
00022 
00023 //#include <stdio.h>
00024 
00025 btConvexPlaneCollisionAlgorithm::btConvexPlaneCollisionAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* col0,btCollisionObject* col1, bool isSwapped)
00026 : btCollisionAlgorithm(ci),
00027 m_ownManifold(false),
00028 m_manifoldPtr(mf),
00029 m_isSwapped(isSwapped)
00030 {
00031        btCollisionObject* convexObj = m_isSwapped? col1 : col0;
00032        btCollisionObject* planeObj = m_isSwapped? col0 : col1;
00033        
00034        if (!m_manifoldPtr && m_dispatcher->needsCollision(convexObj,planeObj))
00035        {
00036               m_manifoldPtr = m_dispatcher->getNewManifold(convexObj,planeObj);
00037               m_ownManifold = true;
00038        }
00039 }
00040 
00041 
00042 btConvexPlaneCollisionAlgorithm::~btConvexPlaneCollisionAlgorithm()
00043 {
00044        if (m_ownManifold)
00045        {
00046               if (m_manifoldPtr)
00047                      m_dispatcher->releaseManifold(m_manifoldPtr);
00048        }
00049 }
00050 
00051 
00052 
00053 void btConvexPlaneCollisionAlgorithm::processCollision (btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
00054 {
00055        (void)dispatchInfo;
00056        (void)resultOut;
00057        if (!m_manifoldPtr)
00058               return;
00059 
00060        btCollisionObject* convexObj = m_isSwapped? body1 : body0;
00061        btCollisionObject* planeObj = m_isSwapped? body0: body1;
00062 
00063        btConvexShape* convexShape = (btConvexShape*) convexObj->getCollisionShape();
00064        btStaticPlaneShape* planeShape = (btStaticPlaneShape*) planeObj->getCollisionShape();
00065 
00066        bool hasCollision = false;
00067        const btVector3& planeNormal = planeShape->getPlaneNormal();
00068        const btScalar& planeConstant = planeShape->getPlaneConstant();
00069        btTransform planeInConvex;
00070        planeInConvex= convexObj->getWorldTransform().inverse() * planeObj->getWorldTransform();
00071        btTransform convexInPlaneTrans;
00072        convexInPlaneTrans= planeObj->getWorldTransform().inverse() * convexObj->getWorldTransform();
00073 
00074        btVector3 vtx = convexShape->localGetSupportingVertex(planeInConvex.getBasis()*-planeNormal);
00075        btVector3 vtxInPlane = convexInPlaneTrans(vtx);
00076        btScalar distance = (planeNormal.dot(vtxInPlane) - planeConstant);
00077 
00078        btVector3 vtxInPlaneProjected = vtxInPlane - distance*planeNormal;
00079        btVector3 vtxInPlaneWorld = planeObj->getWorldTransform() * vtxInPlaneProjected;
00080 
00081        hasCollision = distance < m_manifoldPtr->getContactBreakingThreshold();
00082        resultOut->setPersistentManifold(m_manifoldPtr);
00083        if (hasCollision)
00084        {
00086               btVector3 normalOnSurfaceB = planeObj->getWorldTransform().getBasis() * planeNormal;
00087               btVector3 pOnB = vtxInPlaneWorld;
00088               resultOut->addContactPoint(normalOnSurfaceB,pOnB,distance);
00089        }
00090        if (m_ownManifold)
00091        {
00092               if (m_manifoldPtr->getNumContacts())
00093               {
00094                      resultOut->refreshContactPoints();
00095               }
00096        }
00097 }
00098 
00099 btScalar btConvexPlaneCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* col0,btCollisionObject* col1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
00100 {
00101        (void)resultOut;
00102        (void)dispatchInfo;
00103        (void)col0;
00104        (void)col1;
00105 
00106        //not yet
00107        return btScalar(1.);
00108 }