将花栗鼠身体移动到精灵位置

发布于 2024-09-12 02:48:19 字数 274 浏览 3 评论 0原文

我有一个花栗鼠的形状,有一个身体,在一个空间里。我正在将身体从空间中移开,以便我可以定位它并且不会使其因重力等而掉落。我需要能够使这个身体移动,所以我不会使其静止。

我需要主体根据场景中 Cocos2D 精灵的位置 + 偏移量来更新其位置。

我设置身体位置:

collShape->body->p = collSprite.position; - 这似乎不起作用,不是编译错误,它运行,但碰撞形状不移动。

是否可以根据我的勾选方法中精灵的位置来移动碰撞体?

I have a Chipmunk shape, with a body, in a space. I am removing the body from the space so that 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所的.畏惧 2024-09-19 02:48:19

你正在做的事情应该是可能的。

最简洁的方法是创建一个从 CCSprite 派生的新类,然后重写 setPosition 方法来更新精灵的主体。

这样做的优点是,只要精灵的位置发生变化(无论是由您或任何动画序列明确更改),花栗鼠的身体都会自动更新。

-(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
    }
}

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
    }
}
北音执念 2024-09-19 02:48:19

当您调用 cpSpaceStep 时,将创建活动形状列表,并为每个形状调用 cpShapeUpdateFunc。该函数如下所示:

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

...它将形状更新为它所附加的身体位置和旋转。如果这种情况没有发生,也许您的形状尚未添加到空间或尚未添加到主体?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文