2011-08-09 6 views
2

나는 Cocos2D v0.99를 사용하여 iPhone에서 공간 침입자 복제를 만들고 있습니다. 나는 프레임 워크와 함께 거기에 가고있다. 그러나 아직도 많이 모른다. 내가 붙어있는 단계는 충돌로 이어진다. 두 가지 작업이 필요합니다.어떻게 코코스 2D 아이폰과 완벽하게 충돌합니까 픽셀

1) 스프라이트 시트를 사용하여 애니메이션 된 CCSprites의 픽셀과 완벽한 충돌을해야합니다. 2)베이스와 비슷한 pixelperfect 충돌을해야하지만 기본 픽셀을 삭제해야합니다.

나는 몇 자원 (http://groups.google.com/group/cocos2d-iphone-discuss/browse_thread/thread/f6e734e00d863f5e/41768952a1bcca0e?lnk=gst & Q = 이미지 + 마스크를) 발견에 노력했다 CCMutableTexture2D를 사용하지만이 cocos2d 버전에서는 잘 작동하지 않습니다.

내 접근 방식은 CCMutableTexture2D를 수정하는 것이지만 현재 스프레드 텍스처 데이터를 MutableTexture로 가져오고 있습니다.

이 작업을 수행하는 가장 좋은 방법입니까 아니면 다른 기술이 누락 되었습니까?

감사합니다. http://www.cocos2d-iphone.org/forum/topic/18522

나는 또한 CCRenderTexture과 조화를 사용하는 관리 :

는 ----- UPDATE -----

나는이 여기에 색상 혼합 방법을 사용하여 시뮬레이터에서 작동하도록 관리 함수는 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]; 
    } 
} 

그래서, glReadPixels()는 OpenGL ES에서 지원 또는이다 이 작업을 수행하는 더 좋은 방법이?

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

또한이 코드에는 꽤 심각한 성능 문제가 있지만이를 해결할 수 있습니다.

감사합니다.

----- UPDATE (다시) ------

난 데이터 구조는 기본 초기화 구축과 충돌하는 조회로 사용되는 경우 프로세스는 부분적 지금 일하고있다. 그것은 시간의 50 % ish 작동하지만 다음 malloc EXC_BAD_ACCESS 오류로 좀비 및 XCode있는 모든 다양한 malloc 디버깅 옵션을 설정할 때까지 충돌.

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 
지금, 나는 (명확성을 위해없는 몇 줄의) 수행하여

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

나는이에 도착 라인에 일관성 충돌을 얻을

올바른 스레드에 관한 몇 가지 사항을 읽었지만 스레드 1에있는 것처럼 보입니다.

s 또는 malloc 오류를 디버그하는 방법에 대한 좋은 리소스에 대한 링크는 매우 감사하게 받아 들여질 것입니다.

감사합니다,

마틴

----- 아웃이 인터넷 검색의 온 ANOTHER UPDATE -----

한가지는 OpenGL을 접선에 약간의 복잡성이 있다는 것입니다 컨텍스트 제대로 (http://stackoverflow.com/questions/4459415/problem-with-opengles-to-show-an-image) 난 그냥 cocos2d 0.99 기지를 사용하고 그래서이 문제가 될 수 있을까? 그렇다면 어떻게 * & % @ £ $ 내가 고칠 수 있습니까?

+0

[이] (http://stackoverflow.com/questions/1241023/pixel-perfect-collision) 본 적이 있습니까? –

+0

CGImage 타입의 것들을 보았습니다. 그러나 문제는 어떤 종류의 픽셀 데이터로 텍스처를 얻는 방법입니다. CCMutableTexture2D는 pixelAt로 이것을 수행하는 것으로 보이지만 CCTableure2D를 CCMutableTexture2D로 가져 오는 방법을 알아낼 수는 없습니다. – Martin

답변

1

물리 엔진이 충돌을 처리하게하고, cocos2d가 chipmunk 및 box2D와 잘 통합되도록 제안합니다.이 엔진은 콜백을 사용하여 충돌을 처리 할 수 ​​있습니다.

+0

좋아요, 적어도 옵션은 있지만 꽤 무겁기 때문에 직접 할 수는 있습니다. 감사. – Martin

관련 문제