Shortest Distance from inside of fixture/shape

GJK does a fantastic job of finding the shortest distance from any kind of shape to any other kind of shape.

However I'd like to go one step further to obtain the shortest distance from a "small shape" to any point on the outline of a "large shape" which completely "encloses" the small shape. It needs only to work for convex shapes (don't care about concave).

GJK produces a result distance of 0 when one of the shapes is intersecting the other. I'd like for the case of one shape completely encompassing another to also provide a distance value.

I drew this to illustrate:

Image

The first case is what I want: the orange line is the shortest distance from body B to body A's boundary.

The second case is where there is intersection and I am not interested in this case (but will need to be able to detect it of course)

and the third case is already solved to a completely satisfactory degree by GJK.


Since this is mostly for debug purposes right now, neither performance nor accuracy is a significant concern. I was thinking of just doing a limited number of raycasts from my inner shape, but even the raycast functionality does not really address the issue of casting from within a shape! This means that to implement a raycast based solution to this would be probably almost as difficult as implementing a completely accurate solution by extending GJK somehow or at least using some of its central concepts.

So my question is, what might be the easiest or most straightforward way to obtain this "containment distance" value that I'm looking for? I realize it might be simple to obtain a "separation" distance in the second case as illustrated above but it actually provides no information as to how far completely "submerged" the smaller shape is.
I think it's pretty obvious that the sane solution is to use GJK to perform segment-to-polygon tests on the segments which make up the containing shape. In essense, inverting that shape by forcing the algorithm to consider each of its edges separately.

This is plenty efficient as the polygon-to-polygon GJK can be thought of as a "broadphase" step: Only if the poly-to-poly test returns a distance of zero and the shapes are in fact determined to intersect does this "containment distance" even make sense to be computed.

The question now is whether I can just give a degenerate two-vertex polyshape to the b2Distance function and have it gracefully do what I intend for it to do (tell me the segment-to-poly distance).

But wait! That's silly because Box2D already has great support for edge shapes and edge chains. This should mean that there are functions readily available for me to (probably very efficiently) query the distance from arbitrary line segments to polygons! Solved!

Update: There are very clear explanations of edge shapes and how they work, and it is also quite clear that b2Distance (GJK) is used with CCD with edge chain shapes so there is no reason why I can't build an edge chain in the same shape as my fixtures. In fact this is clearly the proper way to implement a "hollow" object that still has volume: it can be simulated as a regular body by using solid shapes, and it can be hollowed out and collided with internally by using edge shapes and perhaps a bit of collision filtering.

Original thread: https://box2d.org/forum/viewtopic.php?t=8850