gpt4 book ai didi

java - 地形平滑算法不起作用...它通过多次运行将值带向正无穷大和负无穷大

转载 作者:行者123 更新时间:2023-12-02 04:38:26 30 4
gpt4 key购买 nike

我正在用java制作一个世界生成器。该算法的工作原理是,首先设置具有高点(范围)和低点(沟槽)的各种位置,然后通过遍历每个图 block 来平均,获取所有周围图 block 的高度,然后进行平均,然后设置所有周围图 block 到新的平均值。然后它会转到下一个图 block 并执行相同的操作,同时穿过 X 轴和 Y 轴。希望它将创建一个由范围和战壕位置动态生成的世界。通过查看一张简单的图片,它确实实现了这一点。

但是,问题始于算法运行多次,这是平滑 map 所必需的。值(value)观开始飙升,我不明白为什么会发生这种情况。第二次运行会显示这种效果 - 平均值函数应将值推向 0,而不是无穷大。

虽然地形看起来确实有点不稳定,但当通过简单的粗糙化算法运行时,它看起来不错。 (当前禁用)我可以稍后进行粗糙化,我需要弄清楚为什么平滑算法不起作用。

我了解 Perlin Noise 和 Simplex Noise 等工具,而且越来越清楚我可能需要开始使用它们,但我想弄清楚我的问题是什么。

因此,我跟踪了平均值单元格重叠这一事实的错误。平均图 block 0,0 包括所有周围的图 block ,但是当然,当移动到下一个图 block 时,它将从 0,0 获取新值并将其包含在函数中。但这就是我想要的。我希望平均值扩散到整个 map 上。我已经排除了溢出和下溢错误,平均值函数本身工作得很好。有趣的是,将值“乘”(应该是 9)乘以 1.1 可以稍微修复错误,但会使地形变得过于平坦。这显然只是给问题贴上创可贴。

public static void smoothHeight(int num) { 
for (int z = 0; z < 1; z++) { //Run through once. Edit to find optimal?
for (int i = 0; i < toSmooth.size(); i++) { //toSmooth will contain HALF of all values on the map. It will have values on row 0, 2, 4, etc.
Integer[] pos = toSmooth.get(i);
int x = pos[0];
int y = pos[1];
Tile thisTile = FinalMap[x][y];
double avgHeightForAll = thisTile.getHeight(); //avgHeightForAll is used to eventually smooth. THIS IS THE VALUE THAT SHOULD GET AVERAGED OUT
double times = 0;
for (int x1 = x - 1; x1 <= x + 1; x1 += 1) {
for (int y1 = y - 1; y1 <= y + 1; y1 += 1) { //Go through tiles around it, and add to avg
Integer[] posToSmooth = smoothAlgFind(new Integer[] {x1, y1}, 1); //smoothAlgFind ensures that tiles across the map are included. For example, if smoothing at position 0, it will include position 499 in the smoothing, not -1
Tile tileToSmooth = FinalMap[posToSmooth[0]][posToSmooth[1]];
if (tileToSmooth.isCoastal == true || num == 0) {
double smoothTileh = tileToSmooth.getHeight();
avgHeightForAll += smoothTileh;
times++;
}
}
}
DecimalFormat df = new DecimalFormat("0.00");
avgHeightForAll /= (times); //DONT MESS WITH 1.17, REMOVING IT BREAKS EVERYTHING. NO I DONT KNOW WHY. times = 9
//thisTile.setHeight(avgHeightForAll, 0);
avgHeightForAll = Double.parseDouble(df.format(avgHeightForAll));
if (Math.abs(avgHeightForAll) > 40000) { //if something goes wrong this will trigger.
System.out.println((avgHeightForAll * times) + " / " + times + " = " + (avgHeightForAll));
}
for (int x1 = x - 1; x1 <= x + 1; x1 += 1) {
for (int y1 = y - 1; y1 <= y + 1; y1 += 1) {
Integer[] posToSmooth = smoothAlgFind(new Integer[] {x1, y1}, 1);
Tile tile = FinalMap[posToSmooth[0]][posToSmooth[1]];
if (tile.isCoastal == true || num == 0) {//don't worry about isCoastal, has been disabed for now
tile.setHeight(avgHeightForAll, 0);
}
}
}
}


for (int i = toSmooth.size() - 1; i >= 0; i--) { //now we go the other way to prevent scerw-y looking generation skewed to one side
Integer[] pos = toSmooth.get(i);
int x = pos[0];
int y = pos[1];
Tile thisTile = FinalMap[x][y];
double avgHeightForAll = thisTile.getHeight(); //avgHeightForAll is used to eventually smooth
double times = 0;
for (int x1 = x - 1 - p; x1 <= x + 1 + p; x1 += 1) {
for (int y1 = y - 1 - p; y1 <= y + 1 + p; y1 += 1) { //Go through tiles around it, and add to avg
Integer[] posToSmooth = smoothAlgFind(new Integer[] {x1, y1}, 1);
Tile tileToSmooth = FinalMap[posToSmooth[0]][posToSmooth[1]];
if (tileToSmooth.isCoastal == true || num == 0) { //don't worry about isCoastal, has been disabed for now
double smoothTileh = tileToSmooth.getHeight();
avgHeightForAll += smoothTileh;
times++;
}
}
}

DecimalFormat df = new DecimalFormat("0.00");
avgHeightForAll = Double.parseDouble(df.format(avgHeightForAll));
avgHeightForAll /= (times); //DONT MESS WITH 1.17, REMOVING IT BREAKS EVERYTHING. NO I DONT KNOW WHY.
//thisTile.setHeight(avgHeightForAll, 0);
avgHeightForAll = Double.parseDouble(df.format(avgHeightForAll));
//System.out.println("avgHeightForAll" + avgHeightForAll);
if (Math.abs(avgHeightForAll) > 40000) { //if something goes wrong this triggers!
System.out.println((avgHeightForAll * times) + " / " + times + " = " + (avgHeightForAll));
}
for (int x1 = x - 1; x1 <= x + 1; x1 += 1) {
for (int y1 = y - 1; y1 <= y + 1; y1 += 1) {
Integer[] posToSmooth = smoothAlgFind(new Integer[] {x1, y1}, 1);
Tile tile = FinalMap[posToSmooth[0]][posToSmooth[1]];
if (tile.isCoastal == true || num == 0) {
tile.setHeight(avgHeightForAll, 0);
}
}
}
}
}
System.out.println("Smoothed");

}

最佳答案

正如您所指出的,错误是使用了已计算的图 block 的先前平均值。这会扭曲平均值。

完美的平滑操作不会改变数据的总和或平均值。

正确的解决方案是将 hightmap 数据复制到其自己的二维 double 组中:

double[][] heightmapData = new double[xmax][ymax];
for (int x = 0; x < xmax; x++)
for (int y = 0; y < ymax; y++)
heightmapData[x][y] = FinalMap[x][y].getHeight();

然后根据其自身和周围图 block 的平均值计算平滑值,不包括边缘(任何边缘和像 isCostal 这样的固定图 block 都会使总数倾斜,但倾斜将局限于周围图 block ):

for (int x = 1; x < xmax - 1; x++)
for (int y = 1; y < ymax - 1; y++)
FinalMap[x][y].setHeight((
heightmapData[x - 1][y - 1] +
heightmapData[x][y - 1] +
heightmapData[x + 1][y - 1] +
heightmapData[x][y] +
heightmapData[x - 1][y + 1] +
heightmapData[x][y + 1] +
heightmapData[x + 1][y + 1]) / 9.0);

使用Tile.setHeight()忽略固定的图 block 更新。这将使以后更改代码变得更加容易。

关于java - 地形平滑算法不起作用...它通过多次运行将值带向正无穷大和负无穷大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56533748/

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