Pixel Perfect collisions in cocos2dx please help! :( [SOLVED]

Hi, I am a n00b and working on an app. Does anyone know of any way to do pixel perfect collision detection in cocos2dx. I have looked at trying to port the cocos2d-iphone ways but have always came to a dead end in a non-supported by cocos2dx function. Please Help :(:frowning:

UPDATE

look for the solution at the bottom of this thread

Which equivalent functions you need to do things ?

Wasin Thonkaew wrote:

Which equivalent functions you need to do things ?

I was trying to use Ernesto Corvi and ran into a selector porting error, then i tried using this:
http://www.learn-cocos2d.com/2011/12/fast-pixelperfect-collision-detection-cocos2d-code-1of2/

and couldn’t use CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));

Several classes you can’t use with Cocos2d-x as it’s rather for Objective-C.

I haven’t done this myself, but as I searched through I found this one http://www.cocos2d-x.org/boards/6/topics/3472.
In brief from that link: We can get texture from CCSprite as CCTexture2D but unfortunately its function to read each individual pixel has slow in performance, so those functions are not included. But you can insist to add those yourself.

That’s best I can help :wink:

Thanks, i’ll try it

I’m too a little bit of surprise of not so many devs out there do perfect pixel collision, that might be some reasons.

By the way, I found this http://www.cocos2d-iphone.org/forum/topic/18522.
You may adopt his idea to solve this.
By render an intersection between both sprites into a separate buffer/texture (look at CCRenderTexture, http://www.cocos2d-x.org/embedded/cocos2d-x/de/d50/classcocos2d_1_1_c_c_render_texture.html), then after all count the pixels there. If it’s more than 1, then collision occurs.
But nonetheless, the approach the link mentions still use glReadPixels(). Although there’s no other ways to read individual pixel, the performance hit is limit as we check only those bounding intersection area.

After all, be careful with performance. We should do rough check first before actually go deep into finer check as of pixel collision.

Wasin Thonkaew wrote:

I’m too a little bit of surprise of not so many devs out there do perfect pixel collision, that might be some reasons.
>
By the way, I found this http://www.cocos2d-iphone.org/forum/topic/18522.
You may adopt his idea to solve this.
By render an intersection between both sprites into a separate buffer/texture (look at CCRenderTexture, http://www.cocos2d-x.org/embedded/cocos2d-x/de/d50/classcocos2d_1_1_c_c_render_texture.html), then after all count the pixels there. If it’s more than 1, then collision occurs.
But nonetheless, the approach the link mentions still use glReadPixels(). Although there’s no other ways to read individual pixel, the performance hit is limit as we check only those bounding intersection area.
>
After all, be careful with performance. We should do rough check first before actually go deep into finer check as of pixel collision.

I tired porting that one and got an error with using selectors because the visiting sprites for CCRenderTexture is buggy in cocos2d-x.

A person on the other thread I posted gave me a sample app with a partial box-2d solution.

link: http://www.gmtdev.com/blog/2011/08/19/how-to-use-box2d-for-just-collision-detection-with-cocos2d-x/

:slight_smile:
:slight_smile:
:slight_smile:

I wonder that the unnecessary physics calculations are all completely disabled.
I don’t think by setting 0.0, 0.0 for gravity will disable it, the things behind the scene is still going on but just no effect of gravity.

If performance is top priority, better look it out. Anyway, that’s also acceptable solution :wink: