Detect pixel perfect collision location

Started by Mac70, December 04, 2012, 16:38:56

Previous topic - Next topic

Mac70

Like in topic. I have made pixel perfect colision like this (after running bounding box test):

protected boolean accurateTest(int object1, int object2) {
        GL11.glScissor((int) (-Graphics.SCREEN_X*2.5), (int) (-Graphics.SCREEN_Y*2.5), Graphics.SCREEN_X*5, Graphics.SCREEN_Y*5);
        GL11.glClear(GL11.GL_STENCIL_BUFFER_BIT);

        GL11.glStencilFunc(GL11.GL_ALWAYS, 1, 1);
        GL11.glStencilOp(GL11.GL_REPLACE, GL11.GL_REPLACE, GL11.GL_REPLACE);

        Core.List.get(object1).Draw(false);

        GL11.glStencilFunc(GL11.GL_EQUAL, 1, 1);
        GL11.glStencilOp(GL11.GL_KEEP, GL11.GL_KEEP, GL11.GL_KEEP);

        GL15.glBeginQuery(GL15.GL_SAMPLES_PASSED, occquery);

        Core.List.get(object2).Draw(false);

        GL15.glEndQuery(GL15.GL_SAMPLES_PASSED);

        do {
            GL15.glGetQueryObject(occquery, GL15.GL_QUERY_RESULT_AVAILABLE, samples);
        } while (samples.get(0) == 0);

        GL15.glGetQueryObject(occquery, GL15.GL_QUERY_RESULT, samples);

        return samples.get(0) > 0;
    }


Can I detect point(s) where colision taken place?

And one additional question - how can I handle colisions that happen outside screen without making extremally big glScissor?

CodeBunny

Oh god. Looking at your code and inferring what is going on, do you realize how god-awful slow that's going to be?

Seriously, this is not the way to go about doing pixel-perfect collision.

If you want to do pixel-perfect collision, just associate a 2D array of booleans with every sprite (representing where opaque pixels are), and iterate over the intersecting region of both arrays. If two corresponding locations in the arrays are both true, then you have a collision there.

Unfortunately, while that's the best way I can think of to check for pixel-perfect collisions, that's still really slow, and very limited (it's hard to apply rotation/scaling to the geometry, for example). Are you sure you need this type of collision checking? I've yet to see a situation where using actual geometry is inferior.

Mac70

I think that I need very accurate collision checking as I am trying to make (2d) space game where collisions plays very important role - both between two ships and ships versus bullets.

I want to make very realistic looking collisions between ships, so I think that pixel perfect collision system is best suited to make this. If not, could someone recommend me a different method please?