android - How to check if two rectangles are "on" each other -
i want check if enemy_green_light (enemy_green_light_xenemy_green_light_y) , player (x, y) touched each other. however, code doesn't want, , can met though didn't. (x, y) left corner coordinates. thanks!
boolean before_x=(x<enemy_green_light_x)&&(x+player.getwidth()>enemy_green_light_x); boolean after_x=(x>enemy_green_light_x)&&(enemy_green_light_x+enemy_green_light.getwidth()>x); boolean before_y=(y<enemy_green_light_y)&&(y+player.getheight()>enemy_green_light_y); boolean after_y=(y>enemy_green_light_y)&&(enemy_green_light_y+enemy_green_light.getheight()>y); if(before_x||after_x) if(before_y||after_y) { pressed=true; }
edit: tried code too, , didn't work.
float p_y_1,p_y_2,p_x_1,p_x_2,e_y_1,e_y_2,e_x_1,e_x_2; p_y_1=y; p_y_2=y+player.getheight(); p_x_1=x; p_x_2=x+player.getwidth(); e_y_1=enemy_green_light_y; e_y_2=enemy_green_light_y+enemy_green_light.getheight(); e_x_1=enemy_green_light_x; e_x_2=enemy_green_light_x+enemy_green_light.getwidth(); if(!(p_y_2<e_y_1||p_y_1>e_y_2||p_x_2<e_x_1||p_x_1>e_x_2)) pressed=true;
here have explanation how determine if 2 rectangles overlap
the solution expression
! ( p2.y < p3.y || p1.y > p4.y || p2.x < p3.x || p1.x > p4.x )
Comments
Post a Comment