Perlin噪声不会产生介于1和-1之间的数字

时间:2016-07-30 17:56:38

标签: java libgdx

我正在使用 LibGDX 开发Java游戏,我想要随机地形生成。我在网上找到了一个 Perlin 课程,并决定将它用于我的项目。

我目前有一个简单的世界级作为构造函数:

public World() {
    Random rand = new Random();
    seed = rand.nextInt();
    Perlin perlin = new Perlin(seed);

    for(int x = 0; x < map.length; x++) {
        for(int y = 0; y < map.length; y++) {
            map[x][y] = perlin.noise2(x, y);
        }
    }
}

(地图是一个2d浮动数组)

在我的游戏主要课程中,我创建了一个世界,然后使用render()方法绘制它,如下所示:

batch.begin();
for (int x = 0; x < w.getMap().length; x++) {
    for (int y = 0; y < w.getMap().length; y++) {
        if (w.getMap()[x][y] <= 0) {
            batch.draw(water, x * 32, y * 32);
        }
        if (w.getMap()[x][y] > 0) {
            batch.draw(grass, x * 32, y * 32);
        } else {
            log.severe("not between -1 and 1");             
        }
    }
}
batch.end();

执行时,它基本上使用&#34;而不是-1和1&#34;之间的控制台。过了一段时间,屏幕上充满了水纹。

是上课还是我搞砸了?可能是后者。我该如何解决?还有,关于优化的任何提示?代码有点看起来很乱。

This is the class I'm using. Posted it on pastebin because it's ~580 lines long.

更新:我修复了垃圾邮件,这是Rogue指出的一个愚蠢的错误。

现在我面临着另一个问题。它似乎只是在发电。我检查了getMap()[x][y]返回的内容,基本上只有0.0s和-0.0s。

1 个答案:

答案 0 :(得分:2)

嗯,我认为显而易见的是你的 - 如果逻辑关闭,你连续使用两个if语句:

    if (w.getMap()[x][y] <= 0) {
        batch.draw(water, x * 32, y * 32);
    }
    if (w.getMap()[x][y] > 0) {
        batch.draw(grass, x * 32, y * 32);
    } else {
        log.severe("not between -1 and 1");             }
    }

括号对齐也有点不合适,但请尝试使用if else if else

    if (w.getMap()[x][y] <= 0) {
        batch.draw(water, x * 32, y * 32);
    } else if (w.getMap()[x][y] > 0) {
        batch.draw(grass, x * 32, y * 32);
    } else {
        log.severe("not between -1 and 1");
    }

以前任何低于0的内容都会打印出该错误。