Try something like this:
This will only slide against edges that are < 45 degrees to the direction of movement.
If you want to slide against more perpendicular edges, you'd need to probe a few pixels along each way.
And you might need to check the player can stand on the walkable area before moving there.
You'd have to call this several times if the character moves more than one pixel per update.
Hope that helps,
Steve
PS How do I format code on this board? It just trashes tab spaces.
Quote
int x = player.x;
int y = player.y;
int move = 1;
if (dir == UP || dir == DOWN)
{
if (dir == UP)
{
y = y-1;
}
else
{
y = y + 1;
}
if (GetWalkableAreaAt(x, y) == 0)
{
if (GetWalkableAreaAt(x-1, y) > 0)
{
x = x - 1;
}
else
if (GetWalkableAreaAt(x+1, y) > 0)
{
x = x + 1;
}
else
{
move = 0;
}
}
}
else
if (dir == LEFT || dir == RIGHT)
{
if (dir == LEFT)
{
x = x - 1;
}
else
{
x = x + 1;
}
if (GetWalkableAreaAt(x, y) == 0)
{
if (GetWalkableAreaAt(x, y-1) > 0)
{
y = y - 1;
}
else
if (GetWalkableAreaAt(x, y+1) > 0)
{
y = y + 1;
}
else
{
move = 0;
}
}
}
if (move > 0)
{
// move to x, y
}
This will only slide against edges that are < 45 degrees to the direction of movement.
If you want to slide against more perpendicular edges, you'd need to probe a few pixels along each way.
And you might need to check the player can stand on the walkable area before moving there.
You'd have to call this several times if the character moves more than one pixel per update.
Hope that helps,
Steve
PS How do I format code on this board? It just trashes tab spaces.