2017-08-01 12:30:58 +00:00
|
|
|
/*
|
|
|
|
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"
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
#include <float.h> //for FLT_MAX
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
int gExpectedNbTests = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
int gActualNbTests = 0;
|
|
|
|
bool gUseInternalObject = true;
|
|
|
|
|
|
|
|
// Clips a face to the back of a plane
|
2019-01-03 13:26:51 +00:00
|
|
|
void btPolyhedralContactClipping::clipFace(const btVertexArray& pVtxIn, btVertexArray& ppVtxOut, const btVector3& planeNormalWS, btScalar planeEqWS)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int ve;
|
|
|
|
btScalar ds, de;
|
|
|
|
int numVerts = pVtxIn.size();
|
|
|
|
if (numVerts < 2)
|
|
|
|
return;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 firstVertex = pVtxIn[pVtxIn.size() - 1];
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 endVertex = pVtxIn[0];
|
2019-01-03 13:26:51 +00:00
|
|
|
|
|
|
|
ds = planeNormalWS.dot(firstVertex) + planeEqWS;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
for (ve = 0; ve < numVerts; ve++)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
endVertex = pVtxIn[ve];
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
de = planeNormalWS.dot(endVertex) + planeEqWS;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (ds < 0)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (de < 0)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
// Start < 0, end < 0, so output endVertex
|
|
|
|
ppVtxOut.push_back(endVertex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Start < 0, end >= 0, so output intersection
|
2019-01-03 13:26:51 +00:00
|
|
|
ppVtxOut.push_back(firstVertex.lerp(endVertex, btScalar(ds * 1.f / (ds - de))));
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (de < 0)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
// Start >= 0, end < 0 so output intersection and end
|
2019-01-03 13:26:51 +00:00
|
|
|
ppVtxOut.push_back(firstVertex.lerp(endVertex, btScalar(ds * 1.f / (ds - de))));
|
2017-08-01 12:30:58 +00:00
|
|
|
ppVtxOut.push_back(endVertex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
firstVertex = endVertex;
|
|
|
|
ds = de;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
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)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar Min0, Max0;
|
|
|
|
btScalar Min1, Max1;
|
|
|
|
btVector3 witnesPtMinA, witnesPtMaxA;
|
|
|
|
btVector3 witnesPtMinB, witnesPtMaxB;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
hullA.project(transA, sep_axis, Min0, Max0, witnesPtMinA, witnesPtMaxA);
|
|
|
|
hullB.project(transB, sep_axis, Min1, Max1, witnesPtMinB, witnesPtMaxB);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (Max0 < Min1 || Max1 < Min0)
|
2017-08-01 12:30:58 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
btScalar d0 = Max0 - Min1;
|
2019-01-03 13:26:51 +00:00
|
|
|
btAssert(d0 >= 0.0f);
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar d1 = Max1 - Min0;
|
2019-01-03 13:26:51 +00:00
|
|
|
btAssert(d1 >= 0.0f);
|
|
|
|
if (d0 < d1)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
depth = d0;
|
|
|
|
witnessPointA = witnesPtMaxA;
|
|
|
|
witnessPointB = witnesPtMinB;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
depth = d1;
|
|
|
|
witnessPointA = witnesPtMinA;
|
|
|
|
witnessPointB = witnesPtMaxB;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
static int gActualSATPairTests = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
inline bool IsAlmostZero(const btVector3& v)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (btFabs(v.x()) > 1e-6 || btFabs(v.y()) > 1e-6 || btFabs(v.z()) > 1e-6) return false;
|
2017-08-01 12:30:58 +00:00
|
|
|
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.
|
2019-01-03 13:26:51 +00:00
|
|
|
// 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);
|
2017-08-01 12:30:58 +00:00
|
|
|
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];
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
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();
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
out.setValue(x, y, z);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bool TestInternalObjects(const btTransform& trans0, const btTransform& trans1, const btVector3& delta_c, const btVector3& axis, const btConvexPolyhedron& convex0, const btConvexPolyhedron& convex1, btScalar dmin)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btScalar dp = delta_c.dot(axis);
|
|
|
|
|
|
|
|
btVector3 localAxis0;
|
2019-01-03 13:26:51 +00:00
|
|
|
InverseTransformPoint3x3(localAxis0, axis, trans0);
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 localAxis1;
|
2019-01-03 13:26:51 +00:00
|
|
|
InverseTransformPoint3x3(localAxis1, axis, trans1);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
btScalar p0[3];
|
|
|
|
BoxSupport(convex0.m_extents, localAxis0, p0);
|
|
|
|
btScalar p1[3];
|
|
|
|
BoxSupport(convex1.m_extents, localAxis1, p1);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
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();
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
const btScalar MinRadius = Radius0 > convex0.m_radius ? Radius0 : convex0.m_radius;
|
|
|
|
const btScalar MaxRadius = Radius1 > convex1.m_radius ? Radius1 : convex1.m_radius;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
const btScalar MinMaxRadius = MaxRadius + MinRadius;
|
|
|
|
const btScalar d0 = MinMaxRadius + dp;
|
|
|
|
const btScalar d1 = MinMaxRadius - dp;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
const btScalar depth = d0 < d1 ? d0 : d1;
|
|
|
|
if (depth > dmin)
|
2017-08-01 12:30:58 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //TEST_INTERNAL_OBJECTS
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE void btSegmentsClosestPoints(
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3& ptsVector,
|
|
|
|
btVector3& offsetA,
|
|
|
|
btVector3& offsetB,
|
|
|
|
btScalar& tA, btScalar& tB,
|
|
|
|
const btVector3& translation,
|
|
|
|
const btVector3& dirA, btScalar hlenA,
|
2019-01-03 13:26:51 +00:00
|
|
|
const btVector3& dirB, btScalar hlenB)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
// compute the parameters of the closest points on each line segment
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar dirA_dot_dirB = btDot(dirA, dirB);
|
|
|
|
btScalar dirA_dot_trans = btDot(dirA, translation);
|
|
|
|
btScalar dirB_dot_trans = btDot(dirB, translation);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
btScalar denom = 1.0f - dirA_dot_dirB * dirA_dot_dirB;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (denom == 0.0f)
|
|
|
|
{
|
2017-08-01 12:30:58 +00:00
|
|
|
tA = 0.0f;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tA = (dirA_dot_trans - dirB_dot_trans * dirA_dot_dirB) / denom;
|
|
|
|
if (tA < -hlenA)
|
2017-08-01 12:30:58 +00:00
|
|
|
tA = -hlenA;
|
2019-01-03 13:26:51 +00:00
|
|
|
else if (tA > hlenA)
|
2017-08-01 12:30:58 +00:00
|
|
|
tA = hlenA;
|
|
|
|
}
|
|
|
|
|
|
|
|
tB = tA * dirA_dot_dirB - dirB_dot_trans;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (tB < -hlenB)
|
|
|
|
{
|
2017-08-01 12:30:58 +00:00
|
|
|
tB = -hlenB;
|
|
|
|
tA = tB * dirA_dot_dirB + dirA_dot_trans;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (tA < -hlenA)
|
2017-08-01 12:30:58 +00:00
|
|
|
tA = -hlenA;
|
2019-01-03 13:26:51 +00:00
|
|
|
else if (tA > hlenA)
|
2017-08-01 12:30:58 +00:00
|
|
|
tA = hlenA;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else if (tB > hlenB)
|
|
|
|
{
|
2017-08-01 12:30:58 +00:00
|
|
|
tB = hlenB;
|
|
|
|
tA = tB * dirA_dot_dirB + dirA_dot_trans;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (tA < -hlenA)
|
2017-08-01 12:30:58 +00:00
|
|
|
tA = -hlenA;
|
2019-01-03 13:26:51 +00:00
|
|
|
else if (tA > hlenA)
|
2017-08-01 12:30:58 +00:00
|
|
|
tA = hlenA;
|
|
|
|
}
|
|
|
|
|
|
|
|
// compute the closest points relative to segment centers.
|
|
|
|
|
|
|
|
offsetA = dirA * tA;
|
|
|
|
offsetB = dirB * tB;
|
|
|
|
|
|
|
|
ptsVector = translation - offsetA + offsetB;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bool btPolyhedralContactClipping::findSeparatingAxis(const btConvexPolyhedron& hullA, const btConvexPolyhedron& hullB, const btTransform& transA, const btTransform& transB, btVector3& sep, btDiscreteCollisionDetectorInterface::Result& resultOut)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
gActualSATPairTests++;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
//#ifdef TEST_INTERNAL_OBJECTS
|
2017-08-01 12:30:58 +00:00
|
|
|
const btVector3 c0 = transA * hullA.m_localCenter;
|
|
|
|
const btVector3 c1 = transB * hullB.m_localCenter;
|
|
|
|
const btVector3 DeltaC2 = c0 - c1;
|
2019-01-03 13:26:51 +00:00
|
|
|
//#endif
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
btScalar dmin = FLT_MAX;
|
2019-01-03 13:26:51 +00:00
|
|
|
int curPlaneTests = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
int numFacesA = hullA.m_faces.size();
|
|
|
|
// Test normals from hullA
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < numFacesA; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btVector3 Normal(hullA.m_faces[i].m_plane[0], hullA.m_faces[i].m_plane[1], hullA.m_faces[i].m_plane[2]);
|
|
|
|
btVector3 faceANormalWS = transA.getBasis() * Normal;
|
2019-01-03 13:26:51 +00:00
|
|
|
if (DeltaC2.dot(faceANormalWS) < 0)
|
|
|
|
faceANormalWS *= -1.f;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
curPlaneTests++;
|
|
|
|
#ifdef TEST_INTERNAL_OBJECTS
|
|
|
|
gExpectedNbTests++;
|
2019-01-03 13:26:51 +00:00
|
|
|
if (gUseInternalObject && !TestInternalObjects(transA, transB, DeltaC2, faceANormalWS, hullA, hullB, dmin))
|
2017-08-01 12:30:58 +00:00
|
|
|
continue;
|
|
|
|
gActualNbTests++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
btScalar d;
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 wA, wB;
|
|
|
|
if (!TestSepAxis(hullA, hullB, transA, transB, faceANormalWS, d, wA, wB))
|
2017-08-01 12:30:58 +00:00
|
|
|
return false;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (d < dmin)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
dmin = d;
|
|
|
|
sep = faceANormalWS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int numFacesB = hullB.m_faces.size();
|
|
|
|
// Test normals from hullB
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < numFacesB; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btVector3 Normal(hullB.m_faces[i].m_plane[0], hullB.m_faces[i].m_plane[1], hullB.m_faces[i].m_plane[2]);
|
|
|
|
btVector3 WorldNormal = transB.getBasis() * Normal;
|
2019-01-03 13:26:51 +00:00
|
|
|
if (DeltaC2.dot(WorldNormal) < 0)
|
|
|
|
WorldNormal *= -1.f;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
curPlaneTests++;
|
|
|
|
#ifdef TEST_INTERNAL_OBJECTS
|
|
|
|
gExpectedNbTests++;
|
2019-01-03 13:26:51 +00:00
|
|
|
if (gUseInternalObject && !TestInternalObjects(transA, transB, DeltaC2, WorldNormal, hullA, hullB, dmin))
|
2017-08-01 12:30:58 +00:00
|
|
|
continue;
|
|
|
|
gActualNbTests++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
btScalar d;
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 wA, wB;
|
|
|
|
if (!TestSepAxis(hullA, hullB, transA, transB, WorldNormal, d, wA, wB))
|
2017-08-01 12:30:58 +00:00
|
|
|
return false;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (d < dmin)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
dmin = d;
|
|
|
|
sep = WorldNormal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 edgeAstart, edgeAend, edgeBstart, edgeBend;
|
|
|
|
int edgeA = -1;
|
|
|
|
int edgeB = -1;
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 worldEdgeA;
|
|
|
|
btVector3 worldEdgeB;
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 witnessPointA(0, 0, 0), witnessPointB(0, 0, 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
int curEdgeEdge = 0;
|
|
|
|
// Test edges
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int e0 = 0; e0 < hullA.m_uniqueEdges.size(); e0++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btVector3 edge0 = hullA.m_uniqueEdges[e0];
|
|
|
|
const btVector3 WorldEdge0 = transA.getBasis() * edge0;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int e1 = 0; e1 < hullB.m_uniqueEdges.size(); e1++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btVector3 edge1 = hullB.m_uniqueEdges[e1];
|
|
|
|
const btVector3 WorldEdge1 = transB.getBasis() * edge1;
|
|
|
|
|
|
|
|
btVector3 Cross = WorldEdge0.cross(WorldEdge1);
|
|
|
|
curEdgeEdge++;
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!IsAlmostZero(Cross))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
Cross = Cross.normalize();
|
2019-01-03 13:26:51 +00:00
|
|
|
if (DeltaC2.dot(Cross) < 0)
|
2017-08-01 12:30:58 +00:00
|
|
|
Cross *= -1.f;
|
|
|
|
|
|
|
|
#ifdef TEST_INTERNAL_OBJECTS
|
|
|
|
gExpectedNbTests++;
|
2019-01-03 13:26:51 +00:00
|
|
|
if (gUseInternalObject && !TestInternalObjects(transA, transB, DeltaC2, Cross, hullA, hullB, dmin))
|
2017-08-01 12:30:58 +00:00
|
|
|
continue;
|
|
|
|
gActualNbTests++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
btScalar dist;
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 wA, wB;
|
|
|
|
if (!TestSepAxis(hullA, hullB, transA, transB, Cross, dist, wA, wB))
|
2017-08-01 12:30:58 +00:00
|
|
|
return false;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (dist < dmin)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
dmin = dist;
|
|
|
|
sep = Cross;
|
2019-01-03 13:26:51 +00:00
|
|
|
edgeA = e0;
|
|
|
|
edgeB = e1;
|
2017-08-01 12:30:58 +00:00
|
|
|
worldEdgeA = WorldEdge0;
|
|
|
|
worldEdgeB = WorldEdge1;
|
2019-01-03 13:26:51 +00:00
|
|
|
witnessPointA = wA;
|
|
|
|
witnessPointB = wB;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (edgeA >= 0 && edgeB >= 0)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
// printf("edge-edge\n");
|
2017-08-01 12:30:58 +00:00
|
|
|
//add an edge-edge contact
|
|
|
|
|
|
|
|
btVector3 ptsVector;
|
|
|
|
btVector3 offsetA;
|
|
|
|
btVector3 offsetB;
|
|
|
|
btScalar tA;
|
|
|
|
btScalar tB;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 translation = witnessPointB - witnessPointA;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
btVector3 dirA = worldEdgeA;
|
|
|
|
btVector3 dirB = worldEdgeB;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar hlenB = 1e30f;
|
|
|
|
btScalar hlenA = 1e30f;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btSegmentsClosestPoints(ptsVector, offsetA, offsetB, tA, tB,
|
|
|
|
translation,
|
|
|
|
dirA, hlenA,
|
|
|
|
dirB, hlenB);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
btScalar nlSqrt = ptsVector.length2();
|
2019-01-03 13:26:51 +00:00
|
|
|
if (nlSqrt > SIMD_EPSILON)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btScalar nl = btSqrt(nlSqrt);
|
2019-01-03 13:26:51 +00:00
|
|
|
ptsVector *= 1.f / nl;
|
|
|
|
if (ptsVector.dot(DeltaC2) < 0.f)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
ptsVector *= -1.f;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
btVector3 ptOnB = witnessPointB + offsetB;
|
|
|
|
btScalar distance = nl;
|
2019-01-03 13:26:51 +00:00
|
|
|
resultOut.addContactPoint(ptsVector, ptOnB, -distance);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if ((DeltaC2.dot(sep)) < 0.0f)
|
2017-08-01 12:30:58 +00:00
|
|
|
sep = -sep;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btPolyhedralContactClipping::clipFaceAgainstHull(const btVector3& separatingNormal, const btConvexPolyhedron& hullA, const btTransform& transA, btVertexArray& worldVertsB1, btVertexArray& worldVertsB2, const btScalar minDist, btScalar maxDist, btDiscreteCollisionDetectorInterface::Result& resultOut)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
worldVertsB2.resize(0);
|
|
|
|
btVertexArray* pVtxIn = &worldVertsB1;
|
|
|
|
btVertexArray* pVtxOut = &worldVertsB2;
|
|
|
|
pVtxOut->reserve(pVtxIn->size());
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
int closestFaceA = -1;
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btScalar dmin = FLT_MAX;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int face = 0; face < hullA.m_faces.size(); face++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btVector3 Normal(hullA.m_faces[face].m_plane[0], hullA.m_faces[face].m_plane[1], hullA.m_faces[face].m_plane[2]);
|
|
|
|
const btVector3 faceANormalWS = transA.getBasis() * Normal;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar d = faceANormalWS.dot(separatingNormal);
|
|
|
|
if (d < dmin)
|
|
|
|
{
|
|
|
|
dmin = d;
|
|
|
|
closestFaceA = face;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (closestFaceA < 0)
|
2017-08-01 12:30:58 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
const btFace& polyA = hullA.m_faces[closestFaceA];
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
// clip polygon to back of planes of all faces of hull A that are adjacent to witness face
|
2017-08-01 12:30:58 +00:00
|
|
|
int numVerticesA = polyA.m_indices.size();
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int e0 = 0; e0 < numVerticesA; e0++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btVector3& a = hullA.m_vertices[polyA.m_indices[e0]];
|
2019-01-03 13:26:51 +00:00
|
|
|
const btVector3& b = hullA.m_vertices[polyA.m_indices[(e0 + 1) % numVerticesA]];
|
2017-08-01 12:30:58 +00:00
|
|
|
const btVector3 edge0 = a - b;
|
|
|
|
const btVector3 WorldEdge0 = transA.getBasis() * edge0;
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 worldPlaneAnormal1 = transA.getBasis() * btVector3(polyA.m_plane[0], polyA.m_plane[1], polyA.m_plane[2]);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 planeNormalWS1 = -WorldEdge0.cross(worldPlaneAnormal1); //.cross(WorldEdge0);
|
|
|
|
btVector3 worldA1 = transA * a;
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar planeEqWS1 = -worldA1.dot(planeNormalWS1);
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
//int otherFace=0;
|
|
|
|
#ifdef BLA1
|
|
|
|
int otherFace = polyA.m_connectedFaces[e0];
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 localPlaneNormal(hullA.m_faces[otherFace].m_plane[0], hullA.m_faces[otherFace].m_plane[1], hullA.m_faces[otherFace].m_plane[2]);
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar localPlaneEq = hullA.m_faces[otherFace].m_plane[3];
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 planeNormalWS = transA.getBasis() * localPlaneNormal;
|
|
|
|
btScalar planeEqWS = localPlaneEq - planeNormalWS.dot(transA.getOrigin());
|
|
|
|
#else
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 planeNormalWS = planeNormalWS1;
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar planeEqWS = planeEqWS1;
|
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
#endif
|
|
|
|
//clip face
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
clipFace(*pVtxIn, *pVtxOut, planeNormalWS, planeEqWS);
|
|
|
|
btSwap(pVtxIn, pVtxOut);
|
2017-08-01 12:30:58 +00:00
|
|
|
pVtxOut->resize(0);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
//#define ONLY_REPORT_DEEPEST_POINT
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
btVector3 point;
|
|
|
|
|
|
|
|
// only keep points that are behind the witness face
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 localPlaneNormal(polyA.m_plane[0], polyA.m_plane[1], polyA.m_plane[2]);
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar localPlaneEq = polyA.m_plane[3];
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 planeNormalWS = transA.getBasis() * localPlaneNormal;
|
|
|
|
btScalar planeEqWS = localPlaneEq - planeNormalWS.dot(transA.getOrigin());
|
|
|
|
for (int i = 0; i < pVtxIn->size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btVector3 vtx = pVtxIn->at(i);
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar depth = planeNormalWS.dot(vtx) + planeEqWS;
|
|
|
|
if (depth <= minDist)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
// printf("clamped: depth=%f to minDist=%f\n",depth,minDist);
|
2017-08-01 12:30:58 +00:00
|
|
|
depth = minDist;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (depth <= maxDist)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
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");
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
resultOut.addContactPoint(separatingNormal, point, depth);
|
2017-08-01 12:30:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef ONLY_REPORT_DEEPEST_POINT
|
2019-01-03 13:26:51 +00:00
|
|
|
if (curMaxDist < maxDist)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
resultOut.addContactPoint(separatingNormal, point, curMaxDist);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //ONLY_REPORT_DEEPEST_POINT
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btPolyhedralContactClipping::clipHullAgainstHull(const btVector3& separatingNormal1, const btConvexPolyhedron& hullA, const btConvexPolyhedron& hullB, const btTransform& transA, const btTransform& transB, const btScalar minDist, btScalar maxDist, btVertexArray& worldVertsB1, btVertexArray& worldVertsB2, btDiscreteCollisionDetectorInterface::Result& resultOut)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btVector3 separatingNormal = separatingNormal1.normalized();
|
2019-01-03 13:26:51 +00:00
|
|
|
// const btVector3 c0 = transA * hullA.m_localCenter;
|
|
|
|
// const btVector3 c1 = transB * hullB.m_localCenter;
|
2017-08-01 12:30:58 +00:00
|
|
|
//const btVector3 DeltaC2 = c0 - c1;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
int closestFaceB = -1;
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar dmax = -FLT_MAX;
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int face = 0; face < hullB.m_faces.size(); face++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btVector3 Normal(hullB.m_faces[face].m_plane[0], hullB.m_faces[face].m_plane[1], hullB.m_faces[face].m_plane[2]);
|
|
|
|
const btVector3 WorldNormal = transB.getBasis() * Normal;
|
|
|
|
btScalar d = WorldNormal.dot(separatingNormal);
|
|
|
|
if (d > dmax)
|
|
|
|
{
|
|
|
|
dmax = d;
|
|
|
|
closestFaceB = face;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
worldVertsB1.resize(0);
|
2019-01-03 13:26:51 +00:00
|
|
|
{
|
|
|
|
const btFace& polyB = hullB.m_faces[closestFaceB];
|
|
|
|
const int numVertices = polyB.m_indices.size();
|
|
|
|
for (int e0 = 0; e0 < numVertices; e0++)
|
|
|
|
{
|
|
|
|
const btVector3& b = hullB.m_vertices[polyB.m_indices[e0]];
|
|
|
|
worldVertsB1.push_back(transB * b);
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (closestFaceB >= 0)
|
|
|
|
clipFaceAgainstHull(separatingNormal, hullA, transA, worldVertsB1, worldVertsB2, minDist, maxDist, resultOut);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|