[英]SimplexNoise terrain in MonoGame
我在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;
}
我正在使用此实现来产生噪声。
检查您使用的SimplexNoise中的第133行:
// The result is scaled to return values in the interval [-1,1].
将其除以10后,结果将在-0.1到+0.1范围内。您需要在0到1的范围内,因此,除以10之外,您需要:
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)));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.