// create render texture for collision testing and make it visible for testing purposes
// it only needs ot be as large as the sprite I am using as the hit point (where the touch happened)
_collissionRT = [[CCRenderTexture renderTextureWithWidth:self.collissionMarker.contentSize.width height:self.collissionMarker.contentSize.height] retain];
-(BOOL) isCollisionBetweenSpriteA:(CCSprite*)spr1 pixelPerfect:(BOOL)pp collisionPosition:(CGPoint) testPosition
{
BOOL isCollision = NO;
int red_ = 0;
int green_ = 0;
int blue_ = 0;
unsigned int step = 1;
CCSprite* spr2 = collissionMarker;
// move the collision sprite to the touch position ready for bounding box test for spr1 which is the collision target
spr2.position = testPosition;
spr2.visible = YES;
spr2.opacity = 128;
_collissionRT.visible = YES;
CGRect intersection = CGRectIntersection([spr1 boundingBox], [spr2 boundingBox]);
CGPoint spr1_oldPos = spr1.position;
CGPoint spr1_oldAnchorPos = spr1.anchorPoint;
CGPoint collisionMarker_oldPos = spr2.position;
CGPoint collisionMarker_oldAnchorPos = spr2.anchorPoint;
// move the sprites we are testing into the relevant space of the RenderTexture
self.collissionMarker.anchorPoint = ccp(0,0);
self.collissionMarker.position = ccp(0,0);
spr1.anchorPoint = ccp(0,0);
spr1.position = ccp(spr1_oldPos.x - collisionMarker_oldPos.x, spr1_oldPos.y - collisionMarker_oldPos.y);
// Look for simple bounding box collision
if (!CGRectIsEmpty(intersection))
{
// If we're not checking for pixel perfect collisions, return true
if (!pp) {return YES;}
// Get intersection info
unsigned int x = intersection.origin.x * CC_CONTENT_SCALE_FACTOR();
unsigned int y = intersection.origin.y * CC_CONTENT_SCALE_FACTOR();
unsigned int w = intersection.size.width * CC_CONTENT_SCALE_FACTOR();
unsigned int h = intersection.size.height * CC_CONTENT_SCALE_FACTOR();
unsigned int numPixels = w * h;
//NSLog(@"\nintersection = (%u,%u,%u,%u), area = %u",x,y,w,h,numPixels);
// Draw into the RenderTexture
[_collissionRT beginWithClear:0 g:0 b:0 a:0];
// Render both sprites: first one in RED and second one in GREEN
for (CCNode* node in self.children) {
if(node == spr1) {
node.visible = YES; // parent has to be visible or its all pointless!
} else {
node.visible = NO;
}
}
glColorMask(1, 0, 0, 1);
[spr1 visit];
ccColor4B *buffer = (ccColor4B *)malloc( sizeof(ccColor4B) * numPixels );
glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
for(unsigned int i=0; i<numPixels; i+=step)
{
ccColor4B color = buffer[i];
if(color.a != 0) {
red_ += color.r;
blue_ += color.b;
green_ += color.g;
}
}
red_ = red_ /(numPixels-1);
green_ = green_ /(numPixels-1);
blue_ = blue_ /(numPixels-1);
ccColor4B c = ccc4(50, 50, 50, 255);
particleColor_ = ccc4FFromccc4B(c);
for (CCNode* node in self.children) {
if(node == spr2) {
node.visible = YES;
} else {
node.visible = NO;
}
}
glColorMask(0, 1, 0, 1);
[spr2 visit];
glColorMask(1, 1, 1, 1);
// Get color values of intersection area
//ccColor4B *buffer = (ccColor4B *) malloc( sizeof(ccColor4B) * numPixels );
glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
/******* All this is for testing purposes *********/
// Draw the first sprite bounding box
/*
CGRect r1 = [spr1 boundingBox];
glColor4f(1, 0, 0, 1);
glLineWidth(0.5f);
ccDrawLine(ccp(r1.origin.x,r1.origin.y), ccp(r1.origin.x+r1.size.width,r1.origin.y));
ccDrawLine(ccp(r1.origin.x,r1.origin.y), ccp(r1.origin.x,r1.origin.y+r1.size.height));
ccDrawLine(ccp(r1.origin.x+r1.size.width,r1.origin.y), ccp(r1.origin.x+r1.size.width,r1.origin.y+r1.size.height));
ccDrawLine(ccp(r1.origin.x,r1.origin.y+r1.size.height), ccp(r1.origin.x+r1.size.width,r1.origin.y+r1.size.height));
// Draw the second sprite bounding box
CGRect r2 = [spr2 boundingBox];
glColor4f(0, 1, 0, 1);
glLineWidth(0.5f);
ccDrawLine(ccp(r2.origin.x,r2.origin.y), ccp(r2.origin.x+r2.size.width,r2.origin.y));
ccDrawLine(ccp(r2.origin.x,r2.origin.y), ccp(r2.origin.x,r2.origin.y+r2.size.height));
ccDrawLine(ccp(r2.origin.x+r2.size.width,r2.origin.y), ccp(r2.origin.x+r2.size.width,r2.origin.y+r2.size.height));
ccDrawLine(ccp(r2.origin.x,r2.origin.y+r2.size.height), ccp(r2.origin.x+r2.size.width,r2.origin.y+r2.size.height));
// Draw the intersection rectangle in BLUE (testing purposes)
glColor4f(0, 0, 1, 1);
glLineWidth(0.5f);
ccDrawLine(ccp(x,y), ccp(x+w,y));
ccDrawLine(ccp(x,y), ccp(x,y+h));
ccDrawLine(ccp(x+w,y), ccp(x+w,y+h));
ccDrawLine(ccp(x,y+h), ccp(x+w,y+h));
*/
/**************************************************/
[_collissionRT end];
// Read buffer
unsigned int step = 1;
for(unsigned int i=0; i<numPixels; i+=step)
{
ccColor4B color = buffer[i];
if (color.r > 0 && color.g > 0)
{
isCollision = YES;
break;
}
}
// Free buffer memory
free(buffer);
}
for (CCNode* node in self.children) {
node.visible = (node.tag != kTagTargetMask);
}
// move the sprites we are testing into the relevant space of the RenderTexture
spr2.anchorPoint = collisionMarker_oldAnchorPos;
spr2.position = collisionMarker_oldPos;
spr1.anchorPoint = spr1_oldAnchorPos;
spr1.position = spr1_oldPos;
self.collissionMarker.visible = NO;
//_collissionRT.visible = NO;
return isCollision;
}