Java平滑2D矩形碰撞?

发布于 2024-12-10 06:22:03 字数 2491 浏览 0 评论 0原文

大家好,我正在制作一个迷宫游戏,我正在尝试找出如何使 2D 碰撞变得平滑。目前,我想出了一些非常糟糕的碰撞代码,因为我还没有完成这部分,而且它有故障,有时会进入墙内,然后你就无法出去。

我的代码:

    public void checkCollision() {
    Rectangle player_rectangle = new Rectangle(player.getX(),player.getY(),32,32);

    for(Wall wall : walls) {

        Rectangle wall_rectangle = new Rectangle(wall.getX(), wall.getY(), 32,32);

        if(player_rectangle.intersects(wall_rectangle)) {
            if(player.xspeed == 1) {
                player.xspeed = 0;
                player.x -= 1.2;
            } else {

                if(player.xspeed == -1) {
                    player.xspeed = 0;
                    player.x += 1.2;
                }
                else
                    if(player.yspeed == 1) {
                        player.yspeed = 0;
                        player.y -= 1;
                    } else {
                        if(player.yspeed == -1) {
                            player.yspeed = 0;
                            player.y += 1;
                        }
                    }

            }
        }

    }

}

什么是更好的碰撞方法?这是我将拥有的地图类型的图片:

在此处输入图像描述

编辑:

新的碰撞代码:

            if (player_rectangle.intersects(wall_rectangle)) {
            Rectangle intersection = (Rectangle) player_rectangle.createIntersection(wall_rectangle);

            if (player.xspeed > 0) {
                player.x -= intersection.getWidth();
            }

            if (player.yspeed > 0) {
                player.y -= intersection.getHeight();
            }

            if (player.xspeed < 0) {
                player.x += intersection.getWidth();
            }

            if (player.yspeed < 0) {
                player.y += intersection.getHeight(); 
            }

            Print(Integer.toString(intersection.width) + ", " + Integer.toString(intersection.height));

        }

运动代码:

    public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if(key == KeyEvent.VK_LEFT) {

        xspeed = -1;

    } else {
        if(key == KeyEvent.VK_RIGHT) {

                xspeed = 1;

        } else {
            if(key == KeyEvent.VK_UP) {
                yspeed = -1;
            } else {
                if(key == KeyEvent.VK_DOWN) {
                    yspeed = 1;
                }
            }
        }
    }
}

感谢您给我的任何帮助。

Hey guys i'm making a maze game and I'm trying to figure out how to make a 2D collision smooth. Currently I have some really bad collision code that I thought up since I haven't done this part yet and it is glitchy and sometimes goes inside the wall then you can't get out.

My code:

    public void checkCollision() {
    Rectangle player_rectangle = new Rectangle(player.getX(),player.getY(),32,32);

    for(Wall wall : walls) {

        Rectangle wall_rectangle = new Rectangle(wall.getX(), wall.getY(), 32,32);

        if(player_rectangle.intersects(wall_rectangle)) {
            if(player.xspeed == 1) {
                player.xspeed = 0;
                player.x -= 1.2;
            } else {

                if(player.xspeed == -1) {
                    player.xspeed = 0;
                    player.x += 1.2;
                }
                else
                    if(player.yspeed == 1) {
                        player.yspeed = 0;
                        player.y -= 1;
                    } else {
                        if(player.yspeed == -1) {
                            player.yspeed = 0;
                            player.y += 1;
                        }
                    }

            }
        }

    }

}

What is a better way to do the collision? here is a picture of the types of maps I will have:

enter image description here

EDIT:

New Collision code:

            if (player_rectangle.intersects(wall_rectangle)) {
            Rectangle intersection = (Rectangle) player_rectangle.createIntersection(wall_rectangle);

            if (player.xspeed > 0) {
                player.x -= intersection.getWidth();
            }

            if (player.yspeed > 0) {
                player.y -= intersection.getHeight();
            }

            if (player.xspeed < 0) {
                player.x += intersection.getWidth();
            }

            if (player.yspeed < 0) {
                player.y += intersection.getHeight(); 
            }

            Print(Integer.toString(intersection.width) + ", " + Integer.toString(intersection.height));

        }

Movement Code:

    public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if(key == KeyEvent.VK_LEFT) {

        xspeed = -1;

    } else {
        if(key == KeyEvent.VK_RIGHT) {

                xspeed = 1;

        } else {
            if(key == KeyEvent.VK_UP) {
                yspeed = -1;
            } else {
                if(key == KeyEvent.VK_DOWN) {
                    yspeed = 1;
                }
            }
        }
    }
}

Thanks for any help you can give me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ弥猫深巷。 2024-12-17 06:22:03

您可以使用Rectangle.createIntersection(Rectangle)

如果玩家向正方向移动,只需从他的位置中减去所得矩形的宽度和高度,如果他向负方向移动,则将它们相加。

不要将速度设置为零,这可能会导致播放器挂起。

EDIT

if (playerRectangle.intersects(wallRectangle) {
    Rectangle intersection = (Rectangle) playerRectangle.createIntersection(wallRectangle);

    if (player.xspeed > 0) {
        player.x -= intersection.getWidth();
    }

    if (player.yspeed > 0) {
        player.y -= intersection.getHeight();
    }

    if (player.xspeed < 0) {
        player.x += intersection.getWidth();
    }

    if (player.yspeed < 0) {
        player.y += intersection.getHeight();
    }
}

EDIT2

据我所知, keyPressed 仅在您按下某个键时触发,而不是在您释放该键时触发。尝试这样:

private boolean up, down, left, right;

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KEyEvent.VK_UP) {
        up = true;
    }

    ...
}

public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KEyEvent.VK_UP) {
        up = false;
    }

    ...
}


// Call this in the mainthread
public void updateMovement() {
    if (up) {
        player.xspeed = 1;
    }

    ...
}

所以在每一帧中,您更新运动,移动玩家,然后检查碰撞。

You can use Rectangle.createIntersection(Rectangle).

If the player moves to a positive direction, just subtract the width and height of the resulting Rectangle from his position, if he moves to a negative, add them.

Don't set the speed to zero, this could cause the player to hang.

EDIT

if (playerRectangle.intersects(wallRectangle) {
    Rectangle intersection = (Rectangle) playerRectangle.createIntersection(wallRectangle);

    if (player.xspeed > 0) {
        player.x -= intersection.getWidth();
    }

    if (player.yspeed > 0) {
        player.y -= intersection.getHeight();
    }

    if (player.xspeed < 0) {
        player.x += intersection.getWidth();
    }

    if (player.yspeed < 0) {
        player.y += intersection.getHeight();
    }
}

EDIT2

As far as I know, keyPressed is only triggered when you press a key, and not when you release it. Try it like this:

private boolean up, down, left, right;

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KEyEvent.VK_UP) {
        up = true;
    }

    ...
}

public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KEyEvent.VK_UP) {
        up = false;
    }

    ...
}


// Call this in the mainthread
public void updateMovement() {
    if (up) {
        player.xspeed = 1;
    }

    ...
}

So in every frame, you update the movement, move the player and then check the collision.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文