I have implemented a simple tile-based collision system in my top-down game, but I am having problems with moving across corners. Currently, my programs takes the player's center point and works out a tile on a 20px square grid (the alternating grey cbackground). It checks the up, down, left and right tiles for collisions, and if it finds one, it moves onto the next stage.
The next stage is to check where the player is within the tile. For this, I have a function called getCenterDistanceFromCurrentTile()
(creative name, I know). This returns a Vector2i (x and y) of the distance from the center point of the tile, to the center point of the player. I then use this in the following ways;
- If there is a wall tile to the left, and the player's x distance < 0 (the player is to the left of the current tile's centerpoint), the player is moved to the x of the tile next to the wall.
- If there is a wall to the right, and the player's x distance is > 0 (right of the current tile's centerpoint), the player is moved to the x of the tile next to the wall.
- This continues for the Y axis in the same way.
My problem is with diagonal. I do not check diagonal collisions currently, but it appears I will have to. I have already made an attempt to implement it, but the player jumps around when they hit a corner. I was using the above methods of player x < 0 and player y < 0 together, which didn't work out. I have made some images to better explain my problem.
This image demonstrates my collision system working fine, with a collision to the left (shown by the blue tile), and the pink tiles being checked, but no collision found. The green tile shows the player's current tile, and the red square shows the player's current location.
This image clearly shows the problem I have. None of the pink tiles have collided with anything, therefore the player's x and y do not need to be checked.
How should I check for corner collisions in my game?
My current collision checking code;
public boolean isWall(Vector2i loc)
{
return this.isWall(loc.x, loc.y);
}
public boolean isWall(int x, int y)
{
if (x < 0) return true;
if (y < 0) return true;
if (y > map[0].length-1) return true;
if (x > map.length-1) return true;
return map[x][y];
}
public void phys(Player plr) {
Vector2i playerTile = plr.getTile();
// Left
if (isWall(playerTile.x-1, playerTile.y))
{
Vector2i distanceFromTile = plr.getCenterDistanceFromCurrentTile(true);
if (distanceFromTile.x < 0)
{
plr.setX(playerTile.x*BLOCK_SIZE);
}
}
// Right
if (isWall(playerTile.x+1, playerTile.y))
{
Vector2i distanceFromTile = plr.getCenterDistanceFromCurrentTile(true);
if (distanceFromTile.x > 0)
{
plr.setX(playerTile.x*BLOCK_SIZE);
}
}
// Up
if (isWall(playerTile.x, playerTile.y-1))
{
Vector2i distanceFromTile = plr.getCenterDistanceFromCurrentTile(true);
if (distanceFromTile.y < 0)
{
plr.setY(playerTile.y*BLOCK_SIZE);
}
}
// Down
if (isWall(playerTile.x, playerTile.y+1))
{
Vector2i distanceFromTile = plr.getCenterDistanceFromCurrentTile(true);
if (distanceFromTile.y > 0)
{
plr.setY(playerTile.y*BLOCK_SIZE);
}
}
}