views:

38

answers:

1

I'm drawing some lines in cocos2d (using handy ccDrawLine), nothing fancy, but i want to check if my animated line hits something. I could do some math calculations for some objects, but for some dynamic parts in the scene it would be much easier if i could do:

if pixel not black at (x,y)
  // line will hit something
  do handleCollisionDetectedAt(x,y)

What would you suggest? At least what would you suggest if it would be simple OpenGL ES.

A: 

Found solution:

GLubyte pColor[4];
glReadPixels(x,y,1,1,GL_RGBA,GL_UNSIGNED_BYTE,&pColor[0]);
int red = pColor[0];
int green = pColor[1];
int blue = pColor[2];   

NSLog(@"(R,G,B) = (%d,%d,%d)",red,green,blue);
Allisone
Be careful about doing this. Reading pixels out of a framebuffer can be expensive, and probably requires synchronizing the GPU with the CPU which can stall your whole drawing. If you need to do this in tight loop, be careful of the perf consequences.
quixoto
hmm good to know, right now it seems as if I will go with a texture that I will render to, so maybe reading the texture's pixels is less expensive ?
Allisone