1
votes

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.

Problem image 1

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.

Problem image 2

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);
        }
    }
}
1

1 Answers

1
votes

Your phys method could return boolean depending on whether the player is colliding with any of the walls. You could then use the result of this function to decide whether to move back to the previous location in space you were before you moved, or if you did not collide, to continue on.

This would require you to check all of the adjacent walls around the player. This would introduce a much cleaner way of iterating through the collision checks in the phys method.

Such an improvement would look like:

public boolean phys(Player plr)
{
    java.awt.Rectangle playerBounds = new java.awt.Rectangle(plr.x, plr.y, BLOCK_SIZE, BLOCK_SIZE);

    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            // Skip the tile the player is on assuming that he is not on a wall already
            if (x == 1 && y == 1) continue;

            java.awt.Rectangle bounds = new java.awt.Rectangle(/*tile at (x, y) xpos*/, /*tile at (x, y) ypos*/, BLOCK_SIZE, BLOCK_SIZE);

            if (bounds.intersects(playerBounds))
            {
                return true;
            }
        }
    }

    // Did not collide
    return false;
}