Seb*_*uze 5 polygon concave triangulation convex libgdx
好的,所以我有一个多边形(简单但凹形),我试图将其切成三角形以使其与其他多边形碰撞。
我知道我的多边形是凹面的,所以我决定使用 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);
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试绘制我刚刚制作的那些三角形时,它们只是在 0,0 坐标中堆叠......而且,所有三角形看起来几乎相同是否正常,我的意思是它们都有相同的方向?
我没有找到太多关于 libgdx 的这种截断用途的信息你能帮忙吗?
(对不起我的英语我是法国人,对不起没有照片,我在这里太年轻了)
编辑:这是我的多边形(逆时针)
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
});
Run Code Online (Sandbox Code Playgroud)
EDIT2:现在我有足够的点来向您展示这里的多边形

这里的问题EarClippingTriangulator.computeTriangles是返回的输出与您预期的不同。它返回一个索引数组,其中每个索引代表您最初传入的数组中的一个顶点。所以如果你传入一个大小为 8 的数组,那将代表 4 个顶点;返回的数组大小为 6(三角形 1 为 3 个顶点,三角形 2 为 3 个顶点)。
假设您有一个 2 x 3 矩形,左下角位于 (5, 5)。您的顶点将是:
您可以将它们压平为一个数组并将它们传递给computeTriangles:
[5, 5, 7, 5, 7, 10, 5, 10]
返回的数组看起来像这样:
[0, 3, 2, 2, 1, 0]
返回数组中的每组三个索引形成一个三角形。这些值中的每一个都表示您传入的数据中顶点的索引。在本例中,输出将告诉您使用这些顶点:
所以你需要绘制这些三角形:
请注意,这不一定是您使用我提供的输入获得的实际输出,它只是一个示例,旨在说明您应该如何使用返回的数据。
这是我最终得到的代码,它将正确绘制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();
}
Run Code Online (Sandbox Code Playgroud)
问题出在你的循环上:
// 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);
}
Run Code Online (Sandbox Code Playgroud)
第一个三角形将具有正确的坐标,但第二个三角形将使用 1、2、3、4、5、6 个元素,这pointsCoords没有任何意义。您应该i在循环内乘以 6 以考虑偏移量:
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),
Run Code Online (Sandbox Code Playgroud)