# Collision Detection Problems

So I'm making a 2D tile based game but I can't quite get the collisions working properly. I've taken the code from the Platformer Sample and implemented it into my game as seen below.

One problem I'm having is when I'm on the ground for some strange reason I can't move to the left. Now I'm pretty sure this problem is from the HandleCollisions() method because when I stop running it I can smoothly move around with no problems. Another problem I'm having is when I'm close to a tile the character jitters very strangely.

I will try to post a video if necessary. Here is the HandleCollisions() method:

void HandleCollisions() {
Rectangle bounds = BoundingRectangle;

int topTile = (int)Math.Floor((float)bounds.Top / World.PixelTileSize);
int bottomTile = (int)Math.Ceiling((float)bounds.Bottom / World.PixelTileSize) - 1;
int leftTile = (int)Math.Floor((float)bounds.Left / World.PixelTileSize);
int rightTile = (int)Math.Ceiling((float)bounds.Right / World.PixelTileSize) - 1;

isOnGround = false;

for(int x = leftTile; x <= rightTile; x++) {
for(int y = topTile; y <= bottomTile; y++) {
if(world.Map[y, x].Collidable == true) {
Rectangle tileBounds =
new Rectangle(x * World.PixelTileSize, y * World.PixelTileSize,
World.PixelTileSize, World.PixelTileSize);
Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds,
tileBounds);

if(depth != Vector2.Zero) {
if(Math.Abs(depth.Y) < Math.Abs(depth.X)) {
isOnGround = true;
position = new Vector2(position.X, position.Y + depth.Y);
}
else {
position = new Vector2(position.X + depth.X, position.Y);
}

bounds = BoundingRectangle;
}
}
}
}
...
}


I saw something like this before in the platformer that I'm developing.

One possibility is that there's a rounding or precision error. If you're using the Rectangle class (in Microsoft.Xna.Framework), you're limited to the precision of integers. Try something equivalent that uses floats.

Also, if you're updating both and x and y positions, then checking for collisions, you might have problems. If that's the case, try moving on one axis at a time, check for collisions, then move on the other axis.

I suggested the same thing in another thread: How to determine collision direction between two rectangles?

Edit: Now that I think about it again, I'm pretty sure doing one axis at a time should solve this problem. But it's still a good idea to use higher precision for your geometries. (You should only be rounding geometry coordinates to integers when it's time to draw graphics.)

• +1 it was rounding in my case -- trying to draw a sprite with a Y of 732.2316 caused problems. Rounding solved it. – ashes999 Dec 6 '12 at 16:21

Sounds like the tile to the left might be slightly greater than the one to the right, try making sure all tiles are flush or make it so when moving it adjusts to small changes in height.

• No that couldn't be it all the tile are completely flush with each other. – MrPlosion1243 Jun 8 '12 at 17:30

I had some problems myself with the platform starter kit. Check out the answer, maybe it would help. Platformer Starter Kit - Collision Issues