Box2D Android Studio 绘制倒置形状

Box2D Android Studio Drawing inverted shapes

我正在使用 Android Studio 和 LibGDX 创建一个碰撞测试游戏,我在游戏集上拥有我需要的一切,但是我想创建一个像这样的倒三角形Box2D,显然,它不会看起来像这样,但这张图片是给你一个例子

然而当我 运行 它看起来像这样:

我试过更改 Box2D 中代码的顺序,但它没有创建倒三角形,如下所示:

private Fixture createcollisionFixture(Body collisionBody) {
        Vector2[] vertices = new Vector2[3];
        vertices[0] = new Vector2(-0.5f, -0.5f);
        vertices[1] = new Vector2(0.5f, -0.5f);
        vertices[2] = new Vector2(0, 0.5f);
        PolygonShape shape = new PolygonShape();
        shape.set(vertices);
        Fixture fix = collisionBodyBody.createFixture(shape, 1);
        shape.dispose();
        return fix;
    }

我改成:

private Fixture createcollision2Fixture(Body collision2Body) {
        Vector2[] vertices = new Vector2[3];
        vertices[2] = new Vector2(0, 0.5f);
        vertices[1] = new Vector2(0.5f, -0.5f);
        vertices[0] = new Vector2(-0.5f, -0.5f);
        PolygonShape shape = new PolygonShape();
        shape.set(vertices);
        Fixture fix = collision2Body.createFixture(shape, 1);
        shape.dispose();
        return fix;

    }

但是一直加载第二张图,不是倒三角形,请问怎么解决?将不胜感激。

问题不在 Box2D 中,而是在您用来绘制三角形的坐标中。无论您以什么顺序将它们传递给固定装置,它们都会产生这样的三角形:

如果你想旋转三角形,你必须像这样传递另一组坐标:

最后你的代码应该是这样的:

    ...

    Vector2[] vertices = new Vector2[3];
    vertices[2] = new Vector2(-0.5, 0.5f);
    vertices[1] = new Vector2(0.5f, 0.5f);
    vertices[0] = new Vector2(0.5f, -0.5f);

    ...