1
votes

I have been working on a 2D game, kind of like Terraria, and i have got to the stage where i am ready to do collision detection. I tried it myself, but it... well, its very strange. I was wondering if anybody could help me with this. I store all of my blocks in a 2D array:

Block[][] map = new Block[mw][mh];

where mw is map width and mh is map height (in terms of number of blocks). Each Block is an image that is displayed as 16x16 pixels. Here is what i attempted, i know that it is wrong but i dont really know what i need to do.

private void checkCollision() {

    for(int x = -1; x <= 1; x++){
        for(int y = -1; y <= 2; y++){
            Rectangle obj = new Rectangle((int)Block.getXOnScreen(xblock+x), (int)Block.getYOnScreen(yblock+y), 16, 16);
            try{
                if(main.map[(int) (xblock+x)][(int) (yblock+y)].solid && obj.intersects(bounds()){
                    if(y <= -1 && velY > 0){
                        velY = 0;
                        System.out.println("Collision below");
                        onground = true;
                    }else if(y >= 2 && velY < 0){
                        velY = 0;
                        System.out.println("Collision above");
                    }
                    if(x <= -1 && velX < 0){
                        velX = 0;
                        System.out.println("Collision left");
                    }else if(x >= 1 && velX > 0){
                        velX = 0;
                        System.out.println("Collision right");
                    }
                }
            }catch(Exception e){}
        }
    }
}

I call this method every tick like so, but it doesn't collide with anything so the player just falls.

public void tick(){
    xblock += velX;
    yblock += velY;
    velY += gravity;

    checkCollision();
} 

If anyone knows how to do collision detection efficiently please can you share this with me. Thanks! :)

1
What value range does your velocity have? Because it looks to me, if you have velocities that are larger than the size of a single block you don't detect the collision. (because your x/yblock variables "jump" past the blocks you want to hit)Nitram
Casey aka handmadehero has a few good tutorials on collision detection in tile based games here, note that he translates from his tile map (integer indices) to actual world coordinates (floats) and does more accurate collision handling on them.BeyelerStudios
the gravity variable is 0.8user3679700
ok i will take a look at that thank youuser3679700

1 Answers

-1
votes

Based on the few JavaFX/Swing applications that I once wrote, the way you want to detect collision is by always keeping track of where the object is, and when it moves check to see that it is new location (old + movement count) is not exceeding the limits of the map. If it does, you want to reset its position to be right on limit so that it can go in all other directions that are valid. If you keep trying to move outside of the limits then it should keep resetting your position.

So say the limit of the page is 500 pixels wide, you are in position 495, and each move is 10, next time you move you would in position 505, so that's clearly out of range. Thus you run a check if(currentPosition > limit) {reset position to limit} thus your currentPosition will be set to 500 which is right on the border, and within the limits.