For example Say you have a tile in an L-shape like this:
[_
And the player is about here:
[_
P
When you push up on the control pad, Link will slide to the left to here:
[_
P
And then continue upwards to here:
P[_
I was looking at my tile collision code, and thought how the player gets 'caught' on the corners of tiles. I thought that this would be a good solution for that problem. I found that the cutoff point that determines if link will collide with the wall or slide is about 4-6 pixels. Using that information, how can I add this feature to my code?
I am using an altered version of a collision code from parallel realities. s is the player, and tm is the tilemap. tm->tiles is a two-dimensional numeric index for each tile, 0 is passable and everything else is solid.
void collide(sprite* s, tilemap* tm)
{
int TILE_SIZE = 16;
int i, x1, x2, y1, y2;
/* Test the horizontal movement first */
i = s->h > TILE_SIZE ? TILE_SIZE : s->h;
for (;;)
{
x1 = (s->x + s->xsp) / TILE_SIZE;
x2 = (s->x + s->xsp + s->w/* - 1*/) / TILE_SIZE;
y1 = (s->y) / TILE_SIZE;
y2 = (s->y + i - 1) / TILE_SIZE;
if (x1 >= 0 && x2 < tm->w && y1 >= 0 && y2 < tm->h)
{
if (s->xsp > 0)
{
/* Trying to move right */
if ((tm->tiles[y1][x2] != 0) || (tm->tiles[y2][x2]!=0))
{
/* Place the player as close to the solid tile as possible */
s->x = x2 * TILE_SIZE;
s->x -= s->w + 1;
s->x += 1;
s->xsp = 0;
}
}
else if (s->xsp < 0)
{
/* Trying to move left */
if ((tm->tiles[y1][x1] != 0) || (tm->tiles[y2][x1]!=0))
{
/* Place the player as close to the solid tile as possible */
s->x = (x1 + 1) * TILE_SIZE;
s->xsp = 0;
}
}
}
if (i == s->h)
{
break;
}
i += TILE_SIZE;
if (i > s->h)
{
i = s->h;
}
}
/* Now test the vertical movement */
i = s->w > TILE_SIZE ? TILE_SIZE : s->w;
for (;;)
{
x1 = (s->x) / TILE_SIZE;
x2 = (s->x + i - 1) / TILE_SIZE;
y1 = (s->y + s->ysp) / TILE_SIZE;
y2 = (s->y + s->ysp + s->h - 1) / TILE_SIZE;
if (x1 >= 0 && x2 < tm->w && y1 >= 0 && y2 < tm->h)
{
if (s->ysp > 0)
{
/* Trying to move down */
if ((tm->tiles[y2][x1] != 0) || (tm->tiles[y2][x2]!=0))
{
/* Place the player as close to the solid tile as possible */
s->y = y2 * TILE_SIZE;
s->y -= s->h;
s->ysp = 0;
//s->onGround = 1;
}
}
else if (s->ysp < 0)
{
/* Trying to move up */
if ((tm->tiles[y1][x1] != 0) || (tm->tiles[y1][x2]!=0))
{
/* Place the player as close to the solid tile as possible */
s->y = (y1 + 1) * TILE_SIZE;
s->ysp = 0;
}
}
}
if (i == s->w)
{
break;
}
i += TILE_SIZE;
if (i > s->w)
{
i = s->w;
}
}
}
How can I implement corner-cutting into this code? What is the logic/geometry behind it?