所以我想解决我上一篇文章中描述的问题:Terrain / Mountain算法无法按预期工作。问题的副本如下:
我想使用一个非常基本的原理创建一个上面有山的地形,此高度图显示了这一点:
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 2 1 0 0 0 0
0 0 0 1 2 3 2 1 0 0 0
0 0 1 2 3 4 3 2 1 0 0
0 0 0 1 2 3 2 1 0 0 0
0 0 0 0 1 2 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
它从的一个随机点开始height = 4
,然后逐渐向邻域减小。
递归的想法很简单,我开始一点,用height - 1
(在此示例中)递归到上/下/左/右,只有在尚未遇到的情况下,我才设置它们的值。
因此,我继续使用BFS来实现它:
private void createMountain(final float[][] heightMapping, final float startHeight) {
boolean[][] traversed = new boolean[width][depth];
boolean positive = (startHeight >= 0f);
int x = random.nextInt(width);
int z = random.nextInt(depth);
PriorityQueue<QueueElement> priorityQueue = new PriorityQueue<>((o1, o2) -> (int)Math.signum(o1.height - o2.height));
priorityQueue.offer(new QueueElement(x, z, startHeight));
while (!priorityQueue.isEmpty()) {
QueueElement current = priorityQueue.poll();
if (current.x < 0 || current.x >= width || current.z < 0 || current.z >= depth) {
continue;
}
if (traversed[current.x][current.z]) {
continue;
}
if ((positive && current.height <= 0f) || (!positive && current.height >= 0f)) {
continue;
}
heightMapping[x][z] = current.height;
priorityQueue.offer(new QueueElement(x, z - 1, calculateNewHeight(current.height, positive)));
priorityQueue.offer(new QueueElement(x, z + 1, calculateNewHeight(current.height, positive)));
priorityQueue.offer(new QueueElement(x - 1, z, calculateNewHeight(current.height, positive)));
priorityQueue.offer(new QueueElement(x + 1, z, calculateNewHeight(current.height, positive)));
}
}
private class QueueElement {
public int x, z;
public float height;
public QueueElement(final int x, final int z, final float height) {
this.x = x;
this.z = z;
this.height = height;
}
}
private float calculateNewHeight(final float startHeight, final boolean positive) {
float delta = startHeight / maxDecayFactor;
return (positive) ? startHeight - delta : startHeight + delta;
}
现在代码永不停止,我尝试调试了几次,但是没有得到有用的结果,或者地形完全平坦。
我目前唯一真正的线索是,priorityQueue.size()
始终保持增加3。
有人知道发生了什么吗?
更新:即使修复了问题,它仍然无法按预期工作。
0.6 0.5 0.4 0.3 0.3 0.2 0.2 0.1 0.1 0.1 0.1 0.1 0.0 0.0 0.0 0.0
0.8 1.0 1.2 1.6 1.9 2.4 3.0 3.8 4.7 5.9 7.4 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
可能是什么问题?是PriorityQueue
现在搞乱的东西了?
您traversed[x][z] = true;
在轮询新元素时忘记设置。
确保在查询后执行此操作if (traversed[current.x][current.z]) ...
另外,请注意,对于您的实现,Queue
我认为a是一个更好的主意。(当我写一个PriorityQueue会更好时,我会想到多个高峰-对于您来说似乎并非如此,我认为一个简单的Queue会是最好的)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句