无法在Box2D(iphone)中设置“setAsEdge”。简单但不明白

时间:2011-03-08 17:17:17

标签: iphone box2d edge

首先是我的代码

// Define the ground body.
        b2BodyDef groundBodyDef;
        groundBodyDef.position.Set(0,0); // bottom-left corner

        // Call the body factory which allocates memory for the ground body
        // from a pool and creates the ground box shape (also from a pool).
        // The body is also added to the world.
        b2Body* groundBody = world->CreateBody(&groundBodyDef);

        // Define the ground box shape.
        b2PolygonShape groundBox;       

        // bottom
        groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0));
        groundBody->CreateFixture(&groundBox,0);

        // top
        groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO));
        groundBody->CreateFixture(&groundBox,0);

        // left
        groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
        groundBody->CreateFixture(&groundBox,0);

        // right
        groundBox.SetAsEdge(b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,0));
        groundBody->CreateFixture(&groundBox,0);

这将在iphone屏幕周围创建线条。但我希望底线应该在中间(我需要更改.y但我不知道如何)。 怎么做?

可能有人向我解释这些“setAsEdge”方法吗?

谢谢:)

1 个答案:

答案 0 :(得分:1)

// bottom

float screenMid = screenSize.height/2;  // Y axis on screen middle

        groundBox.SetAsEdge(b2Vec2(0,screenMid/PTM_RATIO),b2Vec2(screenSize.width/PTM_RATIO,screenMid/PTM_RATIO));

这会将你的底线转移到屏幕中间位置。 SetAsEdge方法需要两个点并从点-1到点-2绘制一条线。在上面的陈述中,第一点是“b2Vec2(0,screenMid / PTM_RATIO)”。其中0是x轴,screenMid是第一个点的y轴。第二点也是如此。

您必须通过PTM_RATIO划分每个点以将其转换为box2d坐标。