Skip to main content
gamedev.net
🔒 Locked

fast rectangle intersection

Started by streamer Sep 2, 2006 at 3:25 AM 8 replies 11.8k views
Original Post
streamer
streamer
Hello all. Can you please tell me which is the fastest way to check if two rectangles intersect in c++? I have resolved problem with bunch of if-s, but that doesn't seem to be the right approach.
Captain P
Captain P
Are these axis-aligned boxes, or can they be rotated?

In the case of axis-aligned ones, there are at most four checks you need to perform:
1. Check if box A is completely to the left of box B (a.left <= b.right). If so, then quit the process: they don't intersect.
2. Otherwise, check if it's completely to the right (a.right >= b.left). If so, quit the process.
3. If not, check if it's completely above box B (a.bottom >= b.top). If so, quit.
4. If not, check if it's completely beneath box B (a.top <= b.bottom). If so, quit.
If not, then the boxes intersect.

This method is in fact a simplification of the Separating Axis Theorem.
streamer
streamer
Yes they are.
I tried a different approach, than that you mentioned. I checked for if x,y points of rectangle are inside of another, and if so then they intersect. But that wasn't perfect. Also your approach is simplier.

And if boxes aren't axis-aligned? How can I check that?
Also how can I check if right-angled triangle and axis-aligned box intersect?
Zakwayda
Zakwayda
Quote:
Original post by streamer
And if boxes aren't axis-aligned? How can I check that?
Also how can I check if right-angled triangle and axis-aligned box intersect?
As Captain P mentioned, the axis-aligned rect test is a special case of the separating axis theorem. The generalized version of the SAT test can be used for both the oriented box and triangle vs. box tests you mention above.
streamer
streamer
Quote:
Original post by jyk
Quote:
Original post by streamer
And if boxes aren't axis-aligned? How can I check that?
Also how can I check if right-angled triangle and axis-aligned box intersect?
As Captain P mentioned, the axis-aligned rect test is a special case of the separating axis theorem. The generalized version of the SAT test can be used for both the oriented box and triangle vs. box tests you mention above.


I'm sorry but can I have some link with teoretic part on separating axis theorem, or in better case some c++ code? I'll apreciate that.

Thanks in advance.
Zakwayda
Zakwayda
Quote:
Original post by streamer
I'm sorry but can I have some link with teoretic part on separating axis theorem, or in better case some c++ code? I'll apreciate that.
Just google 'separating axis theorem' and you'll get a bunch of hits. Also try geometrictools.com.
streamer
streamer
Quote:
Original post by Captain P
I've found N Tutorials - Collision Detection and Response to be a very insightfull article. It even contains Flashy examples to visualize the theory.


That was what I searched for. Thank you very much!!!!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.