cocos2d でピクセルの完全な衝突を見つける方法を実装しました。ただし、オブジェクトのピクセル パーフェクト コリジョンをスケーリングする必要がある場合は、機能しません。
//sprite declaration
fruit1=[CCSprite spriteWithSpriteFrameName:@"Burger-Cut1.png"];
fruit1.position=ccp(200,100);
[self addChild:fruit1];
fruit.scale=0.5;
//find the collision
-(bool) isTransparentWithSprite: (CCSprite *)sprite pointInNodeSpace: (CGPoint) point {
NSLog(@"in function");
bool isTransparent = YES;
unsigned int w = 1;
unsigned int h = 1;
unsigned int wh = w * h;
//ccColor4B *buffer = ccColor4B[4];
Byte buffer[4];
// Draw into the RenderTexture
[rt beginWithClear:0 g:0 b:0 a:0];
CGPoint oldPos = sprite.position;
CGPoint oldAnchor = sprite.anchorPoint;
sprite.anchorPoint = ccp(0, 0);
sprite.position = ccp(-point.x, -point.y);
[sprite visit];
sprite.anchorPoint = oldAnchor;
sprite.position = oldPos;
// read the pixels
float x = 0;//local.x;
float y = 0;//local.y;
glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
//NSLog(@"%d: (%f, %f)", sprite.tag, x, y);
[rt end];
// Read buffer
ccColor4B color;
for(unsigned int i=0; i<wh; i++) {
//color = buffer[i];
// if untouched, point will be (0, 0, 0, 0). if the sprite was drawn into this pixel (ie not transparent), one of those values will be altered
if ( buffer[0] || buffer[1] || buffer[2] || buffer[3]) {
NSLog(@" ");
NSLog(@"%d %d %d %d=== > ",buffer[0],buffer[1],buffer[2],buffer[3]);
NSLog(@" ");
isTransparent = NO;
}
}
//delete buffer;
return isTransparent;
}