I have a problem with my collision. My character is 2 tiles tall (128x64) and moves in 8 pixel increments
I have a collision method set up, but there’s a really big problem
Whenever you approach a wall from the left, the collision does nothing until you are inside the wall, at which point it softlocks the game. I desperately need help. Can anyone offer some insight?
Code is below.
Movement
protected override void Update(GameTime gameTime)
{
if ((currentkeyboardstate.IsKeyUp(Keys.D)) && (currentkeyboardstate.IsKeyUp(Keys.A)))
{
Fool.PlayerPosition.X = 0;
Fool.PlayerVelocity.X = 0;
}
if (currentkeyboardstate.IsKeyDown(Keys.D))
Fool.PlayerVelocity.X = 8;
if (currentkeyboardstate.IsKeyDown(Keys.A))
Fool.PlayerVelocity.X = -8;
if (currentkeyboardstate.IsKeyUp(Keys.W))
{
Fool.PlayerVelocity.Y = -8;
}
if (currentkeyboardstate.IsKeyDown(Keys.W))
Fool.PlayerVelocity.Y = +8;
Fool.PlayerVelocity += Fool.PlayerAcceleration;
Fool.PlayerPosition += Fool.PlayerVelocity;
int tempx = map.ObjectGroups["PlayerLayer"].Objects["Player"].X + (int)(Fool.PlayerPosition.X);
int tempy = map.ObjectGroups["PlayerLayer"].Objects["Player"].Y - (int)(Fool.PlayerPosition.Y);
//now we have moved checkbounds
if (CheckBoundsX() == false)
{
map.ObjectGroups["PlayerLayer"].Objects["Player"].X = tempx;
CheckX = false;
}
else
{
Fool.PlayerPosition.X = 0;
Fool.PlayerVelocity.X = 0;
Fool.PlayerAcceleration.X = 0;
CheckX = true;
}
if (CheckBoundsY() == false)
{
map.ObjectGroups["PlayerLayer"].Objects["Player"].Y = tempy;
CheckY = false;
}
else
{
Fool.PlayerPosition.Y = 0;
Fool.PlayerVelocity.Y = 0;
Fool.PlayerAcceleration.Y = 0;
CheckY = true;
}
CheckX (horizontal check)
public bool CheckBoundsX()
{
bool checkX = false;
Rectangle playrec = new Rectangle(
map.ObjectGroups["PlayerLayer"].Objects["Player"].X + (int)(Fool.PlayerPosition.X),
map.ObjectGroups["PlayerLayer"].Objects["Player"].Y - (int)(Fool.PlayerPosition.Y),
map.ObjectGroups["PlayerLayer"].Objects["Player"].Width,
map.ObjectGroups["PlayerLayer"].Objects["Player"].Height
);
Vector2 centertile = new Vector2(
(map.ObjectGroups["PlayerLayer"].Objects["Player"].X + (map.ObjectGroups["PlayerLayer"].Objects["Player"].Width)) / 64,
(map.ObjectGroups["PlayerLayer"].Objects["Player"].Y + (map.ObjectGroups["PlayerLayer"].Objects["Player"].Height / 2)) / 64
);
if (collision.GetTile((int)centertile.X + 1, (int)centertile.Y) != 0)
{
Rectangle right = new Rectangle(
(int)(centertile.X + 1) * 64,
(int)centertile.Y * 64,
64,
64
);
if (playrec.Intersects(right))
checkX = true;
}
if (collision.GetTile((int)centertile.X - 1, (int)centertile.Y) != 0)
{
Rectangle left = new Rectangle(
(int)(centertile.X - 1) * 64,
(int)centertile.Y * 64,
64,
64
);
if (playrec.Intersects(left))
checkX = true;
}
return CheckX;
}
CheckY (vertical)
public bool CheckBoundsY()
{
bool checkY = false;
Rectangle playrec = new Rectangle(
map.ObjectGroups["PlayerLayer"].Objects["Player"].X + (int)(Fool.PlayerPosition.X),
map.ObjectGroups["PlayerLayer"].Objects["Player"].Y - (int)(Fool.PlayerPosition.Y),
map.ObjectGroups["PlayerLayer"].Objects["Player"].Width,
map.ObjectGroups["PlayerLayer"].Objects["Player"].Height
);
Vector2 centertile = new Vector2(
(map.ObjectGroups["PlayerLayer"].Objects["Player"].X + (map.ObjectGroups["PlayerLayer"].Objects["Player"].Width / 2)) / 64,
(map.ObjectGroups["PlayerLayer"].Objects["Player"].Y + (map.ObjectGroups["PlayerLayer"].Objects["Player"].Height / 2)) / 64
);
if (collision.GetTile((int)centertile.X, (int)centertile.Y - 1) != 0)
{
Rectangle top = new Rectangle(
(int)centertile.X * 64,
(int)(centertile.Y - 1) * 64,
64,
64
);
if (playrec.Intersects(top))
checkY = true;
}
if (collision.GetTile((int)centertile.X, (int)centertile.Y + 1) != 0)
{
Rectangle bottom = new Rectangle(
(int)centertile.X * 64,
(int)(centertile.Y + 1) * 64,
64,
64
);
if (playrec.Intersects(bottom))
checkY = true;
}
return checkY;
}
Any help is welcome. Thanks in advance.