0

i have a problem with cocos2d and glReadPixels because don't work correctly. I found in web a code for pixel perfect collision and i modified for my app, but with the animation or more fast animation don't work.

This is the code:

-(BOOL) isCollisionBetweenSpriteA:(CCSprite*)spr1 spriteB:(CCSprite*)spr2 pixelPerfect:(BOOL)pp { BOOL isCollision = NO; CGRect intersection = CGRectIntersection([spr1 boundingBox], [spr2 boundingBox]);

// Look for simple bounding box collision
if (!CGRectIsEmpty(intersection))
{
    // If we're not checking for pixel perfect collisions, return true
    if (!pp) {return YES;}

    // Get intersection info
    unsigned int x = intersection.origin.x;
    unsigned int y = intersection.origin.y;
    unsigned int w = intersection.size.width;
    unsigned int h = intersection.size.height;
    unsigned int numPixels = w * h;
    //NSLog(@"\nintersection = (%u,%u,%u,%u), area = %u",x,y,w,h,numPixels);

    // Draw into the RenderTexture
    [_rt beginWithClear:0 g:0 b:0 a:0];

    // Render both sprites: first one in RED and second one in GREEN
    glColorMask(1, 0, 0, 1);
    [spr1 visit];
    glColorMask(0, 1, 0, 1);
    [spr2 visit];
    glColorMask(1, 1, 1, 1);

    // Get color values of intersection area

    ccColor4B *buffer = malloc( sizeof(ccColor4B) * numPixels );
    glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    /******* All this is for testing purposes *********/


    // Draw the intersection rectangle in BLUE (testing purposes)


    /**************************************************/

    [_rt end];

    // Read buffer
    unsigned int step = 1;
    for(unsigned int q=0; q<1; q+=step)
    {
        ccColor4B color = buffer[q];

        if (color.r > 0 && color.g > 0)
        {
            isCollision = YES;
            break;
        }
    }

    // Free buffer memory
    free(buffer);
}

return isCollision;

}

where is the problem?I tried but nothing.

Thank you very much. regards.

Antonio
  • 21
  • 5

1 Answers1

0

If you are using iOS6, have a look at this post for a solution:

CAEAGLLayer *eaglLayer = (CAEAGLLayer *) self.layer;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithBool:YES],
                                    kEAGLDrawablePropertyRetainedBacking,
                                    kEAGLColorFormatRGBA8,        kEAGLDrawablePropertyColorFormat,
                                    nil];

The explanation is that iOS6 fixes some bugs in iOS Open GL implementation, so that the GL buffer is (correctly) cleared each time it is presented to the screen. Here what Apple writes about this:

Important: You must call glReadPixels before calling EAGLContext/-presentRenderbuffer: to get defined results unless you're using a retained back buffer.

The correct solution would be calling glReadPixels before the render buffer is presented to the screen. After that, it invalidated.

The solution above is just a workaround to make the image sort of "sticky".

Be aware that it can impact your app rendering performance. The point is that if you are using cocos2d, you cannot easily call glReadPixels before the the render buffer is presented.

Hope it helps.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • but i dont understand the problem...you can to post me a example of code?because if i call before [_rt end] and after glReadPixels() work,but normal collision and no pixel perfection collision.why? :( – Antonio Jan 25 '13 at 14:04
  • maybe this will help: http://stackoverflow.com/questions/11863416/read-texture-bytes-with-glreadpixels – sergio Jan 25 '13 at 16:00