| [1963] | 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 "btSphereSphereCollisionAlgorithm.h" | 
|---|
|  | 17 | #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h" | 
|---|
|  | 18 | #include "BulletCollision/CollisionShapes/btSphereShape.h" | 
|---|
|  | 19 | #include "BulletCollision/CollisionDispatch/btCollisionObject.h" | 
|---|
|  | 20 |  | 
|---|
|  | 21 | btSphereSphereCollisionAlgorithm::btSphereSphereCollisionAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* col0,btCollisionObject* col1) | 
|---|
| [2430] | 22 | : btActivatingCollisionAlgorithm(ci,col0,col1), | 
|---|
| [1963] | 23 | m_ownManifold(false), | 
|---|
|  | 24 | m_manifoldPtr(mf) | 
|---|
|  | 25 | { | 
|---|
|  | 26 | if (!m_manifoldPtr) | 
|---|
|  | 27 | { | 
|---|
|  | 28 | m_manifoldPtr = m_dispatcher->getNewManifold(col0,col1); | 
|---|
|  | 29 | m_ownManifold = true; | 
|---|
|  | 30 | } | 
|---|
|  | 31 | } | 
|---|
|  | 32 |  | 
|---|
|  | 33 | btSphereSphereCollisionAlgorithm::~btSphereSphereCollisionAlgorithm() | 
|---|
|  | 34 | { | 
|---|
|  | 35 | if (m_ownManifold) | 
|---|
|  | 36 | { | 
|---|
|  | 37 | if (m_manifoldPtr) | 
|---|
|  | 38 | m_dispatcher->releaseManifold(m_manifoldPtr); | 
|---|
|  | 39 | } | 
|---|
|  | 40 | } | 
|---|
|  | 41 |  | 
|---|
|  | 42 | void btSphereSphereCollisionAlgorithm::processCollision (btCollisionObject* col0,btCollisionObject* col1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut) | 
|---|
|  | 43 | { | 
|---|
|  | 44 | (void)dispatchInfo; | 
|---|
|  | 45 |  | 
|---|
|  | 46 | if (!m_manifoldPtr) | 
|---|
|  | 47 | return; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | resultOut->setPersistentManifold(m_manifoldPtr); | 
|---|
|  | 50 |  | 
|---|
|  | 51 | btSphereShape* sphere0 = (btSphereShape*)col0->getCollisionShape(); | 
|---|
|  | 52 | btSphereShape* sphere1 = (btSphereShape*)col1->getCollisionShape(); | 
|---|
|  | 53 |  | 
|---|
|  | 54 | btVector3 diff = col0->getWorldTransform().getOrigin()-  col1->getWorldTransform().getOrigin(); | 
|---|
|  | 55 | btScalar len = diff.length(); | 
|---|
|  | 56 | btScalar radius0 = sphere0->getRadius(); | 
|---|
|  | 57 | btScalar radius1 = sphere1->getRadius(); | 
|---|
|  | 58 |  | 
|---|
|  | 59 | #ifdef CLEAR_MANIFOLD | 
|---|
|  | 60 | m_manifoldPtr->clearManifold(); //don't do this, it disables warmstarting | 
|---|
|  | 61 | #endif | 
|---|
|  | 62 |  | 
|---|
|  | 63 | ///iff distance positive, don't generate a new contact | 
|---|
|  | 64 | if ( len > (radius0+radius1)) | 
|---|
|  | 65 | { | 
|---|
|  | 66 | #ifndef CLEAR_MANIFOLD | 
|---|
|  | 67 | resultOut->refreshContactPoints(); | 
|---|
|  | 68 | #endif //CLEAR_MANIFOLD | 
|---|
|  | 69 | return; | 
|---|
|  | 70 | } | 
|---|
|  | 71 | ///distance (negative means penetration) | 
|---|
|  | 72 | btScalar dist = len - (radius0+radius1); | 
|---|
|  | 73 |  | 
|---|
|  | 74 | btVector3 normalOnSurfaceB(1,0,0); | 
|---|
|  | 75 | if (len > SIMD_EPSILON) | 
|---|
|  | 76 | { | 
|---|
|  | 77 | normalOnSurfaceB = diff / len; | 
|---|
|  | 78 | } | 
|---|
|  | 79 |  | 
|---|
|  | 80 | ///point on A (worldspace) | 
|---|
| [2882] | 81 | ///btVector3 pos0 = col0->getWorldTransform().getOrigin() - radius0 * normalOnSurfaceB; | 
|---|
| [1963] | 82 | ///point on B (worldspace) | 
|---|
|  | 83 | btVector3 pos1 = col1->getWorldTransform().getOrigin() + radius1* normalOnSurfaceB; | 
|---|
|  | 84 |  | 
|---|
|  | 85 | /// report a contact. internally this will be kept persistent, and contact reduction is done | 
|---|
|  | 86 |  | 
|---|
|  | 87 |  | 
|---|
|  | 88 | resultOut->addContactPoint(normalOnSurfaceB,pos1,dist); | 
|---|
|  | 89 |  | 
|---|
|  | 90 | #ifndef CLEAR_MANIFOLD | 
|---|
|  | 91 | resultOut->refreshContactPoints(); | 
|---|
|  | 92 | #endif //CLEAR_MANIFOLD | 
|---|
|  | 93 |  | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
|  | 96 | btScalar btSphereSphereCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* col0,btCollisionObject* col1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut) | 
|---|
|  | 97 | { | 
|---|
|  | 98 | (void)col0; | 
|---|
|  | 99 | (void)col1; | 
|---|
|  | 100 | (void)dispatchInfo; | 
|---|
|  | 101 | (void)resultOut; | 
|---|
|  | 102 |  | 
|---|
|  | 103 | //not yet | 
|---|
|  | 104 | return btScalar(1.); | 
|---|
|  | 105 | } | 
|---|