btSphereSphereCollisionAlgorithm.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 "btSphereSphereCollisionAlgorithm.h"
00017 #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
00018 #include "BulletCollision/CollisionShapes/btSphereShape.h"
00019 #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
00020 #include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
00021 
00022 btSphereSphereCollisionAlgorithm::btSphereSphereCollisionAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,const btCollisionObjectWrapper* col0Wrap,const btCollisionObjectWrapper* col1Wrap)
00023 : btActivatingCollisionAlgorithm(ci,col0Wrap,col1Wrap),
00024 m_ownManifold(false),
00025 m_manifoldPtr(mf)
00026 {
00027         if (!m_manifoldPtr)
00028         {
00029                 m_manifoldPtr = m_dispatcher->getNewManifold(col0Wrap->getCollisionObject(),col1Wrap->getCollisionObject());
00030                 m_ownManifold = true;
00031         }
00032 }
00033 
00034 btSphereSphereCollisionAlgorithm::~btSphereSphereCollisionAlgorithm()
00035 {
00036         if (m_ownManifold)
00037         {
00038                 if (m_manifoldPtr)
00039                         m_dispatcher->releaseManifold(m_manifoldPtr);
00040         }
00041 }
00042 
00043 void btSphereSphereCollisionAlgorithm::processCollision (const btCollisionObjectWrapper* col0Wrap,const btCollisionObjectWrapper* col1Wrap,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
00044 {
00045         (void)dispatchInfo;
00046 
00047         if (!m_manifoldPtr)
00048                 return;
00049 
00050         resultOut->setPersistentManifold(m_manifoldPtr);
00051 
00052         btSphereShape* sphere0 = (btSphereShape*)col0Wrap->getCollisionShape();
00053         btSphereShape* sphere1 = (btSphereShape*)col1Wrap->getCollisionShape();
00054 
00055         btVector3 diff = col0Wrap->getWorldTransform().getOrigin()-  col1Wrap->getWorldTransform().getOrigin();
00056         btScalar len = diff.length();
00057         btScalar radius0 = sphere0->getRadius();
00058         btScalar radius1 = sphere1->getRadius();
00059 
00060 #ifdef CLEAR_MANIFOLD
00061         m_manifoldPtr->clearManifold(); //don't do this, it disables warmstarting
00062 #endif
00063 
00065         if ( len > (radius0+radius1))
00066         {
00067 #ifndef CLEAR_MANIFOLD
00068                 resultOut->refreshContactPoints();
00069 #endif //CLEAR_MANIFOLD
00070                 return;
00071         }
00073         btScalar dist = len - (radius0+radius1);
00074 
00075         btVector3 normalOnSurfaceB(1,0,0);
00076         if (len > SIMD_EPSILON)
00077         {
00078                 normalOnSurfaceB = diff / len;
00079         }
00080 
00084         btVector3 pos1 = col1Wrap->getWorldTransform().getOrigin() + radius1* normalOnSurfaceB;
00085 
00087         
00088         
00089         resultOut->addContactPoint(normalOnSurfaceB,pos1,dist);
00090 
00091 #ifndef CLEAR_MANIFOLD
00092         resultOut->refreshContactPoints();
00093 #endif //CLEAR_MANIFOLD
00094 
00095 }
00096 
00097 btScalar btSphereSphereCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* col0,btCollisionObject* col1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
00098 {
00099         (void)col0;
00100         (void)col1;
00101         (void)dispatchInfo;
00102         (void)resultOut;
00103 
00104         //not yet
00105         return btScalar(1.);
00106 }