gpt4 book ai didi

java - 使用 libgdx 的 PolygonRegion/PolygonSprite 时出现奇怪、不一致和故障的结果

转载 作者:行者123 更新时间:2023-11-30 07:53:40 25 4
gpt4 key购买 nike

我已经在 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);
}

我最终得到了奇怪的、不一致的和有问题的结果,如下所示: enter image description here...或者像这样: enter image description here

地形(纹理)是这样开始的: enter image description here

最佳答案

EarClippingTriangulator.computeTriangles() 的 JavaDoc 说:

Note the returned array is reused for later calls to the same method.

因此,您可能希望在将索引传递到 PolygonRegion 之前复制索引。

关于java - 使用 libgdx 的 PolygonRegion/PolygonSprite 时出现奇怪、不一致和故障的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32985947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com