How to make collision detection with many walls (maze)?

In my game, the player goes into the maze. I can’t figure out how to correctly detect collisions with walls. It is easy to perform collision detection to stay in a specific area:

if (x > rightWallX - playerWidth) x = rightWallX - playerWidth;
if (x < leftWallX) x = leftWallX;
//...

But how could I detect collisions of many walls?

I can do the usual collision detection without correction (for example, if (intersecting) return true;), but I cannot fix it correctly. If I just keep the old xand yand reset them,

  • The object never touches the wall.
  • If the object can rise, but is blocked to the right, it will not go up, it simply will not move.

How is a collision detected in the maze?

0
source share
3 answers

The easiest way, as soon as you decide to detect a collision, to correct the collision is to move the actor to the closest valid position to where the actor would not be for the object he is facing. This does not imply inertia, but it is enough for labyrinth games or top-down games on the map.

, , x y. , ( ), . ( : - ).

, ( ). , (, ), - .

+1

Rectangle.intersects():

    public Rectangle Player(){
         return new Rectangle(PlayerX,PlayerY,PlayerWidth,PlayerHeight);
         //we do this for getting players x and y values every tick
    }

    if(Player().intersects(new Rectangle(0,0,100,50)))//if(player touching wall)

Rectangle (0,0,100,50) - , .

+1

, 2D- , , . [] [] tiles = new Tile [levelWidth] [levelHeight]; . , , - , , .

getTile.

public Tile[][] getTile(int x, int y) {
    if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight()) {
    return new VoidTile();
} else {
    return tiles[x][y];
}
}

Tile.java isSolid() , , . Tile.java, , . , , , . , , :)

.intersects() .contains() Sprite. .

, player.java checkBlockedDirection (int x, int y), .

    public void checkBlockedDirection(int x, int y) {
    boolean u = map.getTile(x, y - 1).isSolid();
    boolean d = map.getTile(x, y + 1).isSolid();
    boolean l = map.getTile(x - 1, y).isSolid();
    boolean r = map.getTile(x + 1, y).isSolid();

    if (u) {
        uBlocked = true;
        System.out.println("up tile blocked");
    } else {
        uBlocked = false;
    }
    if (d) {
        dBlocked = true;
        System.out.println("down tile blocked");
    } else {
        dBlocked = false;
    }
    if (l) {
        lBlocked = true;
        System.out.println("left tile blocked");
    } else {
        lBlocked = false;
    }
    if (r) {
        rBlocked = true;
        System.out.println("right tile blocked");
    } else {
        rBlocked = false;
    }
}

public void tick() {
    float dx = 0;
    float dy = 0;

    if (input.up.isPressed()) {
        direction = 0;
    } else if (input.down.isPressed()) {
        direction = 2;
    } else if (input.left.isPressed()) {
        direction = 3;
    } else if (input.right.isPressed()) {
        direction = 1;
    } else {
        direction = 4; // standing
    }

    checkBlockedDirection((int)x, (int)y);


    if (input.up.isPressed() && y > 0 && !uBlocked) {
            dy += -speed;
    } else if (input.down.isPressed() && y < map.getHeight() - 1 && !dBlocked) {
            dy += speed;
    } else if (input.left.isPressed() && x > 0 && !lBlocked) {
            dx += -speed;
    } else if (input.right.isPressed() && x < map.getWidth() - 1 && !rBlocked) {
            dx += speed;
    }
    x += dx;
    y += dy;

}

, , , . , , , .

, , :)

, :)

Enjoy

0

All Articles