• Create Account

## 2D AABB Contains

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1lukesmith123  Members

153
Like
0Likes
Like

Posted 18 July 2012 - 03:49 AM

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?

### #2RulerOfNothing  Members

1369
Like
2Likes
Like

Posted 18 July 2012 - 04:52 AM

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:
• 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.
• Repeat step 1, but for the y-axis instead of the x-axis.
• Multiply the results from step 1 and step 2 to get the area of intersection.

### #3Ashaman73  Members

13651
Like
0Likes
Like

Posted 18 July 2012 - 05:06 AM

[source lang="cpp"]return A.x1< = B.x2 && A.x2 > =B.x1 && A.y1< =B.y2 && A.y2> =B.y1;[/source]

Edited by Ashaman73, 18 July 2012 - 05:07 AM.

Ashaman

### #4lukesmith123  Members

153
Like
0Likes
Like

Posted 18 July 2012 - 05:14 PM

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, 18 July 2012 - 05:15 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.