Archived

This topic is now archived and is closed to further replies.

Rotated Rectangles Collisions

This topic is 5876 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

Hello. How can I check for collision between two rotated rectangles? They''re defind like this: struct _RotRect { float x, y, // Position w, h, // Halved size (real size is (2*w, 2*h) ) ang; // Angle of rotation }; Regards, John Spitz.

Share this post


Link to post
Share on other sites
There''s another thread about this except that it''s in 3D. I think you should be able to adept those methods to 2D. If not, I''m sure someone else will be able to help you.

http://www.gamedev.net/community/forums/topic.asp?topic_id=63228

Share this post


Link to post
Share on other sites
People suggest me to conduct 8 "Point In Rotated Rectangle" tests (for each vertex). This is not a perfect method, but it might be enough for me.
The problem is that so many checks are too slow.

Is there another idea?


Thanks,
John Spitz.

Share this post


Link to post
Share on other sites
You can find a great tutorial on the subject here.
http://www.gamasutra.com/features/19991018/Gomez_5.htm
Only difference with what you want is that it''s 3D but that should be easy to fix. (All the explanation pictures are in 2D anyway.) Also the tutorial uses the local axis of the bounding boxes instead of rotation angles. But you can build those from the angles:
axis.x = cos(ang)
axis.y = sin(ang)
(assuming that an angle of 0 points along the positive x-axis)

Share this post


Link to post
Share on other sites
There are many equations for calculating the intersection point of two straight lines. Could you not use such an algorithm and check each line in one rectangle against all the lines in the other rectangle?

If you don''t get any intersections, the rectangles do not cross each other.

Ooh, you might also have to check for one rectangle being contained within the other, if this is ever possible.

It''s worth checking out the FAQ for comp.graphics.algorithms, there are lots of useful algorithms like that.

Share this post


Link to post
Share on other sites