1

我有一个桶,我想在其中添加 box2d body 。不是在整个桶上,而是在左、右和底部,这样我就可以把球扔进桶里。这是我的桶。我在桶的左侧和右侧添加了 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;
    }
4

1 回答 1

2

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

您必须创建具有逆时针缠绕 (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);

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

于 2013-10-25T07:48:23.510 回答