碰撞检测故障

时间:2019-06-18 04:52:56

标签: java lwjgl

先谢谢了。我正在尝试制作一款具有完美碰撞检测功能的滚动2D游戏,该检测器位于玩家的角落。我遇到的问题是,如果您在墙壁上向左或向下走,效果很好,但是在右侧或向上,似乎可以无限检测到碰撞并将您拖到角落。

我尝试过重设运动,恢复到旧位置,从平移后的位置减去像素,还更改了变量。

package me.Rigidity.Apocalypse;

public class Entity extends Tile {

    private float x, y, xm, ym;

    public Entity(String name, float r, float g, float b, float x, float y, float size) {
        super(name, r, g, b);
        this.size = size;
        this.x = x;
        this.y = y;
    }

    public void move(float x, float y) {
        this.x += x;
        this.y += y;
    }
    public void position(float x, float y) {
        this.x = x;
        this.y = y;
    }
    public void factor(float x, float y) {
        this.xm *= x;
        this.ym *= y;
    }

    public void motion(Map map) {
        motionX(map);
        motionY(map);
    }
    public void motionX(Map map) {
        x += xm;
        if (collision(map)) {
            if (left(map)) {
                x = rightX();
            }
            if (right(map)) {
                x = leftX();
            }
        }
    }
    public void motionY(Map map) {
        y += ym;
        if (collision(map)) {
            if (top(map)) {
                y = bottomY();
                return;
            }
            if (bottom(map)) {
                y = topY();
            }
        }
    }

    public void motion(float x, float y) {
        xm += x;
        ym += y;
    }
    public float x() {
        return x;
    }
    public float y() {
        return y;
    }
    public void x(float x) {
        this.x = x;
    }
    public void y(float y) {
        this.y = y;
    }
    public float xm() {
        return xm;
    }
    public float ym() {
        return ym;
    }

    public Tile bottomleft(Map map) {
        return map.getAbsoluteTile(leftX(), bottomY());
    }
    public Tile bottomright(Map map) {
        return map.getAbsoluteTile(rightX(), bottomY());
    }
    public Tile topleft(Map map) {
        return map.getAbsoluteTile(leftX(), topY());
    }
    public Tile topright(Map map) {
        return map.getAbsoluteTile(rightX(), topY());
    }
    public int leftX() {
        return (int)(((int)(x/Tile.BASIC.size()))*Tile.BASIC.size()+Tile.BASIC.size()-size);
    }
    public int rightX() {
        return (int)(((int)(x/Tile.BASIC.size()))*Tile.BASIC.size()+Tile.BASIC.size());
    }
    public int topY() {
        return (int)(((int)(y/Tile.BASIC.size()))*Tile.BASIC.size()+Tile.BASIC.size());
    }
    public int bottomY() {
        return (int)(((int)(y/Tile.BASIC.size()))*Tile.BASIC.size()+Tile.BASIC.size()-size);
    }
    public boolean top(Map map) {
        return topleft(map) instanceof Solid || topright(map) instanceof Solid;
    }
    public boolean bottom(Map map) {
        return bottomleft(map) instanceof Solid || bottomright(map) instanceof Solid;
    }
    public boolean left(Map map) {
        return topleft(map) instanceof Solid || bottomleft(map) instanceof Solid;
    }
    public boolean right(Map map) {
        return topright(map) instanceof Solid || bottomright(map) instanceof Solid;
    }
    public boolean collision(Map map) {
        return bottomleft(map) instanceof Solid || topleft(map) instanceof Solid || bottomright(map) instanceof Solid || topright(map) instanceof Solid;
    }

}

没有错误消息,但是在右侧或顶部碰撞时结果与预期的不同。左侧和底部正常工作,并产生清晰的碰撞以及滑动。

0 个答案:

没有答案