1

我正在 MonoGame 中制作一个随机生成的瓷砖游戏,并且我正在尝试使用 Simplex Noise 来生成地形。问题是,我以前从未使用过 Simplex Noise,所以您可能会猜到,我的代码不起作用。它只创建草瓷砖。这是我尝试过的代码:

public void Generate() {
    Tiles = new List<Tile>();
    Seed = GenerateSeed();
    for (int x = 0; x < Width; x++) {
        for (int y = 0; y < Height; y++) {
            float value = Noise.Generate((x / Width) * Seed, (y / Height) * Seed) / 10.0f;
            if (value <= 0.1f) {
                Tiles.Add(new Tile(Main.TileGrass, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
            }
            else if (value > 0.1f && value <= 0.5f) {
                Tiles.Add(new Tile(Main.TileSand, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
            }
            else {
                Tiles.Add(new Tile(Main.TileWater, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
            }
        }
    }
}

public int GenerateSeed() {
    Random random = new Random();
    int length = 8;
    int result = 0;

    for (int i = 0; i < length; i++) {
        result += random.Next(0, 9);
    }

    return result;
}

我正在使用这个实现来产生噪音。

4

1 回答 1

2

检查您正在使用的 SimplexNoise 中的第 133 行:

// The result is scaled to return values in the interval [-1,1].

将其除以 10 后,结果将在 -0.1 到 +0.1 的范围内。您需要一个从 0 到 1 的范围,因此您不需要除以 10,而是需要:

  • 加 1(范围为 0 到 2)。
  • 除以 2(范围为 0 到 1)。

float value = (Noise.Generate((x / Width) * Seed, (y / Height) * Seed) + 1) / 2.0f;

或者更改您的 if/else 以使用 -1 到 +1 范围。

if (value <= -0.8f)
{
    Tiles.Add(new Tile(Main.TileGrass, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
else if (value <= 0)
{
    Tiles.Add(new Tile(Main.TileSand, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
else
{
    Tiles.Add(new Tile(Main.TileWater, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
于 2015-05-17T14:16:05.807 回答