Jump to content
  • Advertisement
Sign in to follow this  
Dark_Oppressor

2D Rotated Rectangle Collision

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

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!