Pixel Perfect Collision Detection

maheshit: I did experienced this problem, and solved it using absolute path instead of relative path, something not so portable.

However, after been able to load shader files, my trouble is that when the to sprites collide, they get shrunk.

Am I doing something wrong?

Any help would be appreciated.

Hey,

I’ve been using you code for pixel perfect collision and it seems to have a problem where it sometimes works and sometimes crashes specifically on the call to glReadPixels. I noticed there was a comment on your blog post that had the same problem but no solution ? I’m using cocos2dx 2.2.3 with android

Any help would be appreciated thanks

I’m having trouble too, for some reason it doesn’t work properly.
Can someone upload a sample project which uses this method?
I really want to use this in my game.

Thanks! :slight_smile:

Thanks! I really need in such function, and I’ve added these 4 files in my project. But code is working strange, looks like REAL size of sprites more than displayed size. And after adding “CollisionDetection::GetInstance()->areTheSpritesColliding(….)” moving of my sprites working very strange.
I Use x-code and cocos2d-x v3 version (from GitHub)

Who know how to solve this problem? Or may be there is other completed solution?

Hi all,

Here’s a simple solution for pixel perfect detection. It should be enough to get you starting if you need something more complex.

Hello,

I also posted a few months ago my own implementation:

Any feedback is appreciated.

Hi, @happybirthday
I just checked your blog, and I wanted to implement my own version. Because, in my case there are no sprites, and only DrawNodes, and it seems like your version is for older Cocos2d-x.
http://blog.muditjaju.infiniteeurekas.in/?p=1

I want to ask few questions. You made the secondary buffer(_rt) invisible. Why?
glReadPixels seems to read only the pixels of showing frame buffer. So, I think it’s impossible to read the pixels of invisible nodes.
Actually, I thought it reads current buffer, and if we call glReadPixels, between _rt->beginWithClear( 0, 0, 0, 0); and _rt->end(); it returns the pixels of _rt even it’s invisible.
But, it seems it doesn’t work that way. It still returns the pixels of main frame buffer(visible screen).

Can you help me out? Thanks.

Oh, I got it. In Cocos2d-x 3.x, visit() seems asynchronous.
So, I had to call Director::getInstance()->getRenderer()->render(); before glReadPixels.

Hi ahlwong ,

Please tell me where I’m wrong in using your class for detecting pixel perfect collision.

I created HitDetectHelper.h and HitDetectHelper.cpp file in my project.

Then I copy and past code from http://alexanderwong.me/post/108850552513/cocos2d-x-3-x-pixel-perfect-hit-detection to HitDetectHelper.h and HitDetectHelper.cpp file.

Then I call hitTest function in my game class to check the pixel perfect collision like this

if (HitDetectHelper::hitTest(PlaneSprite, Point(topFloorSprite->getPosition().x, topFloorSprite->getPosition().y)))
{
showBlast();
}

But no collision is detected.

If I’m wrong please tell me how to use your solution for pixel perfect detection.

the Point you pass into the method should be a point local to the Sprite you’re testing (a point relative to the sprite itself).

So, in your example, you’d want to do something like:

Point globalPoint = topFloorSprite->getParent()->convertToWorldSpace(topFloorSprite->getPosition());
Point testPoint = PlaneSprite->convertToNodeSpace(worldPoint);

if (HitDetectHelper::hitTest(PlaneSprite, testPoint))
{
showBlast();
}

Hello ahlwong ,

Thanks for your reply.

I’m using the following code to detect collision between 2 sprite.

Point globalPoint = topFloorSprite->getParent()->convertToWorldSpace(topFloorSprite->getPosition());
//Point testPoint = PlaneSprite->convertToNodeSpace(worldPoint);
Point testPoint = PlaneSprite->convertToNodeSpace(globalPoint );

if (HitDetectHelper::hitTest(PlaneSprite, testPoint))
{
showBlast();
}

Above code is showing the collision only when the mid point of PlaneSprite and mid point of topFloorSprite collides.

I want the collision like as shown in the below image.

Why

How can I achieve this kind of collision ?

I followed another topic, implemented it and found/fixed bug there.
It could be that this topic has the same bug.
Working version is here: Pixel collision detection for v3.x
–Josef

My helper class is not written for this kind of collission detection.

You could test each bit but it would be an N time operation or N^2 at worst. If I were writing your game, I’d use some kind of heuristic. Off the top of my head, I would preprocess the ship images and keep track of the maximal bounds of the ship. Say, keep track of the first pixel x value for any given y value from and left and from the right and compare. Basically some sort of approximation that runs much faster

Maybe break the sprites conceptually into primitive shapes that you can mathematically check intersection on quickly

OK thanks for all your support.

I’ll try it.

This doesn’t work if the Sprites are child nodes of different sprites.

Does anyone else have this problem ? It seems to me because r1.intersects(r2) in PixelCollision::collidesWithSprite(Sprite *sprite1, Sprite *sprite2, bool pp) sometimes returns false.

This must be obvious to the rest of you, but have I misunderstood something about how bounding boxes work ? Or is this a bug ?

I am using 3.13.1

Check this out:

@piotrros - Isn’t that a different issue ?

I’ve never used collidesWithSprite method - it doesn’t even seem to be in the code. Where did you get it and how is it implemented?

Hmmm I seem to have posted in the wrong thread! The source is: