三角剖分弄乱了三角形
triangulation messing up triangles
我正在尝试对凸多边形进行三角剖分。
左边是三角化的多边形,右边是没有三角化的凸多边形。 (都错了)但是右边的那个位置是对的,除了少了凸的部分,还不错...
我创建三角形并将它们添加到数组列表
float[] vertices = polygonObject.getPolygon().getTransformedVertices();
float[] worldVertices = new float[vertices.length];
for (int i = 0; i < vertices.length; ++i) {
System.out.println(vertices[i]);
worldVertices[i] = vertices[i] / ppt;
}
// Make triangles
Vector<float[]> trianglesVertices = new Vector<float[]>();
EarClippingTriangulator triangulator = new EarClippingTriangulator();
ArrayList<PolygonShape> triangles = new ArrayList<PolygonShape>();
ShortArray pointsCoords = triangulator.computeTriangles(worldVertices);
for (int i = 0; i < pointsCoords.size / 6; i++)
{
trianglesVertices.add(new float[] {
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),
});
PolygonShape triangleShape = new PolygonShape();
triangleShape.set(trianglesVertices.get(i));
triangles.add(triangleShape);
}
稍后我循环那个 arraylist 并创建主体,但不知何故 libgdx(可能是我,而不是 libgdx)完全搞砸了。
for(PolygonShape triangle:triangles){
BodyDef bd = new BodyDef();
bd.type = BodyDef.BodyType.StaticBody;
Body body = world.createBody(bd);
body.createFixture(triangle, 1);
bodies.add(body);
triangle.dispose();
}
这是我要在 libgdx 中渲染的形状(在 Tiled 中创建)
我找到了另一个例子,它成功了!
我从这个数组创建三角形的方式是错误的
ShortArray pointsCoords = triangulator.computeTriangles(worldVertices);
这是正确创建三角形的代码,
for (int i = 0; i < pointsCoords.size; i += 3) {
int v1 = pointsCoords.get(i) * 2;
int v2 = pointsCoords.get(i + 1) * 2;
int v3 = pointsCoords.get(i + 2) * 2;
PolygonShape triangleShape = new PolygonShape();
triangleShape.set(
new float[]{
worldVertices[v1 + 0],
worldVertices[v1 + 1],
worldVertices[v2 + 0],
worldVertices[v2 + 1],
worldVertices[v3 + 0],
worldVertices[v3 + 1]
}
);
triangles.add(triangleShape);
}
我正在尝试对凸多边形进行三角剖分。
左边是三角化的多边形,右边是没有三角化的凸多边形。 (都错了)但是右边的那个位置是对的,除了少了凸的部分,还不错...
我创建三角形并将它们添加到数组列表
float[] vertices = polygonObject.getPolygon().getTransformedVertices();
float[] worldVertices = new float[vertices.length];
for (int i = 0; i < vertices.length; ++i) {
System.out.println(vertices[i]);
worldVertices[i] = vertices[i] / ppt;
}
// Make triangles
Vector<float[]> trianglesVertices = new Vector<float[]>();
EarClippingTriangulator triangulator = new EarClippingTriangulator();
ArrayList<PolygonShape> triangles = new ArrayList<PolygonShape>();
ShortArray pointsCoords = triangulator.computeTriangles(worldVertices);
for (int i = 0; i < pointsCoords.size / 6; i++)
{
trianglesVertices.add(new float[] {
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),
});
PolygonShape triangleShape = new PolygonShape();
triangleShape.set(trianglesVertices.get(i));
triangles.add(triangleShape);
}
稍后我循环那个 arraylist 并创建主体,但不知何故 libgdx(可能是我,而不是 libgdx)完全搞砸了。
for(PolygonShape triangle:triangles){
BodyDef bd = new BodyDef();
bd.type = BodyDef.BodyType.StaticBody;
Body body = world.createBody(bd);
body.createFixture(triangle, 1);
bodies.add(body);
triangle.dispose();
}
这是我要在 libgdx 中渲染的形状(在 Tiled 中创建)
我找到了另一个例子,它成功了!
我从这个数组创建三角形的方式是错误的
ShortArray pointsCoords = triangulator.computeTriangles(worldVertices);
这是正确创建三角形的代码,
for (int i = 0; i < pointsCoords.size; i += 3) {
int v1 = pointsCoords.get(i) * 2;
int v2 = pointsCoords.get(i + 1) * 2;
int v3 = pointsCoords.get(i + 2) * 2;
PolygonShape triangleShape = new PolygonShape();
triangleShape.set(
new float[]{
worldVertices[v1 + 0],
worldVertices[v1 + 1],
worldVertices[v2 + 0],
worldVertices[v2 + 1],
worldVertices[v3 + 0],
worldVertices[v3 + 1]
}
);
triangles.add(triangleShape);
}