현재 iPhone 화면에 일련의 거품을 그려야하는 게임을하고 있습니다. 각각의 버블에는 자체의 다람쥐 바디가 있으며, PhysicsEditor라는 다각형 물리 편집기를 사용하여 그려져 있습니다. 이를 통해 다람쥐 몸체 (질량, 탄성, 앵커 포인트 등)를 만드는 데 필요한 정보가 들어있는 .plist 파일을 만들었습니다.Chipmunk Physics 'Anchorpoints and positions
다음은 주어진 좌표로 거품을 만드는 데 사용하는 코드입니다.
- (cpBody *)drawBubblesWithX:(int)x andY:(int)y {
NSString *name = names[arc4random() % 7];
CCSprite *sprite; // Will contain the sprite object if name isn't Nil
// create physics shape
[[GCpShapeCache sharedShapeCache] anchorPointForShape:@"BubbleSlot"];
cpBody *body = [[GCpShapeCache sharedShapeCache] createBodyWithName:@"BubbleSlot" inSpace:space withData:nil];
cpBodySetPos(body, cpv(x, y));
if (name != nil) { // If a color has been selected
// create and add sprite
sprite = [CCSprite spriteWithFile:[NSString stringWithFormat:@"%@.png", name]];
[self addChild:sprite];
// set the position of the sprite to be the same as the physics body
sprite.position = CGPointMake(x, y);
// set the position of body based on the sprite object
cpBodySetUserData(body, (__bridge cpDataPointer)(sprite));
}
return body;
}
에서 [[GCpShapeCache sharedShapeCache]을 목적은 기관의 생성을 위해 사용되는 .plist 파일을 포함한다. .plist에는 "BubbleSlot"이라는 이름의 몸체가 하나뿐입니다.이 거품은 모든 거품을 만들기위한 템플릿으로 사용됩니다.
본문의 고정 점은 약 (0.5, 0.5)입니다.
내 문제는 cpBodySetPos() 함수가 내 피직스 개체의 위치를 설정하지 않는다는 것입니다. 앵커 포인트에 의해 변경됩니다. 예를 들어 iPhone 화면 중앙에 내 cpBody 객체의 위치를 설정하려면 cpBodySetPos (body, cpv (160,240))를 사용합니다. 그러나 어떤 이유로 나는 .plist 파일에 (160,240)의 값으로 본문의 기준점을 설정 한 다음 그에 따라 본문을 만들어야합니다.