简体   繁体   English

基于图块的碰撞; 拐角碰撞问题

[英]Tile-based collision; Corner collision problems

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). 目前,我的程序以玩家的中心点为基准,并在20像素的正方形网格(交替的灰色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). 为此,我有一个名为getCenterDistanceFromCurrentTile()的函数(创意名称,我知道)。 This returns a Vector2i (x and y) of the distance from the center point of the tile, to the center point of the player. 这将返回从图块的中心点到播放器中心点的距离的Vector2i(x和y)。 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. 如果左侧有墙砖,并且玩家的x距离<0(玩家位于当前砖的中心点的左侧),则玩家将移至墙旁砖的x。
  • 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. 如果右侧有墙,并且玩家的x距离> 0(当前图块中心点的右侧),则玩家将移动到墙旁边的图块x。
  • This continues for the Y axis in the same way. Y轴以相同的方式继续。

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. 我同时使用了播放器x <0和播放器y <0的上述方法,但没有解决。 I have made some images to better explain my problem. 我制作了一些图像以更好地解释我的问题。

问题图像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. 绿色磁贴显示玩家的当前磁贴,红色方块显示玩家的当前位置。

问题图像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. 没有粉红色的瓷砖与任何东西碰撞,因此不需要检查玩家的x和y。

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

Your phys method could return boolean depending on whether the player is colliding with any of the walls. 您的phys方法可以返回boolean值,具体取决于玩家是否与任何墙壁碰撞。 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. 这将引入一种更干净的方法来迭代phys方法中的冲突检查。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM