Comments and 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 comments and answers for the question "How to detect the distance between two edges (2D sprites)"Comment by Jesse Anders on Jesse Anders's answer
http://answers.unity.com/comments/96125/view.html
Ok, the updated question is a different question than the first one. What you have now is essentially a line intersection problem. How to find the intersection points between a linear component (line, ray, or segment) and an axis-aligned box is pretty well documented online. I can tell you that it's been discussed many times on GDNet, so if you search the forums there for e.g. 'ray aabb intersection' (or similar phrases), you should be able to find some good references and perhaps some example code.Wed, 12 Jan 2011 20:08:43 GMTJesse AndersComment by ibyte on ibyte's answer
http://answers.unity.com/comments/96105/view.html
Hi Jesse, thanks for the reply. I agree the question is not Unity specific but certainly applicable to game development :) I updated the question slightly.Wed, 12 Jan 2011 18:06:21 GMTibyteAnswer 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