cocos2d-x 不规则碰撞检测

 原文:http://www.2cto.com/kf/201401/272331.html

//判断有没有点到有材质的部分, p_point相对, CCSprite坐标  (p_point是相对 Sprite锚点的偏移)

bool isClickTheRealSprite(cocos2d::CCSprite* p_sprite, const cocos2d::CCPoint p_point)

{

    CCSize l_sizeInPixel = p_sprite->getContentSize();

    //得到相对,Sprite左下的偏移

    int l_iX = (int)(l_sizeInPixel.width/2.0f + p_point.x);

    int l_iY = (int)(l_sizeInPixel.height/2.0f + (int)p_point.y);

    CCPoint l_pointBL = ccp(l_iX, l_iY);

    CCRect l_rect = CCRect(0, 0, l_sizeInPixel.width, l_sizeInPixel.height);

    if(!(l_rect.containsPoint(l_pointBL))){

        return false;

    }

    

    //开辟出空内存buffer

    //unsigned char* l_buffer = (unsigned char*)malloc(l_sizeInPixel.width * l_sizeInPixel.height * 4);

    //memset(l_buffer, 0, l_sizeInPixel.width * l_sizeInPixel.height * 4);

    uint8_t l_buffer[4];

    

    //写内存

    CCSize size = p_sprite->getParent()->getContentSize();

    CCRenderTexture* l_renderTexture = CCRenderTexture::create(l_sizeInPixel.width, l_sizeInPixel.height, kCCTexture2DPixelFormat_RGBA8888);

    //l_renderTexture->begin();

    l_renderTexture->beginWithClear(0, 0, 0, 0);

    p_sprite->draw();

    glReadPixels(l_pointBL.x, l_pointBL.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, l_buffer);

    l_renderTexture->end();

    //透明度

    //int l_iValue_A = l_buffer[4*(int)(l_pointBL.y*l_sizeInPixel.width + l_pointBL.x)+3];

    int l_iValue_A = l_buffer[3];

    //free(l_buffer);

    if(l_iValue_A < 5 ){

        return false;

    }

    return true;

}

 

你可能感兴趣的:(cocos2d-x)