Answers for "How to detect the distance between two edges (2D sprites)"
http://answers.unity.com/questions/42517/how-to-detect-the-distance-between-two-edges-2d-sp.html
The latest answers for the question "How to detect the distance between two edges (2D sprites)"Answer by Jesse Anders
http://answers.unity.com/answers/42519/view.html
<p>First, note that this isn't really a Unity question (as there's nothing Unity-specific about it). I'm sure you'll get some good answers here, but for this sort of question you might have as good or better luck on a general game development forum such as gamedev.net.</p>
<p>Anyway, computing the shortest distance between two axis-aligned rectangles isn't too difficult. There are really only two cases you have to consider: the first case is when each box lies fully within a 'corner' Voronoi region of the other box, and the second case is when they don't (in other words, the inverse of the first case).</p>
<p>Here's an example of the first case:</p>
<pre><code>----
| |
----
----
| |
----
</code></pre>
<p>In this case, the closest points are the box corners corresponding to the regions in question, and the shortest distance is the distance between those two points.</p>
<p>Here's an example of the second case:</p>
<pre><code>----
| | ----
| | | |
---- | |
----
</code></pre>
<p>In this case the shortest distance is the perpendicular distance between the closest edges (there is no unique pair of closest points).</p>
<p>The test itself then reduces to classifying one box in relation to the other to determine which Voronoi region(s) of the other box it intersects, and then computing the distance accordingly. The implementation can be a little involved due to the number of specific cases that have to be handled, but the algorithm itself is fairly straightforward.</p>Wed, 12 Jan 2011 16:44:27 GMTJesse Anders