I was making a game and I came up with the idea of using hex color codes to make collisions with objects on the map using a png image as a mask. For this to work, the code would scan every pixel of this mask and, if a pixel had a specific color, an invisible block would spawn at its location. However, since the image I'm using as a mask is pretty large (1582 x 1146), the code uses way too much cpu power (I think it's the cpu, not so sure) to scan every single pixel and the game runs literally at 1 FPS.
public class World {
private Tile[] tiles;
private static int WIDTH = 1582, HEIGHT = 1146;
public World(String path) {
try {
BufferedImage map = ImageIO.read(getClass().getResource(path));
int[] pixels = new int[map.getWidth() * map.getHeight()];
tiles = new Tile[map.getWidth() * map.getHeight()];
map.getRGB(0, 0, map.getWidth(), map.getHeight(), pixels, 0, map.getWidth());
for (int xx = 0; xx < map.getWidth(); xx++) {
for (int yy = 0; yy < map.getHeight(); yy++) {
int pixelAtual = pixels[xx + (yy * map.getWidth())];
if (pixelAtual == 0xfffcff00) //Pixel's hex verification
{
tiles[xx + (yy * WIDTH)] = new TileWall(xx, yy, Tile.TILE_WALL); //Invisible tile wall
}
/*else if (pixelAtual == ) Adding another tile
tiles[xx + (yy * WIDTH)] = new TileWall(xx, yy, Tile.TILE_WALL);
} */
else {
tiles[xx + (yy * WIDTH)] = new TileFloor(xx, yy, Tile.TILE_FLOOR); //Invisible tile floor, just to complete the array
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void render(Graphics g) {
for (int xx = 0; xx < WIDTH; xx++) {
for (int yy = 0; yy < HEIGHT; yy++) {
Tile tile = tiles[xx + (yy * WIDTH)];
tile.render(g);
}
}
}
}
I thought this method of hex scanning would be easy to write and practical to use. Is there any other way to add collisions to the game?