Hello JGO forum, I have done my best in searching this topic through Google but have found nothing but broken links to slickcokeandcode forums. I have been trying to create a top down Zelda-esque game for fun. I cannot seem to get collision among the player and walls to act as they should.
Right now the player moves across the walls but gets stuck sliding across them and I know its because of how I have set up the walls in the arrays. But I honestly don’t know how else to write it, I have posted my code for the player update method in hopes someone can help me out to smooth the collision.
Thanks!
public void update(Room room)
{
pbound.setBounds(x, y, playerimg.getWidth(),playerimg.getHeight());
for(Wall wall : room.wallList)
{
if(pbound.intersects(wall.bound))
{
if(pbound.getMaxX() == wall.bound.getX())
{
if(key.isKeyDown(Input.KEY_LEFT))
dx = -1;
else
dx = 0;
if(key.isKeyDown(Input.KEY_DOWN))
dy = 1;
if(key.isKeyDown(Input.KEY_UP))
dy = -1;
break;
}
else if(pbound.getX() == wall.bound.getMaxX())
{
if(key.isKeyDown(Input.KEY_RIGHT))
dx = 1;
else
dx = 0;
if(key.isKeyDown(Input.KEY_DOWN))
dy = 1;
if(key.isKeyDown(Input.KEY_UP))
dy = -1;
break;
}
else if(pbound.getY() == wall.bound.getMaxY())
{
if(key.isKeyDown(Input.KEY_DOWN))
dy = 1;
else
dy = 0;
if(key.isKeyDown(Input.KEY_LEFT))
dx = -1;
if(key.isKeyDown(Input.KEY_RIGHT))
dx = 1;
break;
}
else if(pbound.getMaxY() == wall.bound.getY())
{
if(key.isKeyDown(Input.KEY_UP))
dy = -1;
else
dy = 0;
if(key.isKeyDown(Input.KEY_LEFT))
dx = -1;
if(key.isKeyDown(Input.KEY_RIGHT))
dx = 1;
break;
}
break;
}
if(!pbound.intersects(wall.bound))
{
if(key.isKeyDown(Input.KEY_RIGHT) && key.isKeyDown(Input.KEY_LEFT))
dx = 0;
else if(key.isKeyDown(Input.KEY_LEFT))
dx = -1;
else if(key.isKeyDown(Input.KEY_RIGHT))
dx = 1;
else
dx = 0;
if(key.isKeyDown(Input.KEY_UP) && key.isKeyDown(Input.KEY_DOWN))
dy = 0;
else if(key.isKeyDown(Input.KEY_DOWN))
dy = 1;
else if(key.isKeyDown(Input.KEY_UP))
dy = -1;
else
dy = 0;
}
}
x += dx;
y += dy;
}