Java Smooth 2D Rectangle collision?

寵の児 提交于 2019-12-11 12:03:23

问题


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:

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.


回答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.



来源:https://stackoverflow.com/questions/7753549/java-smooth-2d-rectangle-collision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!