Point and Rectangle collision
/** * * @param x1 Point * @param y1 point * @param x2 rectangle View x * @param y2 rectangle View y * @param w Rectangle View Width * @param h Rectangle v Iew High * @return */public static boolean iscollsion (int x1, int y1, int x2, int y2, int w, int h) {if (X1 >= x2 &&am P X1 <= x2 + w && y1 >= y2 && y1 <= y2 + h) {return true;} return false;}
Rectangular collisions
/** * Detects if two rectangles collide * @return */public boolean iscollsionwithrect (int x1, int y1, int w1, int h1, int x2,int y2, int w2, int H2) {if (x1 >= x2 && x1 >= x2 + W2) {return false;} else if (x1 <= x2 && x1 + W1 <= x2) {retur n false;} else if (y1 >= y2 && y1 >= y2 + H2) {return false;} else if (y1 <= y2 && y1 + h1 <= y2) {retur n false;} return true;}
Point (X1,X2), center (x2,y2), radius r
if (Math.sqrt (Math.pow (X1-X2, 2) + Math.pow (Y1-y2, 2)) <= R) {//If the point and center distance is less than or equal to the radius then the collision is considered to be true;}
Circles and Circles
/** * Round collision * * @param x1 * Circle 1 center x Coordinate * @param y1 * Circle 2 Center x coordinate * @param x2 * Circle 1 Center y coordinate * @param y2 * Round 2 The Center y coordinate * @param r1 * radius of Circle 1 * @param R2 * radius of Circle 2 * @return */private boolean iscollisionwithcircle (int x1, int y1, int x2, int y2,int r1, int r2) {//MATH.SQRT: Open square//Math.pow (double x, double y): X's Y-square//Cartesian coordinate system, parallel to point 1 and point 2, |x1-x2| for transverse right-angled side, |y 1-y2| for longitudinal right-angled edge of the Pythagorean Theorem c^2=a^2+b^2if (math.sqrt (Math.pow (X1-X2, 2) + Math.pow (Y1-y2, 2)) <= R1 + r2) {//If the center distance of the two circles is less than or equal to two circles The radius and is considered to be a collision return true;} return false;}
Collision detection algorithms: Point and rectangle collisions, point and circle collisions, rectangular collisions, circular collisions