Advertisement

2D AABB Contains

Started by July 18, 2012 09:49 AM
2 comments, last by lukesmith123 12 years, 7 months ago
Whats the fastest way to test containment between two 2D AABBs?




Also I'm testing for intersections like this:


return (Abs(Corners[0].X - B.GetCorners[0].X) * 2 < (Corners[1].X + B.GetCorners[1].X)) &&
(Math.Abs(Corners[0].Y - B.GetCorners[0].Y) * 2 < (Corners[1].Y + B.GetCorners[1].Y));




if I always need to check for intersections as well as containment is there a better method I could use to check both at the same time more cheaply?
You could return the area of intersection rather than a simple boolean result; this would allow you to easily check if one AABB contained the other (you would check to see if the area of intersection was equal to the area of the smaller AABB). To get the area of intersection of two AABBs you can use the following algorithm:

  1. Subtract the distance between the leftmost part and the rightmost part of the two AABBs from the sum of the widths of the two AABBs. If this number is negative, set it to zero.
  2. Repeat step 1, but for the y-axis instead of the x-axis.
  3. Multiply the results from step 1 and step 2 to get the area of intersection.
Advertisement
How about this:
[source lang="cpp"]return A.x1< = B.x2 && A.x2 > =B.x1 && A.y1< =B.y2 && A.y2> =B.y1;[/source]
Thanks for the replies.

The area of intersection sum is really useful.

I found some good stuff in a book and I think the fastest method when using min/max is separating axis:


if (Max.X < B.Min.X || Min.X > B.Max.X) return false;
if (Max.Y < B.Min.X || Min.Y > B.Max.Y) return false;

return true;

This topic is closed to new replies.

Advertisement