0

我目前正在开发一款必须在 iPhone 屏幕上绘制一系列气泡的游戏。每个气泡都有自己的花栗鼠身体,它是使用名为 PhysicsEditor 的多边形物理编辑器绘制的。有了它,我创建了一个 .plist 文件,其中包含创建花栗鼠身体所需的信息(质量、弹性、锚点等)。

下面是我用来创建具有给定坐标的气泡的代码。

- (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 中,只有 1 个主体,名为“BubbleSlot”,用作创建游戏中所有气泡的模板。

身体的锚点约为 (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.

4

2 回答 2

1

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.

于 2012-12-15T21:28:08.533 回答
1

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.

于 2012-12-16T02:42:00.103 回答