java - Collision Detection between player and tiles almost works -


i have been stuck on making collision detection in game (its kind of terraria) while made code and... well, kind of works. works if collision above or on left of player, if collision on right, or below, instead of bouncing back, player accelerates through blocks until there empty space. here code made:

private void checkcollision() {     for(int x = (int) (xpos-1); x <= xpos+1; x++){         if(x < 0 || x > main.mw-1) continue;          for(int y = (int) (ypos-2); y <= ypos+1; y++){             if(y < 0 || y > main.mh-1) continue;              if(main.map[x][y] == null) continue;             if(!main.map[x][y].solid) continue;             if(main.map[x][y].blocktype == -1) continue;              double distance = math.sqrt((xpos-x)*(xpos-x) + (ypos-y)*(ypos-y));              if(distance > 1.0){                 continue;             }else{                  double x_overlap = math.max(0, math.min(xpos + 16, x + 16) - math.max(xpos, x));                 double y_overlap = math.max(0, math.min(ypos + 32, y + 16) - math.max(ypos, y));                 double overlaparea = x_overlap * y_overlap;                  if(overlaparea > 0){                      if(x_overlap > y_overlap){                         yblock += y_overlap/2;                     }                     if(x_overlap < y_overlap){                         xblock += x_overlap/2;                     }                      //guessing need here make player                        go other way if block on other side                 }             }         }     } } 

so how make player bounce if block colliding on right or below. there way can make smoother - right player bouncing on place. thanks! :)

what want keep track of player's location, , if location after moving out of bounds can reset player's position right on edge of limit.

that's how dealt collision detection, answered question similar 1 though folk decided downvote answer, go figure.


Comments