Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用cocos2d iphone进行像素完美碰撞_Iphone_Opengl_Cocos2d Iphone - Fatal编程技术网

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

如何使用cocos2d iphone进行像素完美碰撞,iphone,opengl,cocos2d-iphone,Iphone,Opengl,Cocos2d Iphone,我正在用cocos2dv0.99在iPhone上克隆太空入侵者。我正在用这个框架实现目标,但还有很多我不知道的地方。我被卡住的那一步归结为碰撞。我需要做两件事: 1) 我需要做一个像素完美的碰撞与车队的ccsprite是动画使用spritesheet。 2) 我需要做一个类似的像素完美碰撞的基础,但我还需要删除一些像素的基础 我找到了一些资源(http://groups.google.com/group/cocos2d-iphone-discuss/browse_thread/thread/f6

我正在用cocos2dv0.99在iPhone上克隆太空入侵者。我正在用这个框架实现目标,但还有很多我不知道的地方。我被卡住的那一步归结为碰撞。我需要做两件事:

1) 我需要做一个像素完美的碰撞与车队的ccsprite是动画使用spritesheet。 2) 我需要做一个类似的像素完美碰撞的基础,但我还需要删除一些像素的基础

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

目前我的方法是尝试修复CCMutableTexture2D,但我正在努力将sprite纹理数据导入到可变纹理中

这是做这件事的最好方法,还是我错过了其他一些技巧

谢谢

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

我设法在模拟器中使用颜色混合方法实现了这一点:

我还设法通过RenderMask类(下面的rm)使用CCRenderTexture和混合函数来完成像素删除

但是。。。设备上的碰撞检测失败。不过我觉得掩蔽效果不错。我最好的猜测是,我使用的东西不是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];
    }
}
此外,这段代码存在相当严重的性能问题,但我可能可以解决这个问题

谢谢

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

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

CGContextClearRect(imageContext, CGRectMake(0, 0, contextWidth, contextHeight));
我是这样做的(为了清楚起见,少了几行):

调试控制台显示:

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
2011-09-12 10:49:07.753入侵者[1341:c803]图像上下文:
入侵者(1341,0xad06c2c0)malloc:保护边缘
入侵者(1341,0xad06c2c0)malloc:使用标准记录器将malloc堆栈记录到磁盘
入侵者(1341,0xad06c2c0)malloc:启用涂鸦来检测释放块的MOD
我读过一些关于正确线程的文章,但看起来都是在线程1上

如果您有任何关于如何调试malloc错误的想法或链接,我们将不胜感激

谢谢

马丁

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


谷歌搜索的一个结果是,正确连接OpenGL上下文有一定的复杂性(http://stackoverflow.com/questions/4459415/problem-with-opengles-to-show-an-image)我只是在使用cocos2d 0.99底座,所以这可能是问题所在吗?如果是的话,*&%@$我该如何解决它?

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

好的,这是一个选项,但它们相当沉重因此,如果可以的话,我宁愿直接做。谢谢。你看到了吗?我看过一些CGImage类型的东西,但问题是如何从纹理到任何类型的像素数据。CCMutableTexture2D似乎是用pixelAt做的:但我不知道如何从CCTexture2D到CCMutableTexture2D
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