public static void RefinePenetration(ConvexShape shapeA, ConvexShape shapeB, ref RigidTransform localTransformB, float initialDepth, ref Vector3 initialNormal, out float penetrationDepth, out Vector3 refinedNormal, out Vector3 position)
{
//The local casting can optionally continue. Eventually, it will converge to the local minimum.
int optimizingCount = 0;
refinedNormal = initialNormal;
penetrationDepth = initialDepth;
float candidateDepth;
Vector3 candidateNormal;
while (true)
{
MPRToolbox.LocalSurfaceCast(shapeA, shapeB, ref localTransformB, ref refinedNormal, out candidateDepth, out candidateNormal, out position);
if (penetrationDepth - candidateDepth <= depthRefinementEpsilon ||
++optimizingCount >= maximumDepthRefinementIterations)
{
//If we've reached the end due to convergence, the normal will be extremely close to correct (if not 100% correct).
//The candidateDepth computed is the previous contact normal's depth.
//The reason why the previous normal is kept is that the last raycast computed the depth for that normal, not the new normal.
penetrationDepth = candidateDepth;
break;
}
penetrationDepth = candidateDepth;
refinedNormal = candidateNormal;
}
}