RenderTexture / Pixel perfect collision with Cocos2d 3.3

Hello,

I’m trying to set up a pixel perfect collision detection using the method described here:
Pixel Perfect Collision Detection (found via this post on this forum).

It is based on rendering nodes we are testing to a texture and then check the pixels in it to see if sprites images overlap.
For some reason, the first sprite is correctly drawn, but the second one seems to have a transformation issue, it’s like mirrored or something.
I think my problem might be related to the fact that I’m testing nodes that are children of another node.

Also, the shaders seem to work quite randomly, I can’t have the first sprite red and the second blue. Sometimes all sprite are black, sometimes blue, sometimes the first sprite is rendered in normal colors (like no shader were applied) and the second black …

Since many comments here and there suggest the given code works perfectly, the only things I see that could differ are:

  • The Cocos version (I had a lot of deprecation while copying the code, and I updated the code to remove them)
  • The fact that I use a “master” node which is a child of the Scene and which contains all the sprites of my game (I need it because I’m porting a PC game to Android and I don’t want to modify too heavily its architecture).

Any help would be greatly appreciated, especially because I’m working on this particular point since a few days and I can’t see what to do.

Thanks

Well I think I can make it work, not the best in my opinion …

First, I duplicate the sprites to test and add them as children of the render texture (and I call removeFromParent() after usage).
I don’t use the “glUniform1i” functions, instead I use the program states to set the values.
I create a program for each sprite (so a second one).

Finally, I had to modify the shaders code to:

VS:

gl_Position = CC_PMatrix * a_position;

FS:

uniform float u_color_red;
uniform float u_color_blue;

void main()
{
    vec4 color = texture2D(CC_Texture0, v_texCoord);
    gl_FragColor = vec4(u_color_red, 0, u_color_blue, color.a);
}

So, the code seems to be able to do the trick, but considering the performances, I’m not sure it will be better than using any CPU-based method…