Reputation: 297
I'm making a randomly generated tile game in MonoGame, and I'm trying to use Simplex Noise to generate the terrain. Problem is, I've never used Simplex Noise before, so as you can probably guess, my code doesn't work. It only creates grass tiles. Here's the code I've tried:
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;
}
I'm using this implementation to generate noise.
Upvotes: 1
Views: 1347
Reputation: 566
Check line 133 in the SimplexNoise you are using:
// The result is scaled to return values in the interval [-1,1].
After you divide it by 10, the result would be in range from -0.1 to +0.1 You need a range from 0 to 1, so instead of dividing by 10, you need to:
float value = (Noise.Generate((x / Width) * Seed, (y / Height) * Seed) + 1) / 2.0f;
Or change your if/else to work with -1 to +1 ranges.
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)));
}
Upvotes: 2