为什么我的碰撞检测方法不起作用?

Why does my method for collision detection not work?

我了解碰撞检测的基础,并开发了我自己的碰撞检测方法(据我所知)。但是,它似乎没有用。我所做的不是在精灵、形状和其他对象周围绘制完整的矩形,而是简单地制作了在对象的所有侧面(左、右、顶部和底部)绘制线条(非常细的矩形)的方法。

在指定精灵的左、右、上、下绘制细矩形的方法。这可能不是最好的碰撞方式,但这是我所拥有的,并且对所有想法和不同的解决方案持开放态度!

注:(int)(x-Game.getCamera().getX()),(int)(y + height -Game.getCamera().getY()),就是简单的对于相机,所以当玩家移动时,树木(在我的例子中是我想要碰撞的物体)不会随着他移动

public Rectangle getBounds() {
return new Rectangle((int)(x-Game.getCamera().getX()),
        (int)(y + height - Game.getCamera().getY()),width,height-height);
}

public  Rectangle getBoundsTop() {
return new Rectangle((int)(x-Game.getCamera().getX()),
        (int)(y- Game.getCamera().getY()),width,height-height);
}

public  Rectangle getBoundsRight() {
return new Rectangle((int)(x + width -Game.getCamera().getX()),
        (int)(y - Game.getCamera().getY()),width - width,height);

}

public  Rectangle getBoundsLeft() {
return new Rectangle((int)(x -Game.getCamera().getX()),
        (int)(y- Game.getCamera().getY()),width - width,height);

    }

实际碰撞正在检查(在播放器 class 中)-

if(getBounds().intersects(treeTile.getBoundsTop())) {
    //What happens when they collide
    //When the bottom of the player collides with the top of the tree.
    }
if(getBoundsTop().intersects(treeTile.getBounds())) {
    //What happens when they collide
    //When the top of the player collides with the bottom of the tree

    }
if(getBoundsRight().intersects(treeTile.getBoundsLeft())) {
    //What happens when they collide
    //when the right side of the player collides with the left of the 
    //tree
    }
if(getBoundsLeft().intersects(treeTile.getBoundsRight())) {
    //What happens when they collide
    //When the left of the player collides with the right of the tree
    }

感谢所有能得到的帮助

这里是碰撞和移动代码-

for (int i = 0; i < handler.object.size(); i++) {
        Square handle = handler.object.get(i);
    if (getBounds().intersects(treeTile.getBoundsTop())) {
            handle.setvelY(-1);

    }
    if (getBoundsTop().intersects(treeTile.getBounds())) {
            handle.setvelY(0);

    }
    if (getBoundsRight().intersects(treeTile.getBoundsLeft())) {
            handle.setvelX(0);
    }
    if (getBoundsLeft().intersects(treeTile.getBoundsRight())) {
            handle.setvelX(0);

    }
    }

运动-

    public void keyPressed(KeyEvent e) {

    int code = e.getKeyCode();



    for (int i = 0; i < handler.object.size(); i++) {
        Square handle = handler.object.get(i);

        if (handle.getId() == ObjectId.Zelda) {


            if (code == KeyEvent.VK_D) {
                handle.right();
            }
            if (code == KeyEvent.VK_A) {
                handle.left();
            }
            if (code == KeyEvent.VK_W) {
                handle.up();
            }
            if (code == KeyEvent.VK_S) {
                handle.down();
            }
 }

我的新碰撞想法 -

boolean walkRight = true;
boolean walkLeft = true;
boolean walkDown = true;
boolean walkUp = true;
public void tick() {

    if(walkRight == true) {
        velX = 2;

    }if(walkLeft == true) {
        velX = -2;

    }if(walkDown == true) {
        velY = 2;

    }if(walkUp == true) {
        velY = -2;
    }


if (getBounds().intersects(treeTile.getBoundsTop())) {
       walkDown = false;

}
if (getBoundsTop().intersects(treeTile.getBounds())) {
        walkUp = false;
}
if (getBoundsRight().intersects(treeTile.getBoundsLeft())) {
        walkRight = false;

}
if (getBoundsLeft().intersects(treeTile.getBoundsRight())) {
        walkLeft = false;

} 

类似这样的,不完全是,只是一个例子。

您是否尝试过使用 1 而不是宽度 - 宽度和高度 - 高度?

我认为这些宽度/高度的计算结果为 0 会导致相交函数失败。

编辑

我认为你把这个复杂化了。如果你只有一个大的碰撞盒,而不是四个小的,那么无论你把玩家移动到哪里,你都可以

// Square previousPosition = currentPosition
// do move player code, which presumably updates the currentPosition
// loop through all the trees you have
//    if the player is colliding with a tree
//        currentPosition = previousPosition

这应该可以处理玩家与树的任何一侧发生碰撞的情况,因为我们只是将它们移回到它们之前所在的位置。