资源下载平滑的二维矩形碰撞? [英] Java Smooth 2D Rectangle collision?

查看:127
本文介绍了资源下载平滑的二维矩形碰撞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿家伙,我正在做一个迷宫游戏,我试图找出如何使2D碰撞顺利。目前,我有一些非常糟糕的碰撞code,我想起来,因为我还没有做这部分还没有,这是出问题的,有时去墙内,那么你无法脱身。

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.

我的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:

编辑:

新的碰撞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));

        }

运动code:

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.

推荐答案

您可以使用 Rectangle.createIntersection(矩形)

如果玩家移动到积极的方向发展,只是减去的宽度和高度所产生的矩形从他的位置,如果他移动到负,添加它们。

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.

修改

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

据我知道,当你在释放PSS的关键$ P $,而不是它的关键pressed时才会触发。尝试这样的:

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.

这篇关于资源下载平滑的二维矩形碰撞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆