erincatto / box2d

Box2D is a 2D physics engine for games
https://box2d.org
MIT License
8.05k stars 1.51k forks source link

Distance bug for overlapping circles #672

Closed elemel closed 2 years ago

elemel commented 3 years ago

Looks like the returned points are off when computing the distance between two overlapping circle shapes with different radii. This can happen even when the overlap is very small, such as for touching shapes. From what I can see, the problem is that the radii aren't considered in this case, instead the average of the centers is used. The result is correct as long as there is no overlap.

Repro, changed distance test in testbed: https://github.com/elemel/box2d/pull/1/commits/5a8bebf8f92a3ea108dfa46c47f34cef29f3d8d8

Screenshot:

overlapping-circles

Expected: Yellow dot roughly in the center of the overlapping region. Actual: Yellow dot is the average of the circle centers.

franklange commented 3 years ago

Mhmh.. looking at this part of the b2Distance code it looks like a deliberate choice?https://github.com/erincatto/box2d/blob/95f74a46c077825478f989df6a81003ca538c2c2/src/collision/b2_distance.cpp#L590

erincatto commented 2 years ago

Addressed in 8e32f3e3cf96c3dfb5a78189bd3c6e80a40716e1

The closest points now move smoothly while keeping the logic simple.