Generally speaking, most people will probably tell you that the way to check for tile collisions is not to in fact do intersect tests, but instead take the position of your player and use that to calculate an index into your map data, then just see what value is in the data to determine if a collision has occured. That's going to be the most efficient way of doing it.
That being said, my current project has a scrolling tile-based part to it, and I've actually done real collision detection using intersect. In my case, I have some floor tiles and some wall tiles, so I need to determine collisions with the walls to stop my player from moving. I just do the exact same loop I do when drawing the tiles to the screen, and for every wall tile, I do an intersect test with my player surface.
My map is 12x15 (20x20 tiles), and because of the way it's drawn, I'm not normally doing more than about 100 intersect tests per frame (which is still a lot mind you, but because there's not really much else going on it's not a problem). GD's Intersect function seems to be implemented rather efficiently, so performance, in my case at least, doesn't take a significant hit.
Even still, I'd suggest the first approach. As long as you are calculating the index into your map data in an efficient way, it should fly.
In case you want to go with my approach, here's some psuedo-code to work with:
- Code:
// Map data is a simple chunk of memory, each byte
// is one tile. The value is an index into an array
// of surfaces storing the images of each tile
// (i.e., tile code 0 is the first element in an
// array of surfaces, and maybe that represents a
// floor tile, 1 is a wall, 2 is a door, etc.)
map_data = pointer to my map data;
// Each frame I completely redraw the visible portion
// of the map...
for y = 0 to 14 { // 15 tiles down, 20x20 each
for x = 0 to 11 { // 12 tiles across, 20x20 each
tile_index = y * 12 + x;
tile_type = *(map_data + tile_index);
// tile_type now has the value representing the
// next tile to draw
blit(x * 20, y * 20, map_images[tile_type]);
}
}
// Each tile is a color-keyed (i.e., partly
// transparent) surface. This is true even for tiles
// that have no real transparent portion.
// Then, to check for collision:
for y = 1 to 15 {
for x = 1 to 12 {
tile_index = y * 12 + x;
tile_type = *(map_data + tile_index);
if (tile_type == wall) {
// Do intersect test between this tile and
// the player, or whatever other surface you
// want to compare
}
}
}
I hope that makes some sense. It's really pretty straight-forward doing it that way.