What is the best approach to 2D collision detection on the iPhone?

Posted by Magic Bullet Dave on Stack Overflow See other posts from Stack Overflow or by Magic Bullet Dave
Published on 2010-04-19T07:57:29Z Indexed on 2010/04/19 8:03 UTC
Read the original article Hit count: 274

Been working on this problem of collision detection and there appears to be 3 main approaches I could take:

  1. Sprite and mask approach. (AND the overlap of the sprites and check for a non-zero number in the resulting sprite pixel data).

  2. Bounding circles, rectangles or polygons. (Create one or more shapes that enclose the sprites and do the basic maths to check for overlaps).

  3. Use an existing sprite library.

The first approach, even though it would have been the way I would have done it in the old days of 16x16 sprite blocks, it appears that there just isn’t an easy way of getting at the individual image pixel data and/or alpha channel within Quartz (or OPENGL for that matter). Detecting the overlap of the bounding box is easy, but then creating a 3rd image from the overlap and then testing it for pixels is complicated and my gut feel is that even if we could get it to work would be slow. Am I missing something neat here?

The second approach involves dividing up our sprites into several polygons and testing them for overlaps. The more polygons the more accurate the collision detection. The benefit is that it is fast, and can be accurate. The downside is it makes the sprite creation more complicated. i.e., we have to create the polygons for each sprite. For speed the best approach is to create a tree of polygons.

The 3rd approach I’m not sure about as it involves buying code (or using an open source licence). I am not sure what the best library to use is or whether this would make life easier or give us a problem integrating this into our app.

So in short I am favouring the polygon and tree approach and would appreciate you views on this before I go and write lots of code.

Best regards

Dave

© Stack Overflow or respective owner

Related posts about iphone

Related posts about collision-detection