# 2D Rotated Rectangle Collision

This topic is 2776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok. I've been searching around for a couple of days now, I've tried writing my own code, and I've tried using code I found. I'm stuck on this. I should mention that I have relatively little knowledge of math and physics, which is probably why I'm so stuck on this.

I've got a 2D game with sprites that walk around, represented physically as AABBs. I usually just check for collisions with those. But I've added the ability for items to rotate, so for instance an arrow can be shot in a direction, and the image can be rotated to the direction the arrow is traveling. This is easy enough in OpenGL. However, I realized I also need to test for collisions using this rotated version of the rectangle, instead of the actual AABB. So I have been trying to construct a "check for collisions but rotate the rectangles by their angles first" function ever since.

I have figured out that I need to use the Separating Axis Theorem. I am able to give the collision function anything it needs. The game already knows the top-left corner of the AABB, its width/height, and its rotation angle. Anything else, like the center point is trivial to get. Unfortunately, this is where I'm stuck. The actual meat of the whole thing, heh.

I actually found one piece of code that I was able to use which worked ALMOST. It worked for some angles. It worked pretty well for 180 degrees, but was way off for 0 degrees. This was with a rotated item vs. a non-rotated (always 0 degrees) level tile.

Is there some kind of nifty open library someone's made to do this, per chance? I don't need any kind of collision data, just a simple true/false collision test. Barring a library (drat!), could anyone either explain this, or point me somewhere that will explain this, with "for dummies" simplicity?

##### Share on other sites

Is there some kind of nifty open library someone's made to do this, per chance? I don't need any kind of collision data, just a simple true/false collision test. Barring a library (drat!), could anyone either explain this, or point me somewhere that will explain this, with "for dummies" simplicity?

Try this page . The Test() function in Wm5IntrBox2Box2.cpp implements the Separating Axis Test for two stationary boxes. It is easily modified if your boxes are moving with constant linear velocities.

##### Share on other sites

Is there some kind of nifty open library someone's made to do this, per chance? I don't need any kind of collision data, just a simple true/false collision test. Barring a library (drat!), could anyone either explain this, or point me somewhere that will explain this, with "for dummies" simplicity?

I wrote an article about 2d collision detection, have you seen it already?

http://www.wildbunny.co.uk/blog/2011/04/20/collision-detection-for-dummies/

Cheers, Paul.

##### Share on other sites
Thanks for the suggestions, guys. I realized that items only moved in 8 directions, so I just cheated :-D

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013550
×