SAT Platformer顶部碰撞检测

时间:2018-06-19 16:24:43

标签: java collision-detection game-engine game-development

我已经解决了字符顶部以外的碰撞检测问题。我能够检测到侧面和底部并做出反应,但是当玩家穿过平台底部时,他们会被推到顶部。我需要完成此操作才能继续我的游戏引擎,请帮忙!!!

代码:

private void move() {
    float ox = position.x;
    float oy = position.y;
    xa = 0;
    ya = 0;

    if (input.isKey(KeyEvent.VK_A)) xa -= speed;
    if (input.isKey(KeyEvent.VK_D)) xa += speed;
    position.x += xa;
    hull.setPosition(position);

    for (int tx = 0; tx < level.tiles.length; tx++) {
        for (int ty = 0; ty < level.tiles[tx].length; ty++) {
            TileEntity tile = level.tiles[tx][ty];

            if (tile != null && SAT.collision(hull, tile.hull) && tile.position.y <= position.y + height / 2) {
                position.x = ox;
                xa = 0;
            }
        }
    }

    if (input.isKey(KeyEvent.VK_W) && !jumping) {
        jumping = true;
        velY = (GRAVITY - jumpHeight);
    }
    applyGravity();

    position.y += ya;
    hull.setPosition(position);

    for (int tx = 0; tx < level.tiles.length; tx++) {
        for (int ty = 0; ty < level.tiles[tx].length; ty++) {
            TileEntity tile = level.tiles[tx][ty];

            if (tile != null && SAT.collision(hull, tile.hull) && tile.position.y > position.y - height) {
                position.y = tile.position.y - height;
                ya = 0;
                velY = 0;
                jumping = false;
            }
        }
    }
    if (velY > 0) falling = true;
}

0 个答案:

没有答案