patternjavaModerate
Game Terrain Generation based on Images
Viewed 0 times
generationgamebasedimagesterrain
Problem
Lately I've been working on adding Scenarios to my city building game. I wanted to create custom terrains for some of the different scenarios (such as a chain of islands, or a volcano). I decided that the simplest solution was to try to use topographic images to create an array of integers that could then be converted to heights and different terrains.
Since there are so many different types of colors in topographical maps, I eventually decided to go with a greyscale approach. It turned out that using real topographical maps in my game did not work out so well, so I generated a few black and white images to use for the terrain generation.
Here are a couple sample input images and their resulting terrains:
Here is the code:
Here is the output of the print method:
```
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-00-01-01-02-02-01-01-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-01-02-03-04-04-04
Since there are so many different types of colors in topographical maps, I eventually decided to go with a greyscale approach. It turned out that using real topographical maps in my game did not work out so well, so I generated a few black and white images to use for the terrain generation.
Here are a couple sample input images and their resulting terrains:
Here is the code:
/**
* Only works with images of equal height and width
*
*/
public class TextureToTerrainData {
/**
* For best results, input a grayscale texture. Darker will produce
* lower integers, Lighter will produce higher.
*/
public static int[][] getHeightsForImage(Texture texture) {
Color[][] colorsFromImage = TextureToTerrainData.getColorsForImage(texture);
int size = colorsFromImage.length;
int[][] integersFromImage = new int[size][size];
for (int x = 0; x 1 ? number : "0" + number);
lineBuilder.append("-");
}
System.out.println(lineBuilder.toString());
}
}
}Here is the output of the print method:
```
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-00-01-01-02-02-01-01-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-
00-00-00-00-00-00-00-00-00-00-01-02-03-04-04-04
Solution
This is a long waste of if chains
You're using them to determine a mathematical amount anyway. Just perform the arithmetic necessary by returning
if (color.r < 0.05) {
return 0;
} else if (color.r < 0.1) {
return 1;
} else if (color.r < 0.15) {
return 2;
...
} else if (color.r < 0.95) {
return 18;
}
return 19;You're using them to determine a mathematical amount anyway. Just perform the arithmetic necessary by returning
Math.floor(color.r * 20).Code Snippets
if (color.r < 0.05) {
return 0;
} else if (color.r < 0.1) {
return 1;
} else if (color.r < 0.15) {
return 2;
...
} else if (color.r < 0.95) {
return 18;
}
return 19;Context
StackExchange Code Review Q#104343, answer score: 17
Revisions (0)
No revisions yet.