Libgdx多边形三角

问题描述 投票:4回答:2

好了,我有我试图切成三角形,使其与另一多边形碰撞多边形(简单但凹)。

我知道我的polygone是凹的,所以我决定用LibGDX EarClippingTriangulator来管理它切成三角形。

所以,用这个代码,我让我的三角形顶点:

public void triangulate()
    {
        Vector<float[]> trianglesVertices = new Vector<float[]>();
        ShortArray pointsCoords = new ShortArray();
        EarClippingTriangulator triangulator = new EarClippingTriangulator();

        // Cut in triangles
        pointsCoords = triangulator.computeTriangles(this.getTransformedVertices());

        // Make triangles
        for (int i = 0; i < pointsCoords.size / 6; i++)
        {
            trianglesVertices.add(new float[] {
                    pointsCoords.get(i), pointsCoords.get(i+1),
                    pointsCoords.get(i+2), pointsCoords.get(i+3),
                    pointsCoords.get(i+4), pointsCoords.get(i+5),
            });
            Polygon triangle = new Polygon(trianglesVertices.get(i));
            triangles.add(triangle);
        }

        System.out.printf("Triangulation made %d triangles.\n", pointsCoords.size / 6);
    }

但是,当我尝试绘制这些三角形我只是做了,他们只是堆在0,0坐标。并且,这是正常的,所有的三角形似乎差不多了,我的意思是,他们都得到了相同的方向是什么?

我没有找到这个trangulation使用了libgdx你能帮助这么多信息?

(对不起,我的英语我是法国人,并抱歉没有照片,我太年轻这里)

编辑:这是我的多边形(在CCW)

hitbox.setVertices(new float[]{  
                this.getX() + 13, this.getY() - 60,
                this.getX() + 16, this.getY() - 74,
                this.getX() + 39, this.getY() - 74,
                this.getX() + 45, this.getY() - 105,
                this.getX() + 81, this.getY() - 105,
                this.getX() + 88, this.getY() - 74,
                this.getX() + 108, this.getY() - 74,
                this.getX() + 114, this.getY() - 61,
                this.getX() + 106, this.getY() - 30, // Top right
                this.getX() + 101, this.getY() - 29,
                this.getX() + 101, this.getY() - 57,
                this.getX() + 83, this.getY() - 62,
                this.getX() + 75, this.getY() - 50,
                this.getX() + 65, this.getY() - 4, // Top mid
                this.getX() + 62, this.getY() - 4, // Top mid
                this.getX() + 52, this.getY() - 50,
                this.getX() + 44, this.getY() - 62,
                this.getX() + 25, this.getY() - 56, 
                this.getX() + 25, this.getY() - 30,
                this.getX() + 19, this.getY() - 30,  // Top left
                });

EDIT2:现在我有足够的点这里是你展示多边形

libgdx polygon triangulation convex concave
2个回答
1
投票

问题是,你的循环:

        // Make triangles
        for (int i = 0; i < pointsCoords.size / 6; i++)
        {
            trianglesVertices.add(new float[] {
                    pointsCoords.get(i), pointsCoords.get(i+1),
                    pointsCoords.get(i+2), pointsCoords.get(i+3),
                    pointsCoords.get(i+4), pointsCoords.get(i+5),
            });
            Polygon triangle = new Polygon(trianglesVertices.get(i));
            triangles.add(triangle);
        }

第一三角形将具有正确的坐标,但第二个将将使用1,2,3,4,5,pointsCoords的6个元素,这并不使任何SENCE。你应该通过6内部循环繁殖i要采取的弥补考虑:

                    pointsCoords.get(i*6), pointsCoords.get(i*6 + 1),
                    pointsCoords.get(i*6 + 2), pointsCoords.get(i*6 + 3),
                    pointsCoords.get(i*6 + 4), pointsCoords.get(i*6 + 5),

3
投票

这里的问题是,EarClippingTriangulator.computeTriangles比你期望返回不同的输出。它返回索引数组,其中每个索引代表你在最初传递数组中的顶点。所以,如果你在大小为8的数组,这将是4个顶点通过;阵列返回将是大小6(3个顶点的三角形1,3顶点三角形2)。

比方说,你有一个2×3长方形左下方的(5,5)。你的顶点将是:

  • 顶点0:(5,5)
  • 顶点1:(7,5)
  • 顶点2:(7,10)
  • 顶点3:(5,10)

你会压扁那些下降到阵列,并将它们传递到computeTriangles为:

[5, 5, 7, 5, 7, 10, 5, 10]

返回的数组将是这个样子:

[0, 3, 2, 2, 1, 0]

返回的数组中的每个组的三个指数形成一个三角形。每个值代表了你传入的数据顶点的指数在这个例子中,输出会告诉你使用这些顶点:

  • 三角形1:顶点0,顶点3,顶点2
  • 三角形2:顶点2,顶点1,顶点0

所以,你需要绘制这些三角形:

  • 三角形1:(5,5) - >(5,10) - >(7,10)
  • 三角形2:(7,10) - >(7,5) - >(5,5)

请注意,这不一定是实际的输出,你会用我提供的投入得到的,它只是用来说明你应该如何使用这些数据返回的样本。

这里是我结束了,这将正确绘制Polygon情况下,虽然可以做同样的事情与任何合适的配方顶点数组的代码:

private void drawFilledPolygon(Polygon polygon, Color color) {
    shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
    shapeRenderer.setColor(color);

    float[] vertices = polygon.getTransformedVertices();

    // NOTE: you probably don't want to create a new EarClippingTriangulator each frame
    ShortArray triangleIndices = new EarClippingTriangulator().computeTriangles(vertices);
    for (int i = 0; i < triangleIndices.size; i += 3) {
        shapeRenderer.triangle(
            vertices[triangleIndices.get(i) * 2], vertices[triangleIndices.get(i) * 2 + 1],
            vertices[triangleIndices.get(i + 1) * 2], vertices[triangleIndices.get(i + 1) * 2 + 1],
            vertices[triangleIndices.get(i + 2) * 2], vertices[triangleIndices.get(i + 2) * 2 + 1]
        );
    }

    shapeRenderer.end();
}
© www.soinside.com 2019 - 2024. All rights reserved.