I am currently working on a game where I must draw a series of bubbles on the iPhone screen. Each bubble has its own chipmunk body, which has been drawn using a polygon physics editor named PhysicsEditor. With it, I created a .plist file containing the information needed to create a chipmunk body (mass, elasticity, anchorpoint, etc.).
What follows is the code I use to create a bubble with a given coordinate.
- (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;
}
The [[GCpShapeCache sharedShapeCache] object contains the .plist file used for the creation of the bodies. In the .plist, there is only 1 body, named "BubbleSlot", which is used as a template to create all the bubbles ingame.
The anchorpoint for the body is about (0.5, 0.5).
My problem is that the cpBodySetPos() function does not set the position of my physics object. It is changed by the anchor point. For example, to set the position of my cpBody object in the center of the iPhone screen, I would do cpBodySetPos(body, cpv(160,240)). But for some reason, I must set the anchorpoint of the body in the .plist file with the values of (160,240) , then create the body accordingly.