【问题标题】:Box2D Android Studio Drawing inverted shapesBox2D Android Studio 绘制倒置形状
【发布时间】:2016-06-07 16:07:06
【问题描述】:

我正在使用 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;

    }

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

【问题讨论】:

    标签: java android android-studio libgdx box2d


    【解决方案1】:

    问题不在于 Box2D,而在于 您用于绘制三角形的坐标。无论您以什么顺序将它们传递给 Fixture,它们都会产生如下三角形:

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

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

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

    【讨论】:

    • 谢谢!它就像一个魅力!如果我的代码看起来像这样,三角形就会倒置!:vertices[2] = new Vector2(-0.5f, 0.5f);顶点[1] = new Vector2(0.5f, 0.5f);顶点[0] = new Vector2(0, -0.5f);
    • 绝对!竖起大拇指!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    相关资源
    最近更新 更多