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 "btSphereBoxCollisionAlgorithm.h"
17 #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
18 #include "BulletCollision/CollisionShapes/btSphereShape.h"
19 #include "BulletCollision/CollisionShapes/btBoxShape.h"
20 #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
21 #include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
22 //#include <stdio.h>
23
btSphereBoxCollisionAlgorithm(btPersistentManifold * mf,const btCollisionAlgorithmConstructionInfo & ci,const btCollisionObjectWrapper * col0Wrap,const btCollisionObjectWrapper * col1Wrap,bool isSwapped)24 btSphereBoxCollisionAlgorithm::btSphereBoxCollisionAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,const btCollisionObjectWrapper* col0Wrap,const btCollisionObjectWrapper* col1Wrap, bool isSwapped)
25 : btActivatingCollisionAlgorithm(ci,col0Wrap,col1Wrap),
26 m_ownManifold(false),
27 m_manifoldPtr(mf),
28 m_isSwapped(isSwapped)
29 {
30 const btCollisionObjectWrapper* sphereObjWrap = m_isSwapped? col1Wrap : col0Wrap;
31 const btCollisionObjectWrapper* boxObjWrap = m_isSwapped? col0Wrap : col1Wrap;
32
33 if (!m_manifoldPtr && m_dispatcher->needsCollision(sphereObjWrap->getCollisionObject(),boxObjWrap->getCollisionObject()))
34 {
35 m_manifoldPtr = m_dispatcher->getNewManifold(sphereObjWrap->getCollisionObject(),boxObjWrap->getCollisionObject());
36 m_ownManifold = true;
37 }
38 }
39
40
~btSphereBoxCollisionAlgorithm()41 btSphereBoxCollisionAlgorithm::~btSphereBoxCollisionAlgorithm()
42 {
43 if (m_ownManifold)
44 {
45 if (m_manifoldPtr)
46 m_dispatcher->releaseManifold(m_manifoldPtr);
47 }
48 }
49
50
51
processCollision(const btCollisionObjectWrapper * body0Wrap,const btCollisionObjectWrapper * body1Wrap,const btDispatcherInfo & dispatchInfo,btManifoldResult * resultOut)52 void btSphereBoxCollisionAlgorithm::processCollision (const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap, const btDispatcherInfo& dispatchInfo, btManifoldResult* resultOut)
53 {
54 (void)dispatchInfo;
55 (void)resultOut;
56 if (!m_manifoldPtr)
57 return;
58
59 const btCollisionObjectWrapper* sphereObjWrap = m_isSwapped? body1Wrap : body0Wrap;
60 const btCollisionObjectWrapper* boxObjWrap = m_isSwapped? body0Wrap : body1Wrap;
61
62 btVector3 pOnBox;
63
64 btVector3 normalOnSurfaceB;
65 btScalar penetrationDepth;
66 btVector3 sphereCenter = sphereObjWrap->getWorldTransform().getOrigin();
67 const btSphereShape* sphere0 = (const btSphereShape*)sphereObjWrap->getCollisionShape();
68 btScalar radius = sphere0->getRadius();
69 btScalar maxContactDistance = m_manifoldPtr->getContactBreakingThreshold();
70
71 resultOut->setPersistentManifold(m_manifoldPtr);
72
73 if (getSphereDistance(boxObjWrap, pOnBox, normalOnSurfaceB, penetrationDepth, sphereCenter, radius, maxContactDistance))
74 {
75 /// report a contact. internally this will be kept persistent, and contact reduction is done
76 resultOut->addContactPoint(normalOnSurfaceB, pOnBox, penetrationDepth);
77 }
78
79 if (m_ownManifold)
80 {
81 if (m_manifoldPtr->getNumContacts())
82 {
83 resultOut->refreshContactPoints();
84 }
85 }
86
87 }
88
calculateTimeOfImpact(btCollisionObject * col0,btCollisionObject * col1,const btDispatcherInfo & dispatchInfo,btManifoldResult * resultOut)89 btScalar btSphereBoxCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* col0,btCollisionObject* col1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
90 {
91 (void)resultOut;
92 (void)dispatchInfo;
93 (void)col0;
94 (void)col1;
95
96 //not yet
97 return btScalar(1.);
98 }
99
100
getSphereDistance(const btCollisionObjectWrapper * boxObjWrap,btVector3 & pointOnBox,btVector3 & normal,btScalar & penetrationDepth,const btVector3 & sphereCenter,btScalar fRadius,btScalar maxContactDistance)101 bool btSphereBoxCollisionAlgorithm::getSphereDistance(const btCollisionObjectWrapper* boxObjWrap, btVector3& pointOnBox, btVector3& normal, btScalar& penetrationDepth, const btVector3& sphereCenter, btScalar fRadius, btScalar maxContactDistance )
102 {
103 const btBoxShape* boxShape= (const btBoxShape*)boxObjWrap->getCollisionShape();
104 btVector3 const &boxHalfExtent = boxShape->getHalfExtentsWithoutMargin();
105 btScalar boxMargin = boxShape->getMargin();
106 penetrationDepth = 1.0f;
107
108 // convert the sphere position to the box's local space
109 btTransform const &m44T = boxObjWrap->getWorldTransform();
110 btVector3 sphereRelPos = m44T.invXform(sphereCenter);
111
112 // Determine the closest point to the sphere center in the box
113 btVector3 closestPoint = sphereRelPos;
114 closestPoint.setX( btMin(boxHalfExtent.getX(), closestPoint.getX()) );
115 closestPoint.setX( btMax(-boxHalfExtent.getX(), closestPoint.getX()) );
116 closestPoint.setY( btMin(boxHalfExtent.getY(), closestPoint.getY()) );
117 closestPoint.setY( btMax(-boxHalfExtent.getY(), closestPoint.getY()) );
118 closestPoint.setZ( btMin(boxHalfExtent.getZ(), closestPoint.getZ()) );
119 closestPoint.setZ( btMax(-boxHalfExtent.getZ(), closestPoint.getZ()) );
120
121 btScalar intersectionDist = fRadius + boxMargin;
122 btScalar contactDist = intersectionDist + maxContactDistance;
123 normal = sphereRelPos - closestPoint;
124
125 //if there is no penetration, we are done
126 btScalar dist2 = normal.length2();
127 if (dist2 > contactDist * contactDist)
128 {
129 return false;
130 }
131
132 btScalar distance;
133
134 //special case if the sphere center is inside the box
135 if (dist2 <= SIMD_EPSILON)
136 {
137 distance = -getSpherePenetration(boxHalfExtent, sphereRelPos, closestPoint, normal);
138 }
139 else //compute the penetration details
140 {
141 distance = normal.length();
142 normal /= distance;
143 }
144
145 pointOnBox = closestPoint + normal * boxMargin;
146 // v3PointOnSphere = sphereRelPos - (normal * fRadius);
147 penetrationDepth = distance - intersectionDist;
148
149 // transform back in world space
150 btVector3 tmp = m44T(pointOnBox);
151 pointOnBox = tmp;
152 // tmp = m44T(v3PointOnSphere);
153 // v3PointOnSphere = tmp;
154 tmp = m44T.getBasis() * normal;
155 normal = tmp;
156
157 return true;
158 }
159
getSpherePenetration(btVector3 const & boxHalfExtent,btVector3 const & sphereRelPos,btVector3 & closestPoint,btVector3 & normal)160 btScalar btSphereBoxCollisionAlgorithm::getSpherePenetration( btVector3 const &boxHalfExtent, btVector3 const &sphereRelPos, btVector3 &closestPoint, btVector3& normal )
161 {
162 //project the center of the sphere on the closest face of the box
163 btScalar faceDist = boxHalfExtent.getX() - sphereRelPos.getX();
164 btScalar minDist = faceDist;
165 closestPoint.setX( boxHalfExtent.getX() );
166 normal.setValue(btScalar(1.0f), btScalar(0.0f), btScalar(0.0f));
167
168 faceDist = boxHalfExtent.getX() + sphereRelPos.getX();
169 if (faceDist < minDist)
170 {
171 minDist = faceDist;
172 closestPoint = sphereRelPos;
173 closestPoint.setX( -boxHalfExtent.getX() );
174 normal.setValue(btScalar(-1.0f), btScalar(0.0f), btScalar(0.0f));
175 }
176
177 faceDist = boxHalfExtent.getY() - sphereRelPos.getY();
178 if (faceDist < minDist)
179 {
180 minDist = faceDist;
181 closestPoint = sphereRelPos;
182 closestPoint.setY( boxHalfExtent.getY() );
183 normal.setValue(btScalar(0.0f), btScalar(1.0f), btScalar(0.0f));
184 }
185
186 faceDist = boxHalfExtent.getY() + sphereRelPos.getY();
187 if (faceDist < minDist)
188 {
189 minDist = faceDist;
190 closestPoint = sphereRelPos;
191 closestPoint.setY( -boxHalfExtent.getY() );
192 normal.setValue(btScalar(0.0f), btScalar(-1.0f), btScalar(0.0f));
193 }
194
195 faceDist = boxHalfExtent.getZ() - sphereRelPos.getZ();
196 if (faceDist < minDist)
197 {
198 minDist = faceDist;
199 closestPoint = sphereRelPos;
200 closestPoint.setZ( boxHalfExtent.getZ() );
201 normal.setValue(btScalar(0.0f), btScalar(0.0f), btScalar(1.0f));
202 }
203
204 faceDist = boxHalfExtent.getZ() + sphereRelPos.getZ();
205 if (faceDist < minDist)
206 {
207 minDist = faceDist;
208 closestPoint = sphereRelPos;
209 closestPoint.setZ( -boxHalfExtent.getZ() );
210 normal.setValue(btScalar(0.0f), btScalar(0.0f), btScalar(-1.0f));
211 }
212
213 return minDist;
214 }
215