链形碰撞

时间:2013-07-30 17:12:35

标签: box2d andengine physics-engine

我通过使用链形来面对新问题。两个链状体之间的碰撞不像正常体那样发生。所以这是正常的行为还是我为此做错了?

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

        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();

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

请在此提供任何指导。

1 个答案:

答案 0 :(得分: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碰撞将正常工作。