2

我面临着使用链形的新问题。两个链状体之间的碰撞并没有像正常体那样发生。所以这是正常行为还是我为此做错了什么?

以下是用于此目的的代码。

        ChainShape mChainShape = new ChainShape();
        Vector2[] mVector2 = new Vector2[lineList.size()];

        for (int i = 0; i < lineList.size(); i++) {
                mVector2[i] = new Vector2(lineList.get(i).getX1()
                                / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT,
                                lineList.get(i).getY1()
                                / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT);
        }

        mChainShape.createChain(mVector2);
        FixtureDef mFixtureDef = new FixtureDef();
        Body mChainBody;
        BodyDef mBodyDef = new BodyDef();
        mBodyDef.type = BodyType.DynamicBody;
        mChainBody = mPhysicsWorld.createBody(mBodyDef);
        mFixtureDef.shape = mChainShape;
        mFixtureDef.density = 1f;
        mFixtureDef.friction = 0.5f;
        mFixtureDef.restitution = 0.5f;
        mChainBody.createFixture(mFixtureDef);
        mChainShape.dispose();

如果我想根据触摸坐标创建身体,那么这件事可能吗?

请在这方面给予任何指导。

4

1 回答 1

0

BOX 2D 不支持链形碰撞。

但是,如果您的身体是多边形,您可以为此创建一个三角形近似,并从 b2PolygonShape 组成您的身体。

b2Vec2 triVerts[3];
// for this example polygon should have "center" point, from that it can be observed around. 
triVerts[0] = b2Vec2(0,0); // center
for(int idx = 1; idx < lineList.size(); idx++)
{
    b2PolygonShape triangle;
    fixtureDef.shape = &triangle;
    // Assumes the vertices are going around
    triVerts[1] = mVector2[idx-1];
    triVerts[2] = mVector2[idx];
    triangle.Set(triVerts,3);
    mChainBody.CreateFixture(&fixtureDef);
}

对于 b2PolygonShape 碰撞将正常工作。

于 2016-12-19T03:08:54.897 回答