0
votes

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.

2

2 Answers

1
votes

Sprites have an anchor point too. You probably want to set that to the anchor point you've set in Physics Editor ((0.5, 0.5) style). This way the sprite shows correctly where the center of the body is.

self.sprite.anchorPoint = [[GCpShapeCache sharedShapeCache] anchorPointForShape:@"BubbleSlot"];

That's all I can think the Anchor Point in PE is for. PE's plist loading code sets the body's position to the anchor point, but that's rather useless as you're going to set the body's position anyway to somewhere more meaningful. So I just use it to set the sprite's anchor point, as above.

You are also updating the sprite's position with the body's position each frame, right? Something like this in an "update" or "step" method:

    CGPoint pt = cpBodyGetPos(self.body);
    self.sprite.position = pt;

From your symptoms it sounds like you may be updating the body's position yourself, rather than letting Chipmunk update it. You should only be setting the position once, when you're creating the body and adding it to the world.

1
votes

So I don't know exactly what the anchor point property is used for in Physics Editor, but Chipmunk has no such concept. A body's position is the same thing as it's center of gravity, the same point it rotates around when torque is applied.

Shapes are attached to a body relative to it's position. If you have a polygon shape with vertex of (0,0) it will be at the body's center of gravity. A vertex of (1, 0) will lie to the right of the body's center of gravity (if it's not rotated), etc.

My best guess is that the anchor point is where Physics Editor is treating as (0, 0). That doesn't quite make sense with what you described above though. Hrm.