Pixel perfect collision

Like in topic. I want to make very accurate collision system, so I'm trying to use pixel perfect collision. I think I tried to find a solution almost everywhere, but I haven't found any satisfactory solution. I am writing in Java.

This algorithm works well, but it has one very big drawback - I can only specify the number of pixels that have collided, but not collision coordinates. Outside that, I'm not sure this will work outside screen.

Now the question - is it possible to make pixel perfect collision (or at least very accurate collision detection for irregular shapes) and get coordinates of pixel(s) that have collided? If yes, I would be very grateful if someone will give a link to examples or show how can I handle this problem.

I wouldn't recommend using an occlusion query for this. It is very slow. But then again, pixel perfect collision detection in general is very slow anyways.

I may not be able to change your mind, but you may want to reconsider using pixel perfect collision detection. It is not commonly used, especially because it is very slow. It is better to use a real physics library for collisions, where you can specify hulls to match shapes (there are even some graphical tools for this) like Box2D: http://box2d.org/

Reading pixels from screen and from a textures (i.e. framebuffer attachment) is essentially the same, only when using FBOs you read from texture memory while otherwise you read from memory provided by the underlying window system
also called the default framebuffer (DFB - note that this abbreviation is NOT common, I just use it here).

You can use glReadPixels() for this purpose. This will read from any color, the depth and/or the stencil attachment in a FBO or the DFB.

Originally Posted by Warlander

I want to read pixel coordinates from occlusion query.

You can't read anything from an occlusion query. You can get the result of a query - which, in case of occlusion queries, is either the number of samples passed (GL_ARB_occlusion_query) or a boolean value if any samples passed (GL_ARB_occlusion_query2).

Why are you doing this? 2D collision dectection on the CPU isn't that much of a miracle and frankly, I doubt using occlusion queries brute force, then reading back pixels from the stencil buffer and searching for collision coordinates is in any way faster than simple computation on the CPU.