Hitbox detection?

so i wanted to make it so if the player is not touching any blocks, they fall.
here is my code:

for (int ix = -2; ix < resolution+4; ix++) {
        for (int iy = 0; iy < height/(width/resolution)+4; iy++) {
          if (terrainbase[playerxpos+ix][playerypos+iy] == 0) {
            rect(visxpos+ix+width/resolution*(ix-1), visypos+iy+width/resolution*(iy-1), width/resolution+1, width/resolution+1);
            if (visxpos+ix+width/resolution*(ix-1)-50 < mouseX-25 && visxpos+ix+width/resolution*(ix-1)+50 > mouseX+50
            && visypos+iy+width/resolution*(iy-1) < mouseY && visypos+iy+width/resolution*(iy-1)+50 > mouseY) {
              visypos -= (width/resolution)/10;
              //println(frameCount);
            }
          }
        }
      }

the first & second for loops just loop through the area the player can see.

terrainbase is the array, that saves the world

“width/resolution” is the width of a tile in the world

the first if() statement checks if a tile is air

the first rectangle int the loop draws rectangles on the screen where also the tiles are

the second if() statement tries to check if the player is touching only air.

“visxpos+ix+width/resolution*(ix-1)” and the others that look very alike, are the current tile of the terrain that is air

mouseX and mouseY represent the postion of the player on screen

“visypos -= (width/resolution)/10” moves the player down.

image
that big black box is the player.

problem is sometimes the player isnt moved down or it is beeing moved down even when they are touching something.

im trying to do this kind of collision:

image
image
image
image

blue means fall, green means stay.

i hope this kinda helps.

1 Like