Box2d 与 cocos2d 的联合

发布于 2024-10-05 02:20:25 字数 1584 浏览 1 评论 0原文

我是 box2d 的新手,我尝试在两个身体之间创建关节。 我写了一个像这样的关节

b2RevoluteJointDef jointDef;

        jointDef.bodyA=worm_head;
        jointDef.bodyB=worm_body;

        jointDef.lowerAngle = -0.25f * b2_pi; // -45 degrees

        jointDef.upperAngle = 0.25f * b2_pi; // 45 degrees
        jointDef.enableLimit=true;
        jointDef.maxMotorTorque = 10.0f;

        jointDef.motorSpeed = 10.0f;

        jointDef.enableMotor = true;
        joint=(b2DistanceJoint*)_world->CreateJoint(&jointDef);

,但是当头部移动时身体不移动。

我的勾选方法是

- (void)tick:(ccTime) dt {

    //we update the position of the b2body based on the sprite position
    for (b2Body* body = _world->GetBodyList(); body != nil; body = body->GetNext())
    {
        if (body->GetUserData() != NULL) {
            CCSprite *spritedata = (CCSprite *)body->GetUserData();

            if(spritedata.tag==1)
            {
                b2Vec2 b2Position = b2Vec2(SCREEN_TO_WORLD(spritedata.position.x),
                                   SCREEN_TO_WORLD(spritedata.position.y));
                float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(spritedata.rotation);
                body->SetTransform(b2Position,b2Angle);
            }
            else {
                spritedata.position = ccp(body->GetPosition().x * PTM_RATIO,
                                       body->GetPosition().y * PTM_RATIO);
                spritedata.rotation = -1 * CC_RADIANS_TO_DEGREES(body->GetAngle());
            }

        }

    }
}

为什么不动?我应该如何更改我的代码?

I'm new in box2d and I tried to create joint between two body.
I wrote a joint like

b2RevoluteJointDef jointDef;

        jointDef.bodyA=worm_head;
        jointDef.bodyB=worm_body;

        jointDef.lowerAngle = -0.25f * b2_pi; // -45 degrees

        jointDef.upperAngle = 0.25f * b2_pi; // 45 degrees
        jointDef.enableLimit=true;
        jointDef.maxMotorTorque = 10.0f;

        jointDef.motorSpeed = 10.0f;

        jointDef.enableMotor = true;
        joint=(b2DistanceJoint*)_world->CreateJoint(&jointDef);

but body is not moving when head is moving.

my tick method is

- (void)tick:(ccTime) dt {

    //we update the position of the b2body based on the sprite position
    for (b2Body* body = _world->GetBodyList(); body != nil; body = body->GetNext())
    {
        if (body->GetUserData() != NULL) {
            CCSprite *spritedata = (CCSprite *)body->GetUserData();

            if(spritedata.tag==1)
            {
                b2Vec2 b2Position = b2Vec2(SCREEN_TO_WORLD(spritedata.position.x),
                                   SCREEN_TO_WORLD(spritedata.position.y));
                float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(spritedata.rotation);
                body->SetTransform(b2Position,b2Angle);
            }
            else {
                spritedata.position = ccp(body->GetPosition().x * PTM_RATIO,
                                       body->GetPosition().y * PTM_RATIO);
                spritedata.rotation = -1 * CC_RADIANS_TO_DEGREES(body->GetAngle());
            }

        }

    }
}

Why is not moving ? How should I change my code ?

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

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

发布评论

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

评论(1

墨落画卷 2024-10-12 02:20:25

在 b2RevoluteJointDef 中,一个主体是静态主体,另一个主体是动态主体。我的问题是使用两个动态问题。现在,它解决了。

In b2RevoluteJointDef , one body is static body and another is dynamic body. My problem is using two dynamic problem. Now, it solved.

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