简体   繁体   中英

Game collision detection with rectangles

I have started with my first game but cannot figure out how the collision detection should work. The goal is that the player should not be able to go into the wall.

I have following classes Player(), Wall(), Levels() and Game()

Player class is loading the player and handling movement and collisions for the player.

Wall is creating walls, I have one Arraylist in this class which is creating an rectangle for each wall that is created.

Levels class is loading creating and loading all the levels.

Game class is handling the whole game loop and more.

I have tried to compare x and y positions with player and wall, right now I am trying to use the .intersect class which has not been succesfull yet.

Part of Player Class:

public void collision() {
Rectangle r3 = getOffsetBounds();
for (int i = 0; i < Wall.wallList.size(); i++) {
    Rectangle w1 = Wall.wallList.get(i);
    if (r3.intersects(w1)) {

    }}}

public int moveUp(int velY) {
    collision();
    y -= velY;
    return y;
}

public int moveDown(int velY) {
    collision();
    y += velY;
    return y;
}

public int moveLeft(int velX) {
    collision();
    x -= velX;  
    return x;
}

public int moveRight(int velX) {
    collision();
    x += velX;
    return x;
}

public Rectangle getOffsetBounds() {
    return new Rectangle(x + velX, y + velY, width, height);
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

public int getVelX() {
    return velX;
}

public void setVelX(int velX) {
    this.velX = velX;
}

public int getVelY() {
    return velY;
}

public void setVelY(int velY) {
    this.velY = velY;
}}

Part of Wall Class:

static ArrayList<Rectangle> wallList = new ArrayList<Rectangle>();

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

public void setVisisble(Boolean visible) {
    this.visible = visible;
}

public Rectangle getBounds() {
    return new Rectangle(x,y,width,height);
}

public Rectangle setBounds(int x,int y,int width,int height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    return new Rectangle(x,y,width,height);
}}

Part of Levels class where the wall list is setting

                if(level1[i][j]==1) {
                w = new Wall(j*25, i*25, width, height);
                w.paint(g);
                Wall.wallList.add(w.setBounds(j*25+10, i*25+10, width, height));
                }

Part of Game class where keybindings are

public void KeyBindings() {
    keyManager.addKeyBinding(lvl, KeyEvent.VK_UP, "moveUp", (evt) -> {

        lvl.player.moveUp(5);
    });
    keyManager.addKeyBinding(lvl, KeyEvent.VK_DOWN, "moveDown", (evt) -> {

        lvl.player.moveDown(5);
    });
    keyManager.addKeyBinding(lvl, KeyEvent.VK_LEFT, "moveLeft", (evt) -> {

        lvl.player.moveLeft(5);
    });
    keyManager.addKeyBinding(lvl, KeyEvent.VK_RIGHT, "moveRight", (evt) -> {

        lvl.player.moveRight(5);
    });
}

This is how I always handle any collisions

if (((x1<x2 && x1+w1>x2) || (x2<x1 && x2+w2>x1)) && 
    ((y1<y2 && y1+h1>y2) || (y2<y1 && y2+h2>y1)))

碰撞

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM