2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
#ifndef B3_CONTACT_SPHERE_SPHERE_H
|
|
|
|
#define B3_CONTACT_SPHERE_SPHERE_H
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void computeContactSphereConvex(int pairIndex,
|
|
|
|
int bodyIndexA, int bodyIndexB,
|
|
|
|
int collidableIndexA, int collidableIndexB,
|
|
|
|
const b3RigidBodyData* rigidBodies,
|
|
|
|
const b3Collidable* collidables,
|
|
|
|
const b3ConvexPolyhedronData* convexShapes,
|
|
|
|
const b3Vector3* convexVertices,
|
|
|
|
const int* convexIndices,
|
|
|
|
const b3GpuFace* faces,
|
|
|
|
b3Contact4* globalContactsOut,
|
|
|
|
int& nGlobalContactsOut,
|
|
|
|
int maxContactCapacity)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
float radius = collidables[collidableIndexA].m_radius;
|
|
|
|
float4 spherePos1 = rigidBodies[bodyIndexA].m_pos;
|
|
|
|
b3Quaternion sphereOrn = rigidBodies[bodyIndexA].m_quat;
|
|
|
|
|
|
|
|
float4 pos = rigidBodies[bodyIndexB].m_pos;
|
|
|
|
|
|
|
|
b3Quaternion quat = rigidBodies[bodyIndexB].m_quat;
|
|
|
|
|
|
|
|
b3Transform tr;
|
|
|
|
tr.setIdentity();
|
|
|
|
tr.setOrigin(pos);
|
|
|
|
tr.setRotation(quat);
|
|
|
|
b3Transform trInv = tr.inverse();
|
|
|
|
|
|
|
|
float4 spherePos = trInv(spherePos1);
|
|
|
|
|
|
|
|
int collidableIndex = rigidBodies[bodyIndexB].m_collidableIdx;
|
|
|
|
int shapeIndex = collidables[collidableIndex].m_shapeIndex;
|
|
|
|
int numFaces = convexShapes[shapeIndex].m_numFaces;
|
|
|
|
float4 closestPnt = b3MakeVector3(0, 0, 0, 0);
|
|
|
|
float4 hitNormalWorld = b3MakeVector3(0, 0, 0, 0);
|
2019-01-03 14:26:51 +01:00
|
|
|
float minDist = -1000000.f; // TODO: What is the largest/smallest float?
|
2017-08-01 14:30:58 +02:00
|
|
|
bool bCollide = true;
|
|
|
|
int region = -1;
|
|
|
|
float4 localHitNormal;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int f = 0; f < numFaces; f++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
b3GpuFace face = faces[convexShapes[shapeIndex].m_faceOffset + f];
|
2017-08-01 14:30:58 +02:00
|
|
|
float4 planeEqn;
|
2019-01-03 14:26:51 +01:00
|
|
|
float4 localPlaneNormal = b3MakeVector3(face.m_plane.x, face.m_plane.y, face.m_plane.z, 0.f);
|
|
|
|
float4 n1 = localPlaneNormal; //quatRotate(quat,localPlaneNormal);
|
2017-08-01 14:30:58 +02:00
|
|
|
planeEqn = n1;
|
|
|
|
planeEqn[3] = face.m_plane.w;
|
|
|
|
|
|
|
|
float4 pntReturn;
|
|
|
|
float dist = signedDistanceFromPointToPlane(spherePos, planeEqn, &pntReturn);
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (dist > radius)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
bCollide = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (dist > 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
//might hit an edge or vertex
|
|
|
|
b3Vector3 out;
|
|
|
|
|
|
|
|
bool isInPoly = IsPointInPolygon(spherePos,
|
2019-01-03 14:26:51 +01:00
|
|
|
&face,
|
|
|
|
&convexVertices[convexShapes[shapeIndex].m_vertexOffset],
|
|
|
|
convexIndices,
|
|
|
|
&out);
|
2017-08-01 14:30:58 +02:00
|
|
|
if (isInPoly)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (dist > minDist)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
minDist = dist;
|
|
|
|
closestPnt = pntReturn;
|
|
|
|
localHitNormal = planeEqn;
|
2019-01-03 14:26:51 +01:00
|
|
|
region = 1;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
b3Vector3 tmp = spherePos - out;
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Scalar l2 = tmp.length2();
|
2019-01-03 14:26:51 +01:00
|
|
|
if (l2 < radius * radius)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
dist = b3Sqrt(l2);
|
|
|
|
if (dist > minDist)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
minDist = dist;
|
|
|
|
closestPnt = out;
|
2019-01-03 14:26:51 +01:00
|
|
|
localHitNormal = tmp / dist;
|
|
|
|
region = 2;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
bCollide = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (dist > minDist)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
minDist = dist;
|
|
|
|
closestPnt = pntReturn;
|
|
|
|
localHitNormal = planeEqn;
|
2019-01-03 14:26:51 +01:00
|
|
|
region = 3;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static int numChecks = 0;
|
|
|
|
numChecks++;
|
|
|
|
|
|
|
|
if (bCollide && minDist > -10000)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
float4 normalOnSurfaceB1 = tr.getBasis() * localHitNormal; //-hitNormalWorld;
|
2017-08-01 14:30:58 +02:00
|
|
|
float4 pOnB1 = tr(closestPnt);
|
|
|
|
//printf("dist ,%f,",minDist);
|
2019-01-03 14:26:51 +01:00
|
|
|
float actualDepth = minDist - radius;
|
|
|
|
if (actualDepth < 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
//printf("actualDepth = ,%f,", actualDepth);
|
|
|
|
//printf("normalOnSurfaceB1 = ,%f,%f,%f,", normalOnSurfaceB1.x,normalOnSurfaceB1.y,normalOnSurfaceB1.z);
|
|
|
|
//printf("region=,%d,\n", region);
|
|
|
|
pOnB1[3] = actualDepth;
|
|
|
|
|
|
|
|
int dstIdx;
|
|
|
|
// dstIdx = nGlobalContactsOut++;//AppendInc( nGlobalContactsOut, dstIdx );
|
|
|
|
|
|
|
|
if (nGlobalContactsOut < maxContactCapacity)
|
|
|
|
{
|
|
|
|
dstIdx = nGlobalContactsOut;
|
|
|
|
nGlobalContactsOut++;
|
|
|
|
|
|
|
|
b3Contact4* c = &globalContactsOut[dstIdx];
|
|
|
|
c->m_worldNormalOnB = normalOnSurfaceB1;
|
|
|
|
c->setFrictionCoeff(0.7);
|
|
|
|
c->setRestituitionCoeff(0.f);
|
|
|
|
|
|
|
|
c->m_batchIdx = pairIndex;
|
|
|
|
c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass == 0 ? -bodyIndexA : bodyIndexA;
|
|
|
|
c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass == 0 ? -bodyIndexB : bodyIndexB;
|
|
|
|
c->m_worldPosB[0] = pOnB1;
|
|
|
|
int numPoints = 1;
|
|
|
|
c->m_worldNormalOnB.w = (b3Scalar)numPoints;
|
|
|
|
} //if (dstIdx < numPairs)
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
} //if (hasCollision)
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //B3_CONTACT_SPHERE_SPHERE_H
|