Cocos2dx中的像素完美碰撞检测 [英] Pixel Perfect Collision Detection in Cocos2dx

查看:271
本文介绍了Cocos2dx中的像素完美碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to port the pixel perfect collision detection in Cocos2d-x the original version was made for Cocos2D and can be found here: http://www.cocos2d-iphone.org/forums/topic/pixel-perfect-collision-detection-using-color-blending/

这是我的Cocos2d-x版本代码

Here is my code for the Cocos2d-x version


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屏蔽代码无法正常工作。

My code is working perfectly if I send the "pp" parameter as false. That is if I do only a bounding box collision but I am not able to get it working correctly for the case when I need Pixel Perfect collision. I think the opengl masking code is not working as I intended.

这里是 _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时犯了一个错误

I think I am making a mistake with the implementation of this CCRenderTexture

有人可以指导我我做错了什么吗?

Can anyone guide me with what I am doing wrong ?

谢谢您的时间:)

推荐答案

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

Finally solved the problem. Had to use custom opengl fragment shaders to shade one of the sprites completely RED and the other completely BLUE and then looping through glReadPixels values to find any pixel having both RED and BLUE pixels. (Blending has to be considered as well, we do not want to replace one pixel value by the other)

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

In-Depth information can be found on my blog post http://blog.muditjaju.infiniteeurekas.in/?p=1

这篇关于Cocos2dx中的像素完美碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆