How do I do pixel perfect collisions with cocos2d iphone
I am making a space invaders clone on iPhone using Cocos2D v0.99. I am getting there with the framework but there is still a lot I don't know. The step I am stuck on comes down to collisions. I need to do two things:
1) I need to do a pixel perfect collision with the fleet of CCSprites that are animated using a spritesheet. 2) I need to do a similar pixelperfect collision with the bases but I also need to delete some pixels for the base.
I have found a few resources (http://groups.google.com/group/cocos2d-iphone-discuss/browse_thread/thread/f6e734e00d863f5e/41768952a1bcca0e?lnk=gst&q=image+mask) and tried to use CCMutableTexture2D but it doesn't work very well with this cocos2d version.
My approach at the moment is to try and fix CCMutableTexture2D but I am struggling to get the sprite texture data into the MutableTexture.
Is this the best way to be doing this or am I missing some other technique?
Thanks.
----- UPDATE -----
I managed to get this to work in the simulator using the color blend method here: http://www.cocos2d-iphone.org/forum/topic/18522
I also managed to use CCRenderTexture and a blend function to do the pixel deletion thing via the RenderMask class (rm below).
But... the collision detection fails on device. I think the masking works ok though. My best guess is that there is something that I am using that is not OpenGL ES (or I have not set something up correctly. My code is:
rt = [CCRenderTexture renderTextureWithWidth:480 height:320];
[rt beginWithClear:0 g:0 b:0 a:0];
[rm visit];
// Read pixels
ccColor4B *buffer = malloc( sizeof(ccColor4B) * numPixels );
glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
[_rt end];
// loop through pixels
for(unsigned int i=0; i<numPixels; i++)
{
ccColor4B color = buffer[i];
if ((float)color.a >0) {
[self doSomeStuff];
}
}
So, 开发者_如何学JAVAis glReadPixels() supported in OpenGL ES? Or, is there some better way to do this?
// RenderMask.h
//
// RenderMask.h
//
#import <UIKit/UIKit.h>
#import <stdlib.h>
#import "cocos2d.h"
@interface RenderMask : CCRenderTexture {
@public
NSMutableArray *sprites;
NSMutableArray *spriteMasks;
int currentSprite;
float frameInterval; //How fast this animates
}
-(void) initArrays;
-(void) drawCurrent;
-(void) addSpriteMask: (CCSprite*)spriteMask;
-(void) addSprite: (CCSprite*)sprite;
@property(nonatomic, retain) NSMutableArray *sprites;
@property(nonatomic, retain) NSMutableArray *spriteMasks;
@property(readwrite, assign) int currentSprite;
@property(readwrite, assign) float frameInterval;
@end
// RenderMask.m
//
// RenderMask.m
//
#import "RenderMask.h"
#import "CCActionInterval.h"
@implementation RenderMask
@synthesize sprites, spriteMasks, currentSprite, frameInterval;
-(void) initArrays {
sprites = [[NSMutableArray alloc] init];
spriteMasks = [[NSMutableArray alloc] init];
currentSprite = 0;
frameInterval = 0.1f;
}
-(void) drawCurrent {
[self clear:0.0f g:0.0f b:0.0f a:0.0f];
[self begin];
//Limit drawing to the alpha channel
NSValue *spriteValue = [sprites objectAtIndex:0];
CCSprite *sprite = (CCSprite*)[spriteValue pointerValue];
[sprite setOpacityModifyRGB:NO];
[sprite visit];
glColorMask(1.0f, 1.0f, 1.0f, 1.0f);
NSLog(@"spriteMasks size: %u", [spriteMasks count]);
for(NSValue *value in spriteMasks){
CCSprite *spriteMask = (CCSprite*)[value pointerValue];
[sprite setOpacityModifyRGB:NO];
[spriteMask visit];
}
glColorMask(1.0f, 1.0f, 1.0f, 1.0f);
[self end];
}
-(void) addSpriteMask: (CCSprite*)spriteMask {
NSLog(@"Adding spriteMask!!!");
[spriteMask retain];
[spriteMasks addObject:[NSValue valueWithPointer:spriteMask] ];
}
-(void) addSprite: (CCSprite*)sprite {
NSLog(@"Adding sprite!!!");
[sprite retain];
[sprites addObject:[NSValue valueWithPointer:sprite] ];
}
-(void) runAnimation {
NSLog(@"running animation");
[self drawCurrent];
currentSprite = currentSprite + 1;
if(currentSprite >= [sprites count]){
currentSprite = 0;
}
CCActionInterval * runAction = [CCCallFunc actionWithTarget:self selector:@selector(runAnimation)];
CCActionInterval * delayAndRunAction = [CCSequence actions:[CCDelayTime actionWithDuration:frameInterval], runAction, nil];
[self runAction:delayAndRunAction];
}
@end
Also, there is a pretty serious performance issue with this code but I can probably work through that.
Thanks.
----- UPDATE (AGAIN) ------
I have a process partly working now where a data structure is build on base initialisation and used as a lookup for a collision. It was working 50%ish of the time but then crashing with an malloc EXC_BAD_ACCESS error until I turned on Zombies and all the various malloc debugging options in XCode. Now, I get a consistent crash on the line
CGContextClearRect(imageContext, CGRectMake(0, 0, contextWidth, contextHeight));
I get to this by doing (with a few lines missing for clarity):
CGImageRef image = [UIImage imageNamed:@"aspeaker.png"].CGImage;
imageContext = CGBitmapContextCreate(theData, width, height, 8, 4 * width, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big);
NSLog(@"imageContext: %@",imageContext);
CGContextClearRect(imageContext, CGRectMake(0, 0, width, height));
CGContextTranslateCTM(imageContext, 0, height - imageSize.height);
CGContextDrawImage(imageContext, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
CGContextRelease(imageContext);
The debug console says:
2011-09-12 10:49:07.753 Invaders[1341:c803] imageContext: <CGContext 0x7f798fa0>
Invaders(1341,0xad06c2c0) malloc: protecting edges
Invaders(1341,0xad06c2c0) malloc: recording malloc stacks to disk using standard recorder
Invaders(1341,0xad06c2c0) malloc: enabling scribbling to detect mods to free blocks
I have read a few things about being on the correct thread but it all looks like it is on thread 1.
Any ideas or a link to a good resource on how to debug malloc errors would be very gratefully received.
Thanks,
Martin
----- ANOTHER UPDATE -----
One thing that came out of Googling this is that there is some complexity in hooking up the OpenGL context properly (http://stackoverflow.com/questions/4459415/problem-with-opengles-to-show-an-image) I am just using the cocos2d 0.99 base so could this be the problem? If so how the *&%@£$ do I fix it?
I would suggest letting a physics engine handle the collisions, cocos2d integrates well with chipmunk and box2D, this engines can handle the collisions for you with callbacks.
精彩评论