Only way i could get it to work is to make two collison methods to check X and then Y collision separately, if x returned true the player moved x and if y = true then player.moveY..
The meathods create a temporary variable which represents where the player will be, for the x method for example
int newX = player.getX() + player.getVelY()
work out where he is on the grid by divison to work out the tile he is on. Work out a tile for each corner of the player e.g. the top left point, on 64x64 tiles
tileTopLeftPointX = newX/64;
tileTopRightPointY = player.getY()/64; // as this is just checking x collision player.getY() is just as it is
Then if this new tile is solid that means if the player moves he will be colliding with something so return the meathod false so the player doesnt move.
To stop the player from having a gap between him and the wall hes walking into if he has a high x velocity try to find the minmum x velocity the player could have moved and not intersect with the tile, if this is found then set the player velocty X to this value so he will squish against the wall, you can change how accurate you want it to be e.g.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| if(tiles[i][p].isSolid()){ if(tiles[i][p].getBounds().contains(topLeftX, topLeftY)){ while(!(nDX > -0.2 && nDX < 0.2)){ if(nDX > 0){ nDX -= 0.02; }else if(nDX < 0){ nDX += 0.02; } topLeftX = (int) (player.getX() + nDX); if(!(tiles[i][p].getBounds().contains(topLeftX, topLeftY))){ player.setDX(nDX); return true; } } return false; }else if |