我有一个要在其中添加box2d主体的桶。不是放在整个铲斗上,而是放在左侧,右侧和底部,这样我就可以将球扔进铲斗中。
这是我的水桶。我在水桶的左侧和右侧添加了box2d主体。像这样

这对我来说很好,但是当我在底部添加主体时,我的游戏崩溃了。

这是我的代码,用于在存储桶中添加2个主体以及注释底部主体。

-(b2Body *) createBucket
    {
        CCSprite* bucket = [CCSprite spriteWithFile:@"simple-bucket-md.png"];
        [self addChild:bucket z:3];

        b2Body* b_bucket;

        //set this to avoid updating this object in the tick schedule
        bucket.userData = (void *)YES;

        b2BodyDef bodyDef;
        bodyDef.type = b2_staticBody;
        CGPoint startPos = ccp(400,150);
        bodyDef.position = [self toMeters:startPos];
        bodyDef.userData = bucket;
        bodyDef.gravityScale = 0;

        b2PolygonShape dynamicBox;
            //----------------------------------
        // THis IS body for Left Side
            //----------------------------------
        int num = 5;
        b2Vec2 verts[] = {
            b2Vec2(-29.1f / PTM_RATIO, 25.0f / PTM_RATIO),
            b2Vec2(-25.4f / PTM_RATIO, -14.9f / PTM_RATIO),
            b2Vec2(-18.7f / PTM_RATIO, -14.9f / PTM_RATIO),
            b2Vec2(-21.8f / PTM_RATIO, 26.7f / PTM_RATIO),
            b2Vec2(-28.9f / PTM_RATIO, 25.1f / PTM_RATIO)
        };

        dynamicBox.Set(verts, num);

        b2FixtureDef fixtureDef;
        fixtureDef.shape = &dynamicBox;
        fixtureDef.friction = 0.7;
        fixtureDef.density = 10.0f;
        fixtureDef.restitution = 0.7;

        b_bucket = world->CreateBody(&bodyDef);
        b_bucket->CreateFixture(&fixtureDef);
        //----------------------------------
        // THis IS body for Right Side
        //----------------------------------
        int num1 = 5;
        b2Vec2 verts1[] = {
            b2Vec2(16.8f / PTM_RATIO, 27.0f / PTM_RATIO),
            b2Vec2(15.9f / PTM_RATIO, -11.5f / PTM_RATIO),
            b2Vec2(22.1f / PTM_RATIO, -10.7f / PTM_RATIO),
            b2Vec2(24.6f / PTM_RATIO, 26.9f / PTM_RATIO),
            b2Vec2(16.9f / PTM_RATIO, 26.7f / PTM_RATIO)
        };


        dynamicBox.Set(verts1, num1);
        fixtureDef.shape = &dynamicBox;
        b_bucket-> CreateFixture(&fixtureDef);

        //----------------------------------
        // THis IS body for Bottom
        //----------------------------------
        /*
        int num2 = 5;
        b2Vec2 verts2[] = {
            b2Vec2(-23.0f / PTM_RATIO, -21.6f / PTM_RATIO),
            b2Vec2(18.9f / PTM_RATIO, -21.0f / PTM_RATIO),
            b2Vec2(18.2f / PTM_RATIO, -26.1f / PTM_RATIO),
            b2Vec2(-22.8f / PTM_RATIO, -25.9f / PTM_RATIO),
            b2Vec2(-23.0f / PTM_RATIO, -21.7f / PTM_RATIO)
        };
        dynamicBox.Set(verts2, num2);
        fixtureDef.shape = &dynamicBox;
        b_bucket-> CreateFixture(&fixtureDef);
        */

        return b_bucket;
    }

最佳答案

您的顶点是顺时针排序的,而它们应该是逆时针的。

您必须创建具有逆时针缠绕(CCW)的多边形。我们必须小心,因为CCW的概念是相对于右手坐标系,z轴指向平面之外。根据坐标系统的约定,这可能在屏幕上显示为顺时针方向。


同样,为了创建4个面的多边形(看来您正在尝试),您仅需要4个顶点。尝试这样的事情。

int num2 = 4;
b2Vec2 verts2[] = {
    b2Vec2(-22.8f / PTM_RATIO, -25.9f / PTM_RATIO),
    b2Vec2(18.2f / PTM_RATIO, -26.1f / PTM_RATIO),
    b2Vec2(18.9f / PTM_RATIO, -21.0f / PTM_RATIO),
    b2Vec2(-23.0f / PTM_RATIO, -21.6f / PTM_RATIO)
};
dynamicBox.Set(verts2, num2);
fixtureDef.shape = &dynamicBox;
b_bucket-> CreateFixture(&fixtureDef);

请注意,您也可以删除横向实体的第五个顶点。

关于ios - 在Sprite Cocos2d上添加box2d主体,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19582260/

10-12 03:33