如何与cocos2d iphone进行像素完美碰撞

时间:2011-08-09 10:00:49

标签: iphone opengl cocos2d-iphone

我正在使用Cocos2D v0.99在iPhone上制作太空入侵者。我到达那里的框架,但仍然有很多我不知道。我坚持的步骤归结为碰撞。我需要做两件事:

1)我需要与使用spritesheet动画的CCSprites队列进行像素完美碰撞。 2)我需要与base进行类似的pixelperfect碰撞,但我还需要删除基数的一些像素。

我找到了一些资源(http://groups.google.com/group/cocos2d-iphone-discuss/browse_thread/thread/f6e734e00d863f5e/41768952a1bcca0e?lnk=gst&q=image+mask)并尝试使用CCMutableTexture2D,但它对这个cocos2d版本效果不佳。

我目前的方法是尝试修复CCMutableTexture2D,但我正在努力将精灵纹理数据放入MutableTexture中。

这是最好的方法吗?还是我错过了其他一些技巧?

感谢。

-----更新-----

我设法使用此处的颜色混合方法在模拟器中使用此功能:http://www.cocos2d-iphone.org/forum/topic/18522

我还设法使用CCRenderTexture和混​​合函数通过RenderMask类(下面的rm)进行像素删除。

但是......设备上的碰撞检测失败了。我认为掩蔽工作正常。我最好的猜测是我使用的东西不是OpenGL ES(或者我没有正确设置。我的代码是:

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];
    }
}

那么,OpenGL ES是否支持glReadPixels()?或者,有没有更好的方法来做到这一点?

// 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

此外,此代码存在严重的性能问题,但我可以解决这个问题。

感谢。

-----更新(再次)------

我现在有一个部分工作的过程,其中数据结构基于基本初始化并用作查找冲突。它的工作时间为50%,但随后出现了malloc EXC_BAD_ACCESS错误,直到我打开Zombies和XCode中的所有各种malloc调试选项。现在,我在线上遇到了一致的崩溃

CGContextClearRect(imageContext, CGRectMake(0, 0, contextWidth, contextHeight));

我通过这样做(为清晰起见,缺少几行):

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);

调试控制台说:

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

我已经阅读了一些关于正确线程的内容,但它看起来都在线程1上。

非常感谢收到有关如何调试malloc错误的好资源的任何想法或链接。

谢谢,

马丁

-----另一个更新-----

Google搜索引发的一件事是,正确连接OpenGL上下文存在一些复杂性(http://stackoverflow.com/questions/4459415/problem-with-opengles-to-show-an-image )我只是使用cocos2d 0.99碱基,所以这可能是问题吗?如果是这样的话,*&amp;%@£$如何修复它?

1 个答案:

答案 0 :(得分:1)

我建议让物理引擎处理碰撞,cocos2d与chipmunk和box2D很好地集成,这个引擎可以通过回调来处理碰撞。