2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
#ifndef B3_CONTACT_CONVEX_CONVEX_SAT_H
|
|
|
|
#define B3_CONTACT_CONVEX_CONVEX_SAT_H
|
|
|
|
|
|
|
|
#include "Bullet3Collision/NarrowPhaseCollision/shared/b3Contact4Data.h"
|
|
|
|
#include "Bullet3Collision/NarrowPhaseCollision/shared/b3FindSeparatingAxis.h"
|
|
|
|
#include "Bullet3Collision/NarrowPhaseCollision/shared/b3ReduceContacts.h"
|
|
|
|
|
|
|
|
#define B3_MAX_VERTS 1024
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
inline b3Float4 b3Lerp3(const b3Float4& a, const b3Float4& b, float t)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
return b3MakeVector3(a.x + (b.x - a.x) * t,
|
|
|
|
a.y + (b.y - a.y) * t,
|
|
|
|
a.z + (b.z - a.z) * t,
|
|
|
|
0.f);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clips a face to the back of a plane, return the number of vertices out, stored in ppVtxOut
|
2019-01-03 13:26:51 +00:00
|
|
|
inline int b3ClipFace(const b3Float4* pVtxIn, int numVertsIn, b3Float4& planeNormalWS, float planeEqWS, b3Float4* ppVtxOut)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int ve;
|
|
|
|
float ds, de;
|
|
|
|
int numVertsOut = 0;
|
|
|
|
if (numVertsIn < 2)
|
|
|
|
return 0;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Float4 firstVertex = pVtxIn[numVertsIn - 1];
|
2017-08-01 12:30:58 +00:00
|
|
|
b3Float4 endVertex = pVtxIn[0];
|
2019-01-03 13:26:51 +00:00
|
|
|
|
|
|
|
ds = b3Dot3F4(planeNormalWS, firstVertex) + planeEqWS;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
for (ve = 0; ve < numVertsIn; 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 = b3Dot3F4(planeNormalWS, 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[numVertsOut++] = endVertex;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Start < 0, end >= 0, so output intersection
|
2019-01-03 13:26:51 +00:00
|
|
|
ppVtxOut[numVertsOut++] = b3Lerp3(firstVertex, endVertex, (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[numVertsOut++] = b3Lerp3(firstVertex, endVertex, (ds * 1.f / (ds - de)));
|
2017-08-01 12:30:58 +00:00
|
|
|
ppVtxOut[numVertsOut++] = endVertex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
firstVertex = endVertex;
|
|
|
|
ds = de;
|
|
|
|
}
|
|
|
|
return numVertsOut;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
inline int b3ClipFaceAgainstHull(const b3Float4& separatingNormal, const b3ConvexPolyhedronData* hullA,
|
|
|
|
const b3Float4& posA, const b3Quaternion& ornA, b3Float4* worldVertsB1, int numWorldVertsB1,
|
|
|
|
b3Float4* worldVertsB2, int capacityWorldVertsB2,
|
|
|
|
const float minDist, float maxDist,
|
|
|
|
const b3AlignedObjectArray<b3Float4>& verticesA, const b3AlignedObjectArray<b3GpuFace>& facesA, const b3AlignedObjectArray<int>& indicesA,
|
|
|
|
//const b3Float4* verticesB, const b3GpuFace* facesB, const int* indicesB,
|
|
|
|
b3Float4* contactsOut,
|
|
|
|
int contactCapacity)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int numContactsOut = 0;
|
|
|
|
|
|
|
|
b3Float4* pVtxIn = worldVertsB1;
|
|
|
|
b3Float4* pVtxOut = worldVertsB2;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
int numVertsIn = numWorldVertsB1;
|
|
|
|
int numVertsOut = 0;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
int closestFaceA = -1;
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
float dmin = FLT_MAX;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int face = 0; face < hullA->m_numFaces; face++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const b3Float4 Normal = b3MakeVector3(
|
2019-01-03 13:26:51 +00:00
|
|
|
facesA[hullA->m_faceOffset + face].m_plane.x,
|
|
|
|
facesA[hullA->m_faceOffset + face].m_plane.y,
|
|
|
|
facesA[hullA->m_faceOffset + face].m_plane.z, 0.f);
|
|
|
|
const b3Float4 faceANormalWS = b3QuatRotate(ornA, Normal);
|
|
|
|
|
|
|
|
float d = b3Dot3F4(faceANormalWS, separatingNormal);
|
2017-08-01 12:30:58 +00:00
|
|
|
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 numContactsOut;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3GpuFace polyA = facesA[hullA->m_faceOffset + closestFaceA];
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
// clip polygon to back of planes of all faces of hull A that are adjacent to witness face
|
|
|
|
//int numContacts = numWorldVertsB1;
|
|
|
|
int numVerticesA = polyA.m_numIndices;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int e0 = 0; e0 < numVerticesA; e0++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
const b3Float4 a = verticesA[hullA->m_vertexOffset + indicesA[polyA.m_indexOffset + e0]];
|
|
|
|
const b3Float4 b = verticesA[hullA->m_vertexOffset + indicesA[polyA.m_indexOffset + ((e0 + 1) % numVerticesA)]];
|
2017-08-01 12:30:58 +00:00
|
|
|
const b3Float4 edge0 = a - b;
|
2019-01-03 13:26:51 +00:00
|
|
|
const b3Float4 WorldEdge0 = b3QuatRotate(ornA, edge0);
|
|
|
|
b3Float4 planeNormalA = b3MakeFloat4(polyA.m_plane.x, polyA.m_plane.y, polyA.m_plane.z, 0.f);
|
|
|
|
b3Float4 worldPlaneAnormal1 = b3QuatRotate(ornA, planeNormalA);
|
|
|
|
|
|
|
|
b3Float4 planeNormalWS1 = -b3Cross3(WorldEdge0, worldPlaneAnormal1);
|
|
|
|
b3Float4 worldA1 = b3TransformPoint(a, posA, ornA);
|
|
|
|
float planeEqWS1 = -b3Dot3F4(worldA1, planeNormalWS1);
|
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
b3Float4 planeNormalWS = planeNormalWS1;
|
2019-01-03 13:26:51 +00:00
|
|
|
float planeEqWS = planeEqWS1;
|
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
//clip face
|
|
|
|
//clipFace(*pVtxIn, *pVtxOut,planeNormalWS,planeEqWS);
|
2019-01-03 13:26:51 +00:00
|
|
|
numVertsOut = b3ClipFace(pVtxIn, numVertsIn, planeNormalWS, planeEqWS, pVtxOut);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//btSwap(pVtxIn,pVtxOut);
|
|
|
|
b3Float4* tmp = pVtxOut;
|
|
|
|
pVtxOut = pVtxIn;
|
|
|
|
pVtxIn = tmp;
|
|
|
|
numVertsIn = numVertsOut;
|
|
|
|
numVertsOut = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only keep points that are behind the witness face
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Float4 localPlaneNormal = b3MakeFloat4(polyA.m_plane.x, polyA.m_plane.y, polyA.m_plane.z, 0.f);
|
2017-08-01 12:30:58 +00:00
|
|
|
float localPlaneEq = polyA.m_plane.w;
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Float4 planeNormalWS = b3QuatRotate(ornA, localPlaneNormal);
|
|
|
|
float planeEqWS = localPlaneEq - b3Dot3F4(planeNormalWS, posA);
|
|
|
|
for (int i = 0; i < numVertsIn; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
float depth = b3Dot3F4(planeNormalWS, pVtxIn[i]) + planeEqWS;
|
|
|
|
if (depth <= minDist)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
depth = minDist;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (numContactsOut < contactCapacity)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (depth <= maxDist)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
b3Float4 pointInWorld = pVtxIn[i];
|
|
|
|
//resultOut.addContactPoint(separatingNormal,point,depth);
|
2019-01-03 13:26:51 +00:00
|
|
|
contactsOut[numContactsOut++] = b3MakeVector3(pointInWorld.x, pointInWorld.y, pointInWorld.z, depth);
|
2017-08-01 12:30:58 +00:00
|
|
|
//printf("depth=%f\n",depth);
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Error("exceeding contact capacity (%d,%df)\n", numContactsOut, contactCapacity);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return numContactsOut;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
inline int b3ClipHullAgainstHull(const b3Float4& separatingNormal,
|
|
|
|
const b3ConvexPolyhedronData& hullA, const b3ConvexPolyhedronData& hullB,
|
|
|
|
const b3Float4& posA, const b3Quaternion& ornA, const b3Float4& posB, const b3Quaternion& ornB,
|
|
|
|
b3Float4* worldVertsB1, b3Float4* worldVertsB2, int capacityWorldVerts,
|
|
|
|
const float minDist, float maxDist,
|
|
|
|
const b3AlignedObjectArray<b3Float4>& verticesA, const b3AlignedObjectArray<b3GpuFace>& facesA, const b3AlignedObjectArray<int>& indicesA,
|
|
|
|
const b3AlignedObjectArray<b3Float4>& verticesB, const b3AlignedObjectArray<b3GpuFace>& facesB, const b3AlignedObjectArray<int>& indicesB,
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Float4* contactsOut,
|
|
|
|
int contactCapacity)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int numContactsOut = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
int numWorldVertsB1 = 0;
|
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
B3_PROFILE("clipHullAgainstHull");
|
|
|
|
|
|
|
|
//float curMaxDist=maxDist;
|
2019-01-03 13:26:51 +00:00
|
|
|
int closestFaceB = -1;
|
2017-08-01 12:30:58 +00:00
|
|
|
float dmax = -FLT_MAX;
|
|
|
|
|
|
|
|
{
|
|
|
|
//B3_PROFILE("closestFaceB");
|
2019-01-03 13:26:51 +00:00
|
|
|
if (hullB.m_numFaces != 1)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//printf("wtf\n");
|
|
|
|
}
|
|
|
|
static bool once = true;
|
|
|
|
//printf("separatingNormal=%f,%f,%f\n",separatingNormal.x,separatingNormal.y,separatingNormal.z);
|
2019-01-03 13:26:51 +00:00
|
|
|
|
|
|
|
for (int face = 0; face < hullB.m_numFaces; face++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
#ifdef BT_DEBUG_SAT_FACE
|
|
|
|
if (once)
|
2019-01-03 13:26:51 +00:00
|
|
|
printf("face %d\n", face);
|
|
|
|
const b3GpuFace* faceB = &facesB[hullB.m_faceOffset + face];
|
2017-08-01 12:30:58 +00:00
|
|
|
if (once)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < faceB->m_numIndices; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Float4 vert = verticesB[hullB.m_vertexOffset + indicesB[faceB->m_indexOffset + i]];
|
|
|
|
printf("vert[%d] = %f,%f,%f\n", i, vert.x, vert.y, vert.z);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //BT_DEBUG_SAT_FACE \
|
|
|
|
//if (facesB[hullB.m_faceOffset+face].m_numIndices>2)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
const b3Float4 Normal = b3MakeVector3(facesB[hullB.m_faceOffset + face].m_plane.x,
|
|
|
|
facesB[hullB.m_faceOffset + face].m_plane.y, facesB[hullB.m_faceOffset + face].m_plane.z, 0.f);
|
2017-08-01 12:30:58 +00:00
|
|
|
const b3Float4 WorldNormal = b3QuatRotate(ornB, Normal);
|
|
|
|
#ifdef BT_DEBUG_SAT_FACE
|
|
|
|
if (once)
|
2019-01-03 13:26:51 +00:00
|
|
|
printf("faceNormal = %f,%f,%f\n", Normal.x, Normal.y, Normal.z);
|
2017-08-01 12:30:58 +00:00
|
|
|
#endif
|
2019-01-03 13:26:51 +00:00
|
|
|
float d = b3Dot3F4(WorldNormal, separatingNormal);
|
2017-08-01 12:30:58 +00:00
|
|
|
if (d > dmax)
|
|
|
|
{
|
|
|
|
dmax = d;
|
|
|
|
closestFaceB = face;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
once = false;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Assert(closestFaceB >= 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//B3_PROFILE("worldVertsB1");
|
2019-01-03 13:26:51 +00:00
|
|
|
const b3GpuFace& polyB = facesB[hullB.m_faceOffset + closestFaceB];
|
2017-08-01 12:30:58 +00:00
|
|
|
const int numVertices = polyB.m_numIndices;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int e0 = 0; e0 < numVertices; e0++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
const b3Float4& b = verticesB[hullB.m_vertexOffset + indicesB[polyB.m_indexOffset + e0]];
|
|
|
|
worldVertsB1[numWorldVertsB1++] = b3TransformPoint(b, posB, ornB);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (closestFaceB >= 0)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//B3_PROFILE("clipFaceAgainstHull");
|
2019-01-03 13:26:51 +00:00
|
|
|
numContactsOut = b3ClipFaceAgainstHull((b3Float4&)separatingNormal, &hullA,
|
|
|
|
posA, ornA,
|
|
|
|
worldVertsB1, numWorldVertsB1, worldVertsB2, capacityWorldVerts, minDist, maxDist,
|
|
|
|
verticesA, facesA, indicesA,
|
|
|
|
contactsOut, contactCapacity);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return numContactsOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int b3ClipHullHullSingle(
|
2019-01-03 13:26:51 +00:00
|
|
|
int bodyIndexA, int bodyIndexB,
|
|
|
|
const b3Float4& posA,
|
|
|
|
const b3Quaternion& ornA,
|
|
|
|
const b3Float4& posB,
|
|
|
|
const b3Quaternion& ornB,
|
|
|
|
|
|
|
|
int collidableIndexA, int collidableIndexB,
|
|
|
|
|
|
|
|
const b3AlignedObjectArray<b3RigidBodyData>* bodyBuf,
|
|
|
|
b3AlignedObjectArray<b3Contact4Data>* globalContactOut,
|
|
|
|
int& nContacts,
|
|
|
|
|
|
|
|
const b3AlignedObjectArray<b3ConvexPolyhedronData>& hostConvexDataA,
|
|
|
|
const b3AlignedObjectArray<b3ConvexPolyhedronData>& hostConvexDataB,
|
|
|
|
|
|
|
|
const b3AlignedObjectArray<b3Vector3>& verticesA,
|
|
|
|
const b3AlignedObjectArray<b3Vector3>& uniqueEdgesA,
|
|
|
|
const b3AlignedObjectArray<b3GpuFace>& facesA,
|
|
|
|
const b3AlignedObjectArray<int>& indicesA,
|
|
|
|
|
|
|
|
const b3AlignedObjectArray<b3Vector3>& verticesB,
|
|
|
|
const b3AlignedObjectArray<b3Vector3>& uniqueEdgesB,
|
|
|
|
const b3AlignedObjectArray<b3GpuFace>& facesB,
|
|
|
|
const b3AlignedObjectArray<int>& indicesB,
|
|
|
|
|
|
|
|
const b3AlignedObjectArray<b3Collidable>& hostCollidablesA,
|
|
|
|
const b3AlignedObjectArray<b3Collidable>& hostCollidablesB,
|
|
|
|
const b3Vector3& sepNormalWorldSpace,
|
|
|
|
int maxContactCapacity)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int contactIndex = -1;
|
|
|
|
b3ConvexPolyhedronData hullA, hullB;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
|
|
|
b3Collidable colA = hostCollidablesA[collidableIndexA];
|
|
|
|
hullA = hostConvexDataA[colA.m_shapeIndex];
|
|
|
|
//printf("numvertsA = %d\n",hullA.m_numVertices);
|
|
|
|
|
|
|
|
b3Collidable colB = hostCollidablesB[collidableIndexB];
|
|
|
|
hullB = hostConvexDataB[colB.m_shapeIndex];
|
|
|
|
//printf("numvertsB = %d\n",hullB.m_numVertices);
|
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
b3Float4 contactsOut[B3_MAX_VERTS];
|
|
|
|
int localContactCapacity = B3_MAX_VERTS;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
b3Assert(_finite(bodyBuf->at(bodyIndexA).m_pos.x));
|
|
|
|
b3Assert(_finite(bodyBuf->at(bodyIndexB).m_pos.x));
|
|
|
|
#endif
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
b3Float4 worldVertsB1[B3_MAX_VERTS];
|
|
|
|
b3Float4 worldVertsB2[B3_MAX_VERTS];
|
|
|
|
int capacityWorldVerts = B3_MAX_VERTS;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Float4 hostNormal = b3MakeFloat4(sepNormalWorldSpace.x, sepNormalWorldSpace.y, sepNormalWorldSpace.z, 0.f);
|
2017-08-01 12:30:58 +00:00
|
|
|
int shapeA = hostCollidablesA[collidableIndexA].m_shapeIndex;
|
|
|
|
int shapeB = hostCollidablesB[collidableIndexB].m_shapeIndex;
|
|
|
|
|
|
|
|
b3Scalar minDist = -1;
|
|
|
|
b3Scalar maxDist = 0.;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Transform trA, trB;
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
//B3_PROFILE("b3TransformPoint computation");
|
|
|
|
//trA.setIdentity();
|
|
|
|
trA.setOrigin(b3MakeVector3(posA.x, posA.y, posA.z));
|
|
|
|
trA.setRotation(b3Quaternion(ornA.x, ornA.y, ornA.z, ornA.w));
|
|
|
|
|
|
|
|
//trB.setIdentity();
|
|
|
|
trB.setOrigin(b3MakeVector3(posB.x, posB.y, posB.z));
|
|
|
|
trB.setRotation(b3Quaternion(ornB.x, ornB.y, ornB.z, ornB.w));
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b3Quaternion trAorn = trA.getRotation();
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Quaternion trBorn = trB.getRotation();
|
|
|
|
|
|
|
|
int numContactsOut = b3ClipHullAgainstHull(hostNormal,
|
|
|
|
hostConvexDataA.at(shapeA),
|
|
|
|
hostConvexDataB.at(shapeB),
|
|
|
|
(b3Float4&)trA.getOrigin(), (b3Quaternion&)trAorn,
|
|
|
|
(b3Float4&)trB.getOrigin(), (b3Quaternion&)trBorn,
|
|
|
|
worldVertsB1, worldVertsB2, capacityWorldVerts,
|
|
|
|
minDist, maxDist,
|
|
|
|
verticesA, facesA, indicesA,
|
|
|
|
verticesB, facesB, indicesB,
|
|
|
|
|
|
|
|
contactsOut, localContactCapacity);
|
|
|
|
|
|
|
|
if (numContactsOut > 0)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
B3_PROFILE("overlap");
|
|
|
|
|
|
|
|
b3Float4 normalOnSurfaceB = (b3Float4&)hostNormal;
|
2019-01-03 13:26:51 +00:00
|
|
|
// b3Float4 centerOut;
|
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
b3Int4 contactIdx;
|
|
|
|
contactIdx.x = 0;
|
|
|
|
contactIdx.y = 1;
|
|
|
|
contactIdx.z = 2;
|
|
|
|
contactIdx.w = 3;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
int numPoints = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
B3_PROFILE("extractManifold");
|
2019-01-03 13:26:51 +00:00
|
|
|
numPoints = b3ReduceContacts(contactsOut, numContactsOut, normalOnSurfaceB, &contactIdx);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
b3Assert(numPoints);
|
2019-01-03 13:26:51 +00:00
|
|
|
|
|
|
|
if (nContacts < maxContactCapacity)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
contactIndex = nContacts;
|
|
|
|
globalContactOut->expand();
|
|
|
|
b3Contact4Data& contact = globalContactOut->at(nContacts);
|
2019-01-03 13:26:51 +00:00
|
|
|
contact.m_batchIdx = 0; //i;
|
|
|
|
contact.m_bodyAPtrAndSignBit = (bodyBuf->at(bodyIndexA).m_invMass == 0) ? -bodyIndexA : bodyIndexA;
|
|
|
|
contact.m_bodyBPtrAndSignBit = (bodyBuf->at(bodyIndexB).m_invMass == 0) ? -bodyIndexB : bodyIndexB;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
contact.m_frictionCoeffCmp = 45874;
|
|
|
|
contact.m_restituitionCoeffCmp = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
|
|
|
// float distance = 0.f;
|
|
|
|
for (int p = 0; p < numPoints; p++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
contact.m_worldPosB[p] = contactsOut[contactIdx.s[p]]; //check if it is actually on B
|
|
|
|
contact.m_worldNormalOnB = normalOnSurfaceB;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
//printf("bodyIndexA %d,bodyIndexB %d,normal=%f,%f,%f numPoints %d\n",bodyIndexA,bodyIndexB,normalOnSurfaceB.x,normalOnSurfaceB.y,normalOnSurfaceB.z,numPoints);
|
|
|
|
contact.m_worldNormalOnB.w = (b3Scalar)numPoints;
|
|
|
|
nContacts++;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Error("Error: exceeding contact capacity (%d/%d)\n", nContacts, maxContactCapacity);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return contactIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int b3ContactConvexConvexSAT(
|
2019-01-03 13:26:51 +00:00
|
|
|
int pairIndex,
|
|
|
|
int bodyIndexA, int bodyIndexB,
|
|
|
|
int collidableIndexA, int collidableIndexB,
|
|
|
|
const b3AlignedObjectArray<b3RigidBodyData>& rigidBodies,
|
|
|
|
const b3AlignedObjectArray<b3Collidable>& collidables,
|
|
|
|
const b3AlignedObjectArray<b3ConvexPolyhedronData>& convexShapes,
|
|
|
|
const b3AlignedObjectArray<b3Float4>& convexVertices,
|
|
|
|
const b3AlignedObjectArray<b3Float4>& uniqueEdges,
|
|
|
|
const b3AlignedObjectArray<int>& convexIndices,
|
|
|
|
const b3AlignedObjectArray<b3GpuFace>& faces,
|
|
|
|
b3AlignedObjectArray<b3Contact4Data>& globalContactsOut,
|
|
|
|
int& nGlobalContactsOut,
|
|
|
|
int maxContactCapacity)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int contactIndex = -1;
|
|
|
|
|
|
|
|
b3Float4 posA = rigidBodies[bodyIndexA].m_pos;
|
|
|
|
b3Quaternion ornA = rigidBodies[bodyIndexA].m_quat;
|
|
|
|
b3Float4 posB = rigidBodies[bodyIndexB].m_pos;
|
|
|
|
b3Quaternion ornB = rigidBodies[bodyIndexB].m_quat;
|
|
|
|
|
|
|
|
b3ConvexPolyhedronData hullA, hullB;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Float4 sepNormalWorldSpace;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Collidable colA = collidables[collidableIndexA];
|
|
|
|
hullA = convexShapes[colA.m_shapeIndex];
|
|
|
|
//printf("numvertsA = %d\n",hullA.m_numVertices);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
b3Collidable colB = collidables[collidableIndexB];
|
|
|
|
hullB = convexShapes[colB.m_shapeIndex];
|
|
|
|
//printf("numvertsB = %d\n",hullB.m_numVertices);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
b3Assert(_finite(rigidBodies[bodyIndexA].m_pos.x));
|
|
|
|
b3Assert(_finite(rigidBodies[bodyIndexB].m_pos.x));
|
|
|
|
#endif
|
2019-01-03 13:26:51 +00:00
|
|
|
|
|
|
|
bool foundSepAxis = b3FindSeparatingAxis(hullA, hullB,
|
|
|
|
posA,
|
|
|
|
ornA,
|
|
|
|
posB,
|
|
|
|
ornB,
|
|
|
|
|
|
|
|
convexVertices, uniqueEdges, faces, convexIndices,
|
|
|
|
convexVertices, uniqueEdges, faces, convexIndices,
|
|
|
|
|
|
|
|
sepNormalWorldSpace);
|
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
if (foundSepAxis)
|
|
|
|
{
|
|
|
|
contactIndex = b3ClipHullHullSingle(
|
|
|
|
bodyIndexA, bodyIndexB,
|
2019-01-03 13:26:51 +00:00
|
|
|
posA, ornA,
|
|
|
|
posB, ornB,
|
2017-08-01 12:30:58 +00:00
|
|
|
collidableIndexA, collidableIndexB,
|
2019-01-03 13:26:51 +00:00
|
|
|
&rigidBodies,
|
2017-08-01 12:30:58 +00:00
|
|
|
&globalContactsOut,
|
|
|
|
nGlobalContactsOut,
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
convexShapes,
|
|
|
|
convexShapes,
|
2019-01-03 13:26:51 +00:00
|
|
|
|
|
|
|
convexVertices,
|
|
|
|
uniqueEdges,
|
2017-08-01 12:30:58 +00:00
|
|
|
faces,
|
|
|
|
convexIndices,
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
convexVertices,
|
|
|
|
uniqueEdges,
|
|
|
|
faces,
|
|
|
|
convexIndices,
|
|
|
|
|
|
|
|
collidables,
|
|
|
|
collidables,
|
|
|
|
sepNormalWorldSpace,
|
|
|
|
maxContactCapacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
return contactIndex;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //B3_CONTACT_CONVEX_CONVEX_SAT_H
|