Rotated Rectangles Collisions

Started by
4 comments, last by JohnSpitz 22 years, 5 months ago
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.
Advertisement
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
Dirk =[Scarab]= Gerrits
Anything more specific, anyone?
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.
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)
Dirk =[Scarab]= Gerrits
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.
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan

This topic is closed to new replies.

Advertisement