/* Bullet Continuous Collision Detection and Physics Library Copyright (c) 2011 Advanced Micro Devices, Inc. http://bulletphysics.org This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 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. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ ///This file was written by Erwin Coumans ///Separating axis rest based on work from Pierre Terdiman, see ///And contact clipping based on work from Simon Hobbs #include "btPolyhedralContactClipping.h" #include "BulletCollision/CollisionShapes/btConvexPolyhedron.h" #include //for FLT_MAX int gExpectedNbTests=0; int gActualNbTests = 0; bool gUseInternalObject = true; // Clips a face to the back of a plane void btPolyhedralContactClipping::clipFace(const btVertexArray& pVtxIn, btVertexArray& ppVtxOut, const btVector3& planeNormalWS,btScalar planeEqWS) { int ve; btScalar ds, de; int numVerts = pVtxIn.size(); if (numVerts < 2) return; btVector3 firstVertex=pVtxIn[pVtxIn.size()-1]; btVector3 endVertex = pVtxIn[0]; ds = planeNormalWS.dot(firstVertex)+planeEqWS; for (ve = 0; ve < numVerts; ve++) { endVertex=pVtxIn[ve]; de = planeNormalWS.dot(endVertex)+planeEqWS; if (ds<0) { if (de<0) { // Start < 0, end < 0, so output endVertex ppVtxOut.push_back(endVertex); } else { // Start < 0, end >= 0, so output intersection ppVtxOut.push_back( firstVertex.lerp(endVertex,btScalar(ds * 1.f/(ds - de)))); } } else { if (de<0) { // Start >= 0, end < 0 so output intersection and end ppVtxOut.push_back(firstVertex.lerp(endVertex,btScalar(ds * 1.f/(ds - de)))); ppVtxOut.push_back(endVertex); } } firstVertex = endVertex; ds = de; } } static bool TestSepAxis(const btConvexPolyhedron& hullA, const btConvexPolyhedron& hullB, const btTransform& transA,const btTransform& transB, const btVector3& sep_axis, btScalar& depth, btVector3& witnessPointA, btVector3& witnessPointB) { btScalar Min0,Max0; btScalar Min1,Max1; btVector3 witnesPtMinA,witnesPtMaxA; btVector3 witnesPtMinB,witnesPtMaxB; hullA.project(transA,sep_axis, Min0, Max0,witnesPtMinA,witnesPtMaxA); hullB.project(transB, sep_axis, Min1, Max1,witnesPtMinB,witnesPtMaxB); if(Max0=0.0f); btScalar d1 = Max1 - Min0; btAssert(d1>=0.0f); if (d01e-6 || btFabs(v.y())>1e-6 || btFabs(v.z())>1e-6) return false; return true; } #ifdef TEST_INTERNAL_OBJECTS inline void BoxSupport(const btScalar extents[3], const btScalar sv[3], btScalar p[3]) { // This version is ~11.000 cycles (4%) faster overall in one of the tests. // IR(p[0]) = IR(extents[0])|(IR(sv[0])&SIGN_BITMASK); // IR(p[1]) = IR(extents[1])|(IR(sv[1])&SIGN_BITMASK); // IR(p[2]) = IR(extents[2])|(IR(sv[2])&SIGN_BITMASK); p[0] = sv[0] < 0.0f ? -extents[0] : extents[0]; p[1] = sv[1] < 0.0f ? -extents[1] : extents[1]; p[2] = sv[2] < 0.0f ? -extents[2] : extents[2]; } void InverseTransformPoint3x3(btVector3& out, const btVector3& in, const btTransform& tr) { const btMatrix3x3& rot = tr.getBasis(); const btVector3& r0 = rot[0]; const btVector3& r1 = rot[1]; const btVector3& r2 = rot[2]; const btScalar x = r0.x()*in.x() + r1.x()*in.y() + r2.x()*in.z(); const btScalar y = r0.y()*in.x() + r1.y()*in.y() + r2.y()*in.z(); const btScalar z = r0.z()*in.x() + r1.z()*in.y() + r2.z()*in.z(); out.setValue(x, y, z); } bool TestInternalObjects( const btTransform& trans0, const btTransform& trans1, const btVector3& delta_c, const btVector3& axis, const btConvexPolyhedron& convex0, const btConvexPolyhedron& convex1, btScalar dmin) { const btScalar dp = delta_c.dot(axis); btVector3 localAxis0; InverseTransformPoint3x3(localAxis0, axis,trans0); btVector3 localAxis1; InverseTransformPoint3x3(localAxis1, axis,trans1); btScalar p0[3]; BoxSupport(convex0.m_extents, localAxis0, p0); btScalar p1[3]; BoxSupport(convex1.m_extents, localAxis1, p1); const btScalar Radius0 = p0[0]*localAxis0.x() + p0[1]*localAxis0.y() + p0[2]*localAxis0.z(); const btScalar Radius1 = p1[0]*localAxis1.x() + p1[1]*localAxis1.y() + p1[2]*localAxis1.z(); const btScalar MinRadius = Radius0>convex0.m_radius ? Radius0 : convex0.m_radius; const btScalar MaxRadius = Radius1>convex1.m_radius ? Radius1 : convex1.m_radius; const btScalar MinMaxRadius = MaxRadius + MinRadius; const btScalar d0 = MinMaxRadius + dp; const btScalar d1 = MinMaxRadius - dp; const btScalar depth = d0dmin) return false; return true; } #endif //TEST_INTERNAL_OBJECTS SIMD_FORCE_INLINE void btSegmentsClosestPoints( btVector3& ptsVector, btVector3& offsetA, btVector3& offsetB, btScalar& tA, btScalar& tB, const btVector3& translation, const btVector3& dirA, btScalar hlenA, const btVector3& dirB, btScalar hlenB ) { // compute the parameters of the closest points on each line segment btScalar dirA_dot_dirB = btDot(dirA,dirB); btScalar dirA_dot_trans = btDot(dirA,translation); btScalar dirB_dot_trans = btDot(dirB,translation); btScalar denom = 1.0f - dirA_dot_dirB * dirA_dot_dirB; if ( denom == 0.0f ) { tA = 0.0f; } else { tA = ( dirA_dot_trans - dirB_dot_trans * dirA_dot_dirB ) / denom; if ( tA < -hlenA ) tA = -hlenA; else if ( tA > hlenA ) tA = hlenA; } tB = tA * dirA_dot_dirB - dirB_dot_trans; if ( tB < -hlenB ) { tB = -hlenB; tA = tB * dirA_dot_dirB + dirA_dot_trans; if ( tA < -hlenA ) tA = -hlenA; else if ( tA > hlenA ) tA = hlenA; } else if ( tB > hlenB ) { tB = hlenB; tA = tB * dirA_dot_dirB + dirA_dot_trans; if ( tA < -hlenA ) tA = -hlenA; else if ( tA > hlenA ) tA = hlenA; } // compute the closest points relative to segment centers. offsetA = dirA * tA; offsetB = dirB * tB; ptsVector = translation - offsetA + offsetB; } bool btPolyhedralContactClipping::findSeparatingAxis( const btConvexPolyhedron& hullA, const btConvexPolyhedron& hullB, const btTransform& transA,const btTransform& transB, btVector3& sep, btDiscreteCollisionDetectorInterface::Result& resultOut) { gActualSATPairTests++; //#ifdef TEST_INTERNAL_OBJECTS const btVector3 c0 = transA * hullA.m_localCenter; const btVector3 c1 = transB * hullB.m_localCenter; const btVector3 DeltaC2 = c0 - c1; //#endif btScalar dmin = FLT_MAX; int curPlaneTests=0; int numFacesA = hullA.m_faces.size(); // Test normals from hullA for(int i=0;i=0&&edgeB>=0) { // printf("edge-edge\n"); //add an edge-edge contact btVector3 ptsVector; btVector3 offsetA; btVector3 offsetB; btScalar tA; btScalar tB; btVector3 translation = witnessPointB-witnessPointA; btVector3 dirA = worldEdgeA; btVector3 dirB = worldEdgeB; btScalar hlenB = 1e30f; btScalar hlenA = 1e30f; btSegmentsClosestPoints(ptsVector,offsetA,offsetB,tA,tB, translation, dirA, hlenA, dirB,hlenB); btScalar nlSqrt = ptsVector.length2(); if (nlSqrt>SIMD_EPSILON) { btScalar nl = btSqrt(nlSqrt); ptsVector *= 1.f/nl; if (ptsVector.dot(DeltaC2)<0.f) { ptsVector*=-1.f; } btVector3 ptOnB = witnessPointB + offsetB; btScalar distance = nl; resultOut.addContactPoint(ptsVector, ptOnB,-distance); } } if((DeltaC2.dot(sep))<0.0f) sep = -sep; return true; } void btPolyhedralContactClipping::clipFaceAgainstHull(const btVector3& separatingNormal, const btConvexPolyhedron& hullA, const btTransform& transA, btVertexArray& worldVertsB1,btVertexArray& worldVertsB2, const btScalar minDist, btScalar maxDist,btDiscreteCollisionDetectorInterface::Result& resultOut) { worldVertsB2.resize(0); btVertexArray* pVtxIn = &worldVertsB1; btVertexArray* pVtxOut = &worldVertsB2; pVtxOut->reserve(pVtxIn->size()); int closestFaceA=-1; { btScalar dmin = FLT_MAX; for(int face=0;faceresize(0); } //#define ONLY_REPORT_DEEPEST_POINT btVector3 point; // only keep points that are behind the witness face { btVector3 localPlaneNormal (polyA.m_plane[0],polyA.m_plane[1],polyA.m_plane[2]); btScalar localPlaneEq = polyA.m_plane[3]; btVector3 planeNormalWS = transA.getBasis()*localPlaneNormal; btScalar planeEqWS=localPlaneEq-planeNormalWS.dot(transA.getOrigin()); for (int i=0;isize();i++) { btVector3 vtx = pVtxIn->at(i); btScalar depth = planeNormalWS.dot(vtx)+planeEqWS; if (depth <=minDist) { // printf("clamped: depth=%f to minDist=%f\n",depth,minDist); depth = minDist; } if (depth <=maxDist) { btVector3 point = pVtxIn->at(i); #ifdef ONLY_REPORT_DEEPEST_POINT curMaxDist = depth; #else #if 0 if (depth<-3) { printf("error in btPolyhedralContactClipping depth = %f\n", depth); printf("likely wrong separatingNormal passed in\n"); } #endif resultOut.addContactPoint(separatingNormal,point,depth); #endif } } } #ifdef ONLY_REPORT_DEEPEST_POINT if (curMaxDist dmax) { dmax = d; closestFaceB = face; } } } worldVertsB1.resize(0); { const btFace& polyB = hullB.m_faces[closestFaceB]; const int numVertices = polyB.m_indices.size(); for(int e0=0;e0=0) clipFaceAgainstHull(separatingNormal, hullA, transA,worldVertsB1, worldVertsB2,minDist, maxDist,resultOut); }