I am having trouble with my 2-D wall-sliding. Namely, when the player avatar collides with the wall, it "snaps" to the corner (see screenshot, below) and sticks there. The red, transparent rectangle is my "wall":
My code is as follows:
"FootRectangle" is your standard xna rectangle. Location is a vector2. The player is moving to a target, which could be anywhere on the screen, which is also a vector2.
if (wall.CollisionRectangle.Intersects(player.FootRectangle))
{
// collision above block
if (player.FootRectangle.Top <= wall.CollisionRectangle.Top)
{
player.Location = new Vector2(player.Location.X, wall.CollisionRectangle.Top - player.FootRectangle.Height / 2);
}
// collision below block
else if (player.FootRectangle.Bottom >= wall.CollisionRectangle.Bottom)
{
//player.FootRectangle.Y = block.CollisionRectangle.Bottom;
player.Location = new Vector2(player.Location.X, wall.CollisionRectangle.Bottom + player.FootRectangle.Height / 2);
}
// collision left of block
if (player.FootRectangle.Left <= wall.CollisionRectangle.Left)
{
player.Location = new Vector2(wall.CollisionRectangle.Left - player.FootRectangle.Width / 2, player.Location.Y);
}
// collision right of block
else if (player.FootRectangle.Right >= wall.CollisionRectangle.Right)
{
player.Location = new Vector2(wall.CollisionRectangle.Right + player.FootRectangle.Width / 2, player.Location.Y);
}
How can I resolve these collisions so that the avatar snaps to a corner and smoothly makes his way to the target destnation?
I attempted to implement a similar solution to that at this post; however, it seemed to stick when the collision occurred.