private void ResolveCollisions()
{
isColliding = false;
const float buffer = 0.1f; // Buffer to handle small overlaps uniformly
foreach (Tile tile in currentMapBG.Objects)
{
if (WallTiles.Contains(tile.TileID) && CollidesWith(tile))
{
isColliding = true;
// Calculate overlaps
float overlapX = Math.Min(BoundingBox.Right, tile.BoundingBox.Right) - Math.Max(BoundingBox.Left, tile.BoundingBox.Left);
float overlapY = Math.Min(BoundingBox.Bottom, tile.BoundingBox.Bottom) - Math.Max(BoundingBox.Top, tile.BoundingBox.Top);
Console.WriteLine($"TileID: {tile.TileID}, PlayerPos: {Position}, TilePos: {tile.Position}");
Console.WriteLine($"OverlapX: {overlapX}, OverlapY: {overlapY}");
// Adjust position based on the smallest overlap with a uniform buffer
if (Math.Abs(overlapX) < Math.Abs(overlapY))
{
// Move out of collision along X axis
if (Position.X < tile.Position.X)
{
Position = new Vector2(tile.Position.X - BoundingBox.Width - buffer, Position.Y);
Console.WriteLine($"Adjusting X Position (Left): NewPos: {Position}");
}
else
{
Position = new Vector2(tile.Position.X + tile.BoundingBox.Width + buffer, Position.Y);
Console.WriteLine($"Adjusting X Position (Right): NewPos: {Position}");
}
}
else
{
// Move out of collision along Y axis
if (Position.Y < tile.Position.Y)
{
Position = new Vector2(Position.X, tile.Position.Y - BoundingBox.Height - buffer);
Console.WriteLine($"Adjusting Y Position (Up): NewPos: {Position}");
}
else
{
Position = new Vector2(Position.X, tile.Position.Y + tile.BoundingBox.Height + buffer);
Console.WriteLine($"Adjusting Y Position (Down): NewPos: {Position}");
}
}
}
}
}