我将制作一个游戏,其中地面是一个随机生成的非凸多边形。
该多边形的顶点将传递给以下伪代码方法:
private void createPolygon (FloatArray vertices) {
short[] triangleIndices = new EarClippingTriangulator()
.computeTriangles(vertices)
.items;
myPolygon = new PolygonSprite(
new PolygonRegion(textureRegion, vertices.items, triangleIndices));
// myPolygon is correctly rendered
// now I want to create a Box2D static body with this shape
PolygonShape myShape = new PolygonShape();
Array<Vector2> vect = toVector2Array(vertices);
for (int i = 0; i < triangleIndices.length / 3; i++) {
// Error!
myShape.set(new float[] {
vect.get(triangleIndices[3 * i + 0]).x,
vect.get(triangleIndices[3 * i + 0]).y,
vect.get(triangleIndices[3 * i + 1]).x,
vect.get(triangleIndices[3 * i + 1]).y,
vect.get(triangleIndices[3 * i + 2]).x,
vect.get(triangleIndices[3 * i + 2]).y });
myBody.createFixture(myShape, 0);
}
}
通常这种方法很有效,但有时游戏会崩溃:
AL lib:(EE) alc_cleanup:1 device not closed 断言失败! 程序:C:\ Program Files \ Java \ jre1.8.0_25 \ bin \ javaw.exe 文件:/var/lib/jenkins/workspace/libgdx/extensions/gdx-box2d/gdx-box2d/jni/Box2D/Collision/Shapes/b2PolygonShape.cpp,第158行或223行 表达式:false 或者
表达式:area > 1.19209289550781250000e-7F
传递给“myShape.set()”的float []包含以下内容(当它崩溃时):
- x1: -1061.4121;
- y1: -2178.179;
- x2: 888.95154;
- y2: -154.1218;
- x3: 888.98663;
- y3: -154.08865;
如果我理解正确的话,原因是这个三角形太小了。 我该怎么做才能避免这个问题,或者去掉这些小三角形呢? 提前感谢。