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