开发者

Move Chipmunk Body to Sprite position

I have a Chipmunk shape, with a body, in a space. I am removing the body from the space so t开发者_如何转开发hat I can position it and not have it fall due to gravity etc. I need to be able to make this body move, so I am not making it static.

I need the body to update it's position according to the position of a Cocos2D sprite in the scene + an offset.

I'm setting the bodies position with:

collShape->body->p = collSprite.position; - this seems to not work, not compile errors, it runs, but the collision shape doesn't move.

Is it possible to move a collision body based upon the position of a sprite in my tick method?


What you're doing should be possible.

Cleanest way is to create a new class that derives from CCSprite and then override the setPosition method to update the sprite's body.

The advantage of this, is that anytime the sprite's position is changed (either explicitly by you or by any animation sequence) the Chipmunk body will automatically get updated.

-(void) setPosition:(CGPoint) p{
    [super setPosition:p];
    if (self->body != nil) {
        self->body->p.x = p.x;
        self->body->p.y = p.y;
        //Note: also call cpSpaceRehash to let Chipmunk know about the new position
    }
}


When you call cpSpaceStep, a list of active shapes is created and cpShapeUpdateFunc is called for each. That function looks like:

void
cpShapeUpdateFunc(cpShape *shape, void *unused)
{
    cpBody *body = shape->body;
    cpShapeUpdate(shape, body->p, body->rot);
}

...which updates the shape to the body location and rotation it's attached to. If that's not happening maybe your shape has not been added to the space or has not been added to the body?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜