Tilemap Collision

BlitzMax Forums/BlitzMax Beginners Area/Tilemap Collision

po(Posted 2009) [#1]
I'm in a bit of a pickle. I'm making a platformer Commander Keen/Mario style, using an array of objects for the tiles. My problem is getting the player to accurately collide with the tiles so that he doesn't (appear to) overlap any of the tiles.

Up until now I've been using this system:
There's a field in TPlayer called cTile that holds the TTile object that the player's center is currently overlapping. In other words, it's the tile that the player is currently occupying. I then check if the player is overlapping the tile below him, to the right of him, to the left of him, and above him. For example:
If playerY+playerHeight>tileBelowY Then playerY=tileBelowY-playerHeight
This works fine for all sides, except when dealing with the edge of say, a drop-off. Where in games like Mario, the player can walk to his very heels on the edge of a drop-off, using my method the player simply falls into the tile half-way through the player, looking and playing quite poorly.

So I figure the best method would be to use the CollideRect function instead. So I put CollideRect(tileX,tileY,tileWidth,tileHeight,0,1) in every tile's Draw() method, and checked for collision with the player using CollideRect(playerX,playerY,playerWidth,playerHeight,1,0) and found that I can indeed detect the collision, but I have no way of stopping the player from going through the tiles because I don't know which tile I'm colliding with. Is there a way to get which tile collided with the player? I use the function GetTile(x,y) to get tiles based off of coordinates, but coords don't help me much if I'm trying to avoid the player-on-edge-of-drop-off issue. Any ideas?


therevills(Posted 2009) [#2]
Hi Po,

Heres a quick example of Tilemap collision:



Cheers!


po(Posted 2009) [#3]
Ah, thank you.