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 :(
UPDATE
look for the solution at the bottom of this thread
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.
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’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.
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