This might be really simple but it is giving me a lot grief and I wondered if you guys would be able to shed some light on it. Basically I have a tile map which draws tiles at 64 * 64 and that works very well and they are drawn in a 2D array like this in the game1 class.
TileMap tileMap = new TileMap(new int[,]
{
{ 2,2,2,2,2,2,2,2,2,2 },
{ 2,2,2,2,2,2,2,2,2,2 },
{ 0,0,0,0,0,0,4,4,4,4 },
{ 0,0,0,0,0,0,1,1,4,4 },
{ 1,1,1,1,1,1,1,1,1,1,},
{ 1,1,1,1,1,1,1,1,1,1,}
});
Now comes the problem. Basically I want to determine which side the player has hit the tile from. However, with the algorithm below the collision only works correctly for the bottom and left side. if the player hits the tile from the top it will state it was hit from the bottom. if the player hits from the right it will specify the left side was hit. if the player hits from the left or bottom it will show that collision has happened but state it happened on the right or top of it. it will output that the top of the has been hit but only when the player is completely in the tile like this
Pretty much the same thing happens when it displays the right side is hit (player moves more to the left)
TileMap
int left = (int)Math.Floor((float)player.playerBounds.Left / TILE_WIDTH);
int top = (int)Math.Floor((float)player.playerBounds.Top / TILE_HEIGHT);
int right = (int)Math.Ceiling((float)player.playerBounds.Right / TILE_WIDTH) - 1;
int bottom = (int)Math.Ceiling((float)player.playerBounds.Bottom / TILE_HEIGHT) - 1;
Rectangle tileBounds = new Rectangle((int)tilePosX, (int)tilePosY, TILE_WIDTH, TILE_HEIGHT);
Rectangle playerBounds = player.playerBounds;
float WidthOfRects = 0.5f * playerBounds.Width + tileBounds.Width;
float heightOfRects = 0.5f * playerBounds.Height + tileBounds.Height;
int centerX = (playerBounds.Left + playerBounds.Width / 2) - (tileBounds.Left + tileBounds.Width / 2);
int centerY = (playerBounds.Top + playerBounds.Height / 2) - (tileBounds.Top + tileBounds.Height / 2);
for (int y = top; y <= bottom; ++y)
{
for (int x = left; x <= right; ++x)
{
if (mapCell[y, x].TileID == 1)
{
//minkowski sum
if (Math.Abs(centerX) <= WidthOfRects && Math.Abs(centerY) <= heightOfRects)
{
double wy = WidthOfRects * centerY;
double hx = heightOfRects * centerX;
if (wy > hx)
{
if (wy > -hx)
{
Console.WriteLine("bottom");
//newPos.Y = tileCollision.Bottom;
}
else
{
Console.WriteLine("right");
//newPos.X = tileCollision.Right;
}
}
if (wy > -hx)
{
Console.WriteLine("left ");
//newPos.X = tileCollision.Left - playerBounds.Width;
}
else
{
Console.WriteLine("top");
//newPos.Y = tileCollision.Top - playerBounds.Height;
}
}
// player.Position = newPos;
}
}
}
Map Cell
public class MapCell
{
public int TileID { get; set; }
public MapCell(int tileID)
{
TileID = tileID;
}
Any help would be greatly appreciated.
Thank you.