I'm making a game in which the floor is a randomly generated non-convex polygon.
The vertices of the polygon are passed to the following (pseudocode) method:
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);
}
}
Usually the method works well, but sometimes the game crashes:
AL lib: (EE) alc_cleanup: 1 device not closed
Assertion failed!
Program: C:\Program Files\Java\jre1.8.0_25\bin\javaw.exe
File: /var/lib/jenkins/workspace/libgdx/extensions/gdx-box2d/gdx-box2d/jni/Box2D/Collision/Shapes/b2PolygonShape.cpp, Line 158 or 223Expression: false
or
Expression: area > 1.19209289550781250000e-7F
The float [] passed to "myShape.set ()" contains (when it crashes):
- x1: -1061.4121;
- y1: -2178.179;
- x2: 888.95154;
- y2: -154.1218;
- x3: 888.98663;
- y3: -154.08865;
If I understand correctly, this is because the triangle is too small. What can I do to avoid this problem, or to leave out these small triangles? Thanks in advance