Box2D Android Studio绘图倒置形状

时间:2016-02-25 03:48:35

标签: java android android-studio libgdx box2d

我正在使用Android Studio和LibGDX创建一个崩溃测试游戏,我拥有游戏装置所需的一切,但是我想在Box2D中创建一个颠倒的三角形,显然,它是'不会看起来像这样,但这张图片是给你一个例子Example of my goal on Box2D

然而,当我运行它时,它看起来像这样: Actual result

我尝试更改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;

    }

但它继续作为第二张图片加载,而不是倒三角形,我该如何解决这个问题?非常感谢帮助。

1 个答案:

答案 0 :(得分:1)

问题不在Box2D中,而是用于绘制三角形的坐标。无论你以什么顺序将它们传递给夹具,它们都会产生这样的三角形:

enter image description here

如果要旋转三角形,则必须传递另一组坐标,如下所示:

enter image description here

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

    ...

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

    ...