gpt4 book ai didi

c++ - Cocos2dx 中的像素完美碰撞检测

转载 作者:可可西里 更新时间:2023-11-01 03:56:07 37 4
gpt4 key购买 nike

我正在尝试在 Cocos2d-x 中移植像素完美碰撞检测,原始版本是为 Cocos2D 制作的,可以在这里找到:http://www.cocos2d-iphone.org/forums/topic/pixel-perfect-collision-detection-using-color-blending/

这是我的 Cocos2d-x 版本的代码

bool CollisionDetection::areTheSpritesColliding(cocos2d::CCSprite *spr1, cocos2d::CCSprite *spr2, bool pp, CCRenderTexture* _rt) {    bool isColliding = false;    CCRect intersection;    CCRect r1 = spr1->boundingBox();    CCRect r2 = spr2->boundingBox();    intersection = CCRectMake(fmax(r1.getMinX(),r2.getMinX()), fmax( r1.getMinY(), r2.getMinY()) ,0,0);    intersection.size.width = fmin(r1.getMaxX(), r2.getMaxX() - intersection.getMinX());    intersection.size.height = fmin(r1.getMaxY(), r2.getMaxY() - intersection.getMinY());    // Look for simple bounding box collision    if ( (intersection.size.width>0) && (intersection.size.height>0) ) {        // If we're not checking for pixel perfect collisions, return true        if (!pp) {            return true;        }        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;        //CCLog("Intersection X and Y %d, %d", x, y);        //CCLog("Number of pixels %d", numPixels);        // Draw into the RenderTexture        _rt->beginWithClear( 0, 0, 0, 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 = (ccColor4B *)malloc( sizeof(ccColor4B) * numPixels );        glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);        _rt->end();        // Read buffer        unsigned int step = 1;        for(unsigned int i=0; i 0 && color.g > 0) {                isColliding = true;                break;            }        }        // Free buffer memory        free(buffer);    }    return isColliding;}

如果我将“pp”参数发送为 false,我的代码将完美运行。也就是说,如果我只进行边界框碰撞,但是当我需要 Pixel Perfect 碰撞时,我无法让它正常工作。我认为 opengl 屏蔽代码没有按预期工作。

这是“_rt”的代码

    _rt = CCRenderTexture::create(visibleSize.width, visibleSize.height);    _rt->setPosition(ccp(origin.x + visibleSize.width * 0.5f, origin.y + visibleSize.height * 0.5f));    this->addChild(_rt, 1000000);    _rt->setVisible(true); //For testing

我认为我在执行这个 CCRenderTexture 时犯了一个错误

任何人都可以指导我做错了什么吗?

感谢您的宝贵时间:)

最佳答案

终于解决了问题。必须使用自定义 opengl 片段着色器将其中一个 Sprite 完全着色为红色,将另一个 Sprite 完全着色为蓝色,然后循环遍历 glReadPixels 值以找到同时具有红色和蓝色像素的任何像素。 (也必须考虑混合,我们不想用一个像素值替换另一个像素值)

可以在我的博文中找到深入的信息 http://blog.muditjaju.infiniteeurekas.in/?p=1

关于c++ - Cocos2dx 中的像素完美碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18546066/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com