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
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! 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)
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).
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();
}
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
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 ?