ok now, I'm developing a 2D platformer (e.g. super mario) like castlevania actually
ANYWAY
I'm having problems with performance for the following reason:
A map in my game has like 5 layers, for different purposes, and visual effects.
One layer is like everything the player cannot go through, I call it structures.
So in order to ensure you cannot walk through structures, I naturally check if a tile collides with the player.
I do this by using Rectangle.intersects(Rect...)
ok now for the problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public boolean collide(Rectangle in) { for (int y=0; y<height; y++) { for (int x=0; x<width; x++) { if (tiles[x][y] != null) { if (in.intersects(new Rectangle((x*TILE_SIZE),(y*TILE_SIZE),(int)tiles[x][y].getPic().getWidth(),(int)tiles[x][y].getPic().getHeight()))) { return true; } } } } return false; } |
Now this works fine, but the problem is: when the map gets bigger and bigger
1 2 3 4 5 6
| for (int y=0; y<height; y++) { for (int x=0; x<width; x++) { if (tiles[x][y] != null) { |
this is done more often and often, and it slows the game down extremely, because I do this several times a frame for every object (player or enemies), naturally
I mean... I cannot check weather or not a tile collides with my player without doing a loop like this with every tile ?!