2
\$\begingroup\$

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

I am currently using AABBs, but the problem with this, is that your objects will be hitting things when they shouldn't. On top of this, AABBs cannot be rotated.

I was considering using per pixel collision, but I feel that it would lag on large bitmaps, since there would be a lot of pixels to check.

What I would like to know, is how I can allow my bounding boxes to be rotated, and use any polygon instead of just rectangles for collision. Also, is there a way of creating circular collision bounds that aren't just approximations of circles?

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Why would you use atan2() for circle vs. circle? \$\endgroup\$
    – bummzack
    Feb 20, 2012 at 10:18
  • \$\begingroup\$ 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. \$\endgroup\$
    – mdkess
    Feb 20, 2012 at 15:23

1 Answer 1

6
\$\begingroup\$

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.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .