【发布时间】:2016-01-04 07:32:18
【问题描述】:
我已经在 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();
当我尝试像这样通过 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);
}
【问题讨论】:
标签: java opengl graphics libgdx 2d-games