Jump to content
  • Advertisement
Sign in to follow this  
lukesmith123

2D AABB Contains

This topic is 2344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
How about this:
[source lang="cpp"]return A.x1< = B.x2 && A.x2 > =B.x1 && A.y1< =B.y2 && A.y2> =B.y1;[/source] Edited by Ashaman73

Share this post


Link to post
Share on other sites
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;
Edited by lukesmith123

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!