簡體   English   中英

java 2d 平台游戲碰撞錯誤

[英]java 2d platformer collision bug

當角色走入牆壁時,他們無法通過 go 但是當跳入牆壁時,玩家會根據您擊中瓷磚的高度被向上或向下傳送。

我怎樣才能讓玩家在撞牆時摔倒?

移動和碰撞檢測發生在 Player.java 中。

我認為如果玩家站在一個塊上(for循環中的第一次檢查),這與簽入有關,但我不確定。

鏈接到 github 上的文件

我的問題的直觀表示

這就是我檢查碰撞的方式:

public void collisionCheck(){
    for (ArrayList<Integer> tile: Game.tileCoordinates){
        //current row of the player
        int row = y / Game.TILE_SIZE;
        // the row below the player
        int rowBelow = y / Game.TILE_SIZE + 1;
        // the column the left of the player is in
        int columnLeft = x / Game.TILE_SIZE;
        // the column the right of the player is in
        int columnRight = (x + width) / Game.TILE_SIZE;

        // check if the player goes through the block below
        if (tile.get(1) / Game.TILE_SIZE == rowBelow){
            if ((tile.get(0) / Game.TILE_SIZE == columnLeft || 
                 tile.get(0) / Game.TILE_SIZE == columnRight)) {
                y = tile.get(1) - height;
                if (jumping) {
                    jumping = false;
                    Game.spacePressed = false;
                    dustAfterShowed = false;
                    xAfterJump = x;
                    yAfterJump = y;
                }
                if (falling){
                    falling = false;
                }
            }
        }

        // check if the player goes through the side of a block
        if (y <= tile.get(1) && y + height <= tile.get(1) + Game.TILE_SIZE &&
               y + height >= tile.get(1) && row == tile.get(1) / Game.TILE_SIZE) {
            // left side
            if (x + width >= tile.get(0) && x <= tile.get(0)) {
                x = tile.get(0) - width;
            }
            // right side
            if (x + width >= tile.get(0) + Game.TILE_SIZE && 
                    x <= tile.get(0) + Game.TILE_SIZE) {
                x = tile.get(0) + Game.TILE_SIZE;
            }
        }
        // check if the player hits a block from below
        if (tile.get(1) / Game.TILE_SIZE == row && 
               (tile.get(0) / Game.TILE_SIZE == columnLeft ||
                tile.get(0) / Game.TILE_SIZE == columnRight)){
            if (jumping) {
                y = tile.get(1) + Game.TILE_SIZE;
                velocity = 0;
            }
        }
    }

我通過添加一些布爾值來修復它,當玩家從側面撞到牆壁時,它現在不再檢查底部瓷磚碰撞(使玩家跌倒而不是停留在半空中)。

檢查頂部瓷磚碰撞時,請提前檢查。 如果您不提前檢查,它會檢測到側面碰撞,因為左上角和右上角的像素都在圖塊中。

    for (ArrayList<Integer> tile: Game.tileCoordinates){
        sideCollision = false;
        blockRight = false;
        blockLeft = false;

        // check for side collision, if colided then disable movement
        if ((y / Game.TILE_SIZE == tile.get(1) / Game.TILE_SIZE) &&
                (x < tile.get(0) && x + width > tile.get(0) &&
                 x + width < tile.get(0) + Game.TILE_SIZE)) {
            sideCollision = true;
            blockRight = true;
            x = tile.get(0) - width - 1;
        }

        if ((y / Game.TILE_SIZE == tile.get(1) / Game.TILE_SIZE) &&
                (x < tile.get(0) + Game.TILE_SIZE && x > tile.get(0) &&
                 x + width > tile.get(0) + Game.TILE_SIZE)) {
            sideCollision = true;
            blockLeft = true;
            x = tile.get(0) + Game.TILE_SIZE + 1;
        }

        // if player is about to hit head, stop upwards movement
        if ((y - velocity <= tile.get(1) + Game.TILE_SIZE &&
                 y - velocity > tile.get(1)) && ((x >= tile.get(0) &&
                 x <= tile.get(0) + Game.TILE_SIZE) ||
                (x + width >= tile.get(0) &&
                 x + width <= tile.get(0) + Game.TILE_SIZE))){
            y = tile.get(1) + Game.TILE_SIZE;
            velocity = 0;
        }

        // if there is no side collision, check for bottom collision
        if (!sideCollision) {
            if ((y / Game.TILE_SIZE + 1 == tile.get(1) / Game.TILE_SIZE) &&
                    (y + height > tile.get(1)) &&
                   ((x >= tile.get(0) && x <= tile.get(0) + Game.TILE_SIZE) ||
                    (x + width >= tile.get(0) &&
                     x + width <= tile.get(0) + Game.TILE_SIZE))) {
                y = tile.get(1) - height;
                if (jumping) {
                    jumping = false;
                    Game.spacePressed = false;
                    dustAfterShowed = false;
                    xAfterJump = x;
                    yAfterJump = y;
                }
                if (falling) {
                    falling = false;
                }
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM