Pixel perfect Collision Detection

2068b9d739ccb5184d90f2ab0d217051
0
jbhargav1992 102 Jan 31, 2014 at 17:30

Hi,

I am developing a 2d platformer game using opengl.I managed to display sprites and the background image and I have given movements to the character now I need to implement collisions. Can anyone help me out on how to achieve pixel perfect collision detection? I have searched lot of websites and it’s a bit confusing.

10 Replies

Please log in or register to post a reply.

B9c51b24b655b8afcf9f399049e57f52
2
geekymonkey 103 Feb 01, 2014 at 08:14

Pixel perfect is expensive, so limit when you do it. Do bounding box collision detection first. If that passes, then start pixel perfect detection of the 2 sprites.

loop over all the pixels of one sprite. is the texture there not transparent? If so, calculate the screen position (or world position) of the center of that pixel. Taking into account position, rotation, scale. using that screen position, find the corresponding pixel in the 2nd sprite. If its within the bounds of that sprite, then get the texture pixel. If its not transparent, you have a hit!

Do it simply as above. Create a test scene with oddly shaped sprites spinning or that you can move around. Have them change colors when collision is detected.

once you get that working, then think of ways to optimize things. Exit your loops on the first hit found. Limit the math in the loops to simple adding and multplying by precalculating delta vales for the pixel to next pixel location calculation. You want what’s inside the nested loop to be as quick as possible.

B5262118b588a5a420230bfbef4a2cdf
1
Stainless 151 Feb 01, 2014 at 11:13

Or use polygon collision.

It takes a lot of work to create the collision polygons, you have to write an app to edit your sprite sequences and create a collision polygon for each frame, but you can make it as complex as you want.

I have in the past created collision polygons for individual body parts so I could do location based reactions.

2068b9d739ccb5184d90f2ab0d217051
0
jbhargav1992 102 Feb 04, 2014 at 04:57

OK… I think this is the more ideal method as it uses the concept of bounding box and gives the accuracy close to pixel perfect collisions. Thank you for your suggestion. I have one doubt, though… How would I go about creating collision polygons for my sprites? Please clarify this for me.

Thanks.

B5262118b588a5a420230bfbef4a2cdf
1
Stainless 151 Feb 04, 2014 at 09:57

You either write an app that allows you to create the polygons by hand, or you look online for a tool you can download.

The Torque 2D engine uses this technique, maybe you can grab a free version of that and use the editor to create the polygons.

2068b9d739ccb5184d90f2ab0d217051
0
jbhargav1992 102 Feb 05, 2014 at 04:44

Thanks, Stainless… I found this tool that can auto trace polygons and save a lot of time: PhysicsEditor. The only thing I don’t understand is what format to save it to in order to use it in OpenGL. It has several formats, .plist, .xml, .flash, etc. Some of them are Box2D formats. And I know Box2D can be used with OpenGL. Just not sure which format to use. Please tell me. Thank you.

B5262118b588a5a420230bfbef4a2cdf
1
Stainless 151 Feb 05, 2014 at 10:12

Just have a look here and decide if you are going to use any of the engines

http://www.codeandweb.com/physicseditor/tutorials

2068b9d739ccb5184d90f2ab0d217051
0
jbhargav1992 102 Feb 05, 2014 at 11:55

I’ve seen that page, but I am not sure how to use Box2D with OpenGL in C++. All the tutorials on the internet show Box2D used in collaboration with Cocos2D, which is, if I’m right, a full game engine. I don’t want to use Cocos2D. I want to implement Box2D and write my own collisions in C++. How would I do that?

B5262118b588a5a420230bfbef4a2cdf
1
Stainless 151 Feb 05, 2014 at 14:43

Box2d tutorials show how to do your own renderers.

It’s really easy.

Just look at those.

2068b9d739ccb5184d90f2ab0d217051
0
jbhargav1992 102 Feb 05, 2014 at 16:14

OK… Thanks, Stainless. I found some tutorials. I’ll be looking through those. :-)

D08b0a4acfcc7da7d9dd15715c42445c
2
Dim_Yimma_H 104 Feb 02, 2014 at 18:18

Maybe you would not prefer pixel perfect detection, in the end, there are easier methods. Try the bounding box collision detection first and see if it’s enough.

bool Box::Overlaps(Box another)
{
  if (bottom > another.top && top < another.bottom) //i.e. if the boxes overlap vertically
  {
    if (right > another.left && left < another.right)
      return true;	//and the boxes overlap horizontally too
  }
  return false;
}

If you have near rectangular sprites like humans, cars, tiles, it’s usually easy to specify the box left,top,bottom, right sides so they feel pixel perfect upon overlap detection.