# how to create 2D collision detection

I would like to know the best or most effective way to test for 2D collision.

I also can do AABBs but when you have a line, for example, that is rotated 45ยบ, and it is really long. it will be hitting things when it shouldn't.

I might be able to go through the pixels to see if they are touching others, but that might be slow if I had a big picture. and it might add some complications if I had a movie clip made of several images.

How do I check collision between two Images? How would I do circle to box? Please help : )

PS: I do know java so you can write with java syntax and then use a made up GL

-
Why would you use `atan2()` for circle vs. circle? – bummzack Feb 20 '12 at 10:18
For circle circle collision, all that you have to do is check whether the distance between the centers is less than the sum of the radiuses. Also, avoid atan2() if you can - the function is incredibly slow. – mdkess Feb 20 '12 at 15:23
@mdkess thanks for the tip : ) – Aidan Mueller Mar 7 '12 at 17:27

So it depends on what you are trying to do.

If you want something simple, I recommend stick with axis aligned AABBs for most things - they make collision detection really easy.

Generally when you're writing collision code you'll have to write specialized cases for all pairs of shapes (ie. line-line, line-circle, line-box, circle-line, circle-box, circle-circle, box-box).

For 2D, the main principle is called the Separating Axis Theorem. This is an excellent guide to it.

Typically another trick that people use is to have multiple layers of collision detection to speed things up. Circle-Circle collision might be expensive, but if you check whether the two circles AABBs are overlapping first, you can potentially save a lot of time.

If you are really wanting to have more advanced physics and response, I would recommend that you go with an already made physics engine. JBox2D is a port of the popular Box2D engine, and is generally quite good. That said, I would still recommend reading through those two articles so that you have an understanding of what is going on behind the scenes with collision detection and response.

-
 Thank you so much – Aidan Mueller Feb 20 '12 at 5:09 BTW I don't want to use a preexisting library. I would have just done that to begin with. – Aidan Mueller Feb 20 '12 at 5:11