Collision detection algorithms: Point and rectangle collisions, point and circle collisions, rectangular collisions, circular collisions

Source: Internet
Author: User
Tags pow

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.