Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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.

  • You cannot reply to this topic
3 replies to this topic

#1 lukesmith123   Members   -  Reputation: 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?

Sponsor:

#2 RulerOfNothing   Members   -  Reputation: 1162

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.


#3 Ashaman73   Crossbones+   -  Reputation: 7793

Like
0Likes
Like

Posted 18 July 2012 - 05:06 AM

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, 18 July 2012 - 05:07 AM.


#4 lukesmith123   Members   -  Reputation: 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.



PARTNERS