# Rectangle Collision Not Correct As you can see on the top corner it says COL: False/True. This is if the player bounds and the tiles which are solids. The rectangles for the tiles and players are checked if they intercept each-other. Looks like its working right? Well nope. Look more closely.

The BOTTOM RIGHT corner needs to be inside the tiles for it to count.

Now let's get to the code I used now that hopefully you understand the problem.

Player Bounds (Rectangle)

playerBounds.Width = 32;
playerBounds.Height = 64;
playerBounds.X = (int)this.position.X;
playerBounds.Y = (int)this.position.Y;


Tile Bounds (Rectangle)

newTile.bounds = new Rectangle(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);


Now onto how it detects it:

for (int x = 0; x < Tilemap.MAP_WIDTH; x++)
{
for (int y = 0; y < Tilemap.MAP_HEIGHT; y++)
{

if (tm.tile[x, y].bounds.Intersects(playerBounds))
{
if (tm.tile[x, y].getSolid())
{
Colliding = true;
} else
{
Colliding = false;
}
}
}

}


Move

    public void Move(Vector2 pos)
{
for (int i = 0; i < speed; i++)
{

position += pos;
}

}


I have used breakpoints at the collision detection loop. And the rectangle fully covers the character and the tiles.

• You need colliding TRUE if any intersections occur, not just the last one checked. Put colliding=false outside your loop. Then do colliding = colliding || true; and remove colliding = false. You could also break out of the loop on the first collision detected, no need to see the rest! – david van brink Jul 5 '15 at 18:26
• Also this is in a update method. – John Jul 5 '15 at 19:09
• Wait you might be right the break; fixed the top right corner. – John Jul 5 '15 at 19:19
• @davidvanbrink Can you re-write my Colliding Function with said changes? – John Jul 5 '15 at 19:23
• FIXED IT! Ill post answer – John Jul 5 '15 at 19:34

        for (int x = 0; x < Tilemap.MAP_WIDTH; x++)
{
for (int y = 0; y < Tilemap.MAP_HEIGHT; y++)
{

if (tm.tile[x, y].bounds.Intersects(playerBounds))
{
if (tm.tile[x, y].getSolid())
{
foundCols++;
} else
{

}
}
}

}
if (foundCols < 1)
{
Colliding = false;
foundCols = 0;
}
else
{
Colliding = true;
foundCols = 0;
}