我在libgdx和box2d中构建了一个可破坏的地形,现在我想从纹理中渲染剩余的(非破坏的)地形。每次box2d身体受到"爆炸" (移除地形的一部分)移除主体并使用新的多边形重新制作。我认为libgdx的PolygonSprite非常适合这项任务,但我一直遇到问题。
在这个例子中,我创造了一个" U"在地形上有圆形"爆炸"。
此代码完美无缺:
protected Body createBody(Polygon inputPolygon) {
...
body.setUserData(inputPolygon.getVertices());
...
}
//Later in the render method
sr.setProjectionMatrix(cameraMatrix); //ShapeRenderer sr = new ShapeRenderer();
sr.begin(ShapeRenderer.ShapeType.Line);
Array<Body> bodies = new Array<Body>();
world.getBodies(bodies);
sr.setColor(Color.BLACK);
for (Body b : bodies) {
sr.polygon((float[]) b.getUserData());
}
sr.end();
当我尝试渲染&#34;地形&#34;使用像PolygonSprites(或PolygonRegions)这样的纹理:
protected Body createBody(Polygon inputPolygon) {
...
//triangulator is a EarClippingTriangulator
ShortArray indices = triangulator.computeTriangles(inputPolygon.getVertices());
//The TextureRegion is 100x40 px and the inputPolygon represents a part of this texture (the vertices/coordinates are
//in the same scale, so an inputPolygon with the vertices (0, 0 100, 0 100, 40, 0, 40) would represent the whole TextureRegion.
PolygonSprite polygonSprite = new PolygonSprite(new PolygonRegion(terrainTexture, inputPolygon.getVertices(), indices.items));
polygonSprite.setOrigin(0, 0);
body.setUserData(polygonSprite);
...
}
//Later in the render method
Array<Body> bodies = new Array<Body>();
world.getBodies(bodies);
for (Body b : bodies) {
//Yes the screen is cleared correctly before, the batch is a
//PolygonSpriteBatch, the batch is started with begin() and end()
//and the projection matrix is set correctly.
((PolygonSprite) b.getUserData()).draw(batch);
}
答案 0 :(得分:1)
EarClippingTriangulator.computeTriangles()的JavaDoc说:
请注意,返回的数组将重复用于以后对同一方法的调用。
因此,您可能希望在将索引传递给PolygonRegion之前复制索引。