@JimmyMaslaki wrote:
I've been having this problem for quite a while now. Collision seems to be smooth sailing except for this one thing. The player can go into any corner and pass through the wall.
I've tried tinkering with some code and I can't get anything to fix it.
Here's the source code:
Player.cs
hitbox = new Rectangle((int)Position.X, (int)Position.Y + (Height / 2), Width, Height / 2); //this rectangle is the bottom half of the player if (able) { if (k.IsKeyDown(up) && cantmoveup == false) { Position.Y += -speed; } if (k.IsKeyDown(rightt) && cantmoveright == false) { left = false; right = true; Position.X += speed; } if (k.IsKeyDown(leftt) && cantmoveleft == false) { right = false; left = true; Position.X += -speed; } if (k.IsKeyDown(down) && cantmovedown == false) { Position.Y += speed; } if (k.IsKeyDown(Keys.X)) { speed = 8f; } else { speed = 4f; } }
Game1.cs
public void Update(GameTime gameTime){ foreach (Wall wall in Walls) { if (player.hitbox.isOnLeft(wall.hitbox)) { player.cantmoveright = true; break; }else { player.cantmoveright = false; } if (player.hitbox.TouchRightOf(wall.hitbox)) { player.cantmoveleft = true; break; } else { player.cantmoveleft = false; } if (player.hitbox.isOnBottom(wall.hitbox)) { player.cantmoveup = true; break; } else { player.cantmoveup = false; } if (player.hitbox.isOnTop(wall.hitbox)) { player.cantmovedown = true; break; } else { player.cantmovedown = false; } } } public static class HamburgerHelper
{
const int penetrationMargin = 5;
public static bool isOnTop(this Rectangle r1, Rectangle r2)
{
return (r1.Bottom >= r2.Top - penetrationMargin &&
r1.Bottom <= r2.Top + (r2.Height / 2) &&
r1.Right >= r2.Left + 5 &&
r1.Left <= r2.Right - 5);
}
public static bool isOnLeft(this Rectangle r1, Rectangle r2)
{
return (r1.Right >= r2.Left - penetrationMargin &&
r1.Right <= r2.Left + (r2.Width / 2) &&
r1.Top <= r2.Bottom - 5 &&
r1.Bottom >= r2.Top + 5);
}
public static bool TouchRightOf(this Rectangle r1, Rectangle r2)
{
return (r1.Left <= r2.Right + penetrationMargin &&
r1.Left >= r2.Right - (r2.Width / 2) &&
r1.Top <= r2.Bottom - 5 &&
r1.Bottom >= r2.Top + 5);
}
public static bool isOnBottom(this Rectangle r1, Rectangle r2)
{
return (r1.Top <= r2.Bottom + penetrationMargin && r1.Top >= r2.Bottom - (r2.Height / 2) &&
r1.Right >= r2.Left + 5 &&
r1.Left <= r2.Right - 5);
}
public static bool isTouching(this Rectangle r1, Rectangle r2)
{
return (r1.TouchRightOf(r2) || r1.isOnLeft(r2) || r1.isOnBottom(r2) || r1.isOnTop(r2));
}}
If you could examine it to see what's causing this, it would be greatly appreciated. Thanks for your time.
Posts: 3
Participants: 3