Solved edge case for sphere vs convex polyhedron

This commit is contained in:
Diren D Bharwani 2023-01-01 02:42:44 +08:00
parent 3a7336fe15
commit 00f8726e46
2 changed files with 72 additions and 72 deletions

View File

@ -5,7 +5,7 @@
Components:
Transform Component:
Translate: {x: 2.72256827, y: 0.501797795, z: -0.0273017883}
Rotate: {x: -1.48352849, y: -4.78713309e-06, z: 0.469859391}
Rotate: {x: 0, y: 0, z: 0.436332315}
Scale: {x: 4.61070776, y: 0.99999392, z: 0.999996722}
IsActive: true
RigidBody Component:
@ -66,7 +66,7 @@
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: -2.49145222, y: 6.17996597, z: 0.811235607}
Translate: {x: -2.01111817, y: 7, z: 0.695722342}
Rotate: {x: -0, y: 0, z: -0}
Scale: {x: 1, y: 1, z: 1}
IsActive: true
@ -112,7 +112,7 @@
Components:
Transform Component:
Translate: {x: 0, y: 4.09544182, z: 0}
Rotate: {x: -0, y: 0, z: 0}
Rotate: {x: -0, y: 0, z: -0.436332315}
Scale: {x: 4.61071014, y: 0.999995887, z: 1}
IsActive: true
RigidBody Component:

View File

@ -73,6 +73,7 @@ namespace SHADE
contact.featurePair.key = 0;
// Check if center is inside polyhedron (below the face)
// TODO: Change to a point in polygon test.
if (FACE_QUERY.bestDistance < SHMath::EPSILON)
{
manifold.normal = -POLYHEDRON.GetNormal(FACE_QUERY.closestFace);
@ -116,18 +117,17 @@ namespace SHADE
// 2. Same side as adjacent normal
// Check in regions A
{
const int32_t P2_INDEX = (CLOSEST_POINT + 1) % NUM_VERTICES;
const SHVec3 P2 = POLYHEDRON.GetVertex(FACE.vertexIndices[P2_INDEX].index);
const SHVec3 TANGENT = SHVec3::Normalise(P2 - P1);
SHVec3 tangent = SHVec3::Normalise(P2 - P1);
float projection = SHVec3::Dot(P1_TO_CENTER, TANGENT);
float projection = SHVec3::Dot(P1_TO_CENTER, tangent);
if (projection >= 0.0f)
{
// Find closest point
const SHVec3 CP = P1 + projection * TANGENT;
const SHVec3 CP = P1 + projection * tangent;
// Check 2nd condition
// Get adjacent normal from twin half edge
@ -157,21 +157,19 @@ namespace SHADE
return true;
}
}
}
// Check in region B
{
const int32_t P3_INDEX = CLOSEST_POINT == 0 ? NUM_VERTICES - 1 : CLOSEST_POINT - 1;
const SHVec3 P3 = POLYHEDRON.GetVertex(FACE.vertexIndices[P3_INDEX].index);
const SHVec3 TANGENT = SHVec3::Normalise(P3 - P1);
tangent = SHVec3::Normalise(P3 - P1);
float projection = SHVec3::Dot(P1_TO_CENTER, TANGENT);
projection = SHVec3::Dot(P1_TO_CENTER, tangent);
if (projection >= 0.0f)
{
// Find closest point
const SHVec3 CP = P1 + projection * TANGENT;
const SHVec3 CP = P1 + projection * tangent;
// Check 2nd condition
// Get adjacent normal from twin half edge
@ -201,26 +199,28 @@ namespace SHADE
return true;
}
}
}
// Either region C or D. Take whichever depth is smaller
// Region C has a negative dot product with any of the tangents.
const float C_PENETRATION = RADIUS - P1_TO_CENTER.Length();
if (std::fabs(C_PENETRATION) < RADIUS && std::fabs(C_PENETRATION) < PENETRATION)
projection = SHVec3::Dot(P1_TO_CENTER, tangent);
if (projection < 0)
{
manifold.normal = -SHVec3::Normalise(P1_TO_CENTER);
contact.penetration = C_PENETRATION;
contact.penetration = RADIUS - P1_TO_CENTER.Length();
contact.position = P1;
manifold.contacts[numContacts++] = contact;
manifold.numContacts = numContacts;
return true;
}
else
{
// Region D
manifold.normal = -FACE_NORMAL;
contact.penetration = PENETRATION;
contact.position = CENTER - FACE_NORMAL * RADIUS;
}
manifold.contacts[numContacts++] = contact;
manifold.numContacts = numContacts;