# Collision detection between player and tiles

I am trying to implement basic (for now) collision detection into my platformer. I have tiles that are each 16 x 16 in size. The character is 32 x 32 pixels in size and has its own bounding box. Now, in my Tile class, I have a bool, isSolid. Each of these tiles in my array also have a rect for their respective bounding boxes.

I am checking to see if there's an intersection between the player and tiles by doing:

if (player.GetBoundingBox().Intersects(map.tiles[(int)player.position.Y / 16,
(int)player.position.X / 16].bounds) && map.tiles[(int)player.position.Y / 16,
(int)player.position.X / 16].isSolid)
{
...
}


Now, my problem is that this is extremely inaccurate as I'm rounding off the position. I'm tired as heck right now and for the life of me I can't figure out how to properly do this. What is the best way to approach this issue?

• Have you taken a look at the XNA platformer starter kit? You can find a Windows Phone port here: blogs.msdn.com/b/davrous/archive/2010/03/29/… – ashes999 Dec 29 '12 at 12:47
• I've looked at the kit, but never at how it does collision detection. I guess I will take a look at it when I get home. I'd prefer to implement it without looking at it, but it's frustrating me a bit! I'm thinking to check multiple tiles in the direction that the player is going. Hmm... – Steven B. Dec 29 '12 at 12:53
• you would do well to learn from others who already conquered similar problems in the past -- sometimes in more complexity (like the platformer kit, which even accomodates one-way platforms). No need to reinvent when you can reuse! – ashes999 Dec 29 '12 at 13:36

Your problem begins with your massively long line of code. You lose nothing by breaking out each individual segment and putting it into its own variable... the compiler will optimise these out in the end, anyway. And you will understand the code you are writing, much more clearly. There are plenty of problems challenging enough that you need not give yourself a handicap!

Second, you need to decide how you are going to approach this. Will you do bounding box checks, or will you do individual tile checks (possibly alone, or possibly as well)? At the moment I get the feeling you're confounding the two. Tile-by-tile checks are more expensive if used alone. So better to do bounding rectangle checks first and foremost. Your platforms should have their own bounding rectangles.

So let's look at your code, modified.

foreach (BoundingBox platform : platforms) //platforms is a List you've built beforehand.
{
if (player.GetBoundingBox().Intersects(platform)
{
if (tile.isSolid)
{
pushPlayerBack();
}
}
}


The platforms list is a set of BoundingBoxes which you construct before building your tilemap. Once you have the platform BoundingBoxes, you can derive each tile's isSolid value by running through the list of platform BoundingBoxes and updating tile values accordingly. This way, the tilemap matches the list of platform BoundingBoxes, and you can do a nice cheap and easy Intersects check to see if your player overlaps any solid tiles.

Now that you know whether the BoundingBoxes overlap, you can either accept that result as the final answer or do an additional finer scale, tile-by-tile check within the overlapping area.