Reputation: 3
I've created a small game with tiles and a player, and I'm trying to program in collision detection. So, I've managed to make collision when the character goes left ('A') to work, but it doesn't work when it is going right ('D'). This is my code:
public bool ColX(Texture2D sprite, Vector2 pos, float speed)
{
float PosLeftX = pos.X;
float PosUpY = pos.Y;
float PosRightX = pos.X + sprite.Width;
float PosDownY = pos.Y + sprite.Height;
float DestX = pos.X + speed;
Vector2 tileTopLeft = GetTile(PosLeftX, PosUpY); //using PosLeftX and PosLeftY to set top left tile to a variable
Vector2 tileBotLeft = GetTile(PosLeftX, PosDownY);
Vector2 tileTopRight = GetTile(PosRightX, PosUpY);
Vector2 tileBotRight = GetTile(PosRightX, PosDownY);
Vector2 tileDestUp = GetTile(DestX, PosUpY);
Vector2 tileDestDown = GetTile(DestX, PosDownY);
if(tileTopRight.X < tileDestUp.X)
{
float startX = tileTopRight.X;
float finishX = tileDestUp.X;
float startY = tileTopRight.Y;
float finishY = tileBotRight.Y;
for (int l = (int)startX; l <= finishX; l++)
{
for (int i = (int)startY; i <= finishY; i++)
{
if (tileList[l, i] != null)
{
return true;
}
}
}
return false;
}
else if (tileTopLeft.X > tileDestDown.X)
{
float startX = tileDestUp.X;
float finishX = tileTopLeft.X;
float startY = tileDestUp.Y;
float finishY = tileDestDown.Y;
for (int l = (int)startX; l <= finishX; l++)
{
for (int i = (int)startY; i <= finishY; i++)
{
if (tileList[l, i] != null)
{
return true;
}
}
}
return false;
}
else
{
return false;
}
}
On pressing 'A', speed will be a negative parameter, making the destination tile to the left of the player, and it will check if each tile in the a rectangle from the destination tile to the player's bottom left tile area. If there's a tile, the move is cancelled, and it works. However, the same thing isn't working for the 'D' key, though I assumed that the same code for 'A' would work for 'D', with minor modifications.
Upvotes: 0
Views: 76
Reputation: 657
I know I'm a whole month late, but this guy heleped me a lot in game programming. Click here to open a link where he explains the 'right' way to do the collision in 2D platformers. He explains collision from another perspective. I won't post any code only because I think you'll find it more entertaining to do it yourself.
The jist of it is that if you check the collision when your object hits the wall, then it's too late to do anything. Object will most likely be drawin inside the wall. You need to be checking object's next position (by adding it's X and Y velocity to its position), and if object is then hitting the wall, move the object right next to it. This way, you'll never have a object-in-wall situation. Also, you first calculate X/Y movements, speeds, accelerations AND COLLISION, and then you do the same for the other axis.
This is a kind-of-an answer. I didn't give you the code, but I've told you where to find your answer. Again, I didn't provide the code, because I think you'll have mutch fun coding everything yourself. I beleave that if that's not the case, you'd be on Unity forums by now, not XNA.
Upvotes: 0