Collision Detection Algorithm: point-and-rectangle collision, point-and-circle collision, rectangular collision, and circular collision

Source: Internet
Author: User

Collision Detection Algorithm: point-and-rectangle collision, point-and-circle collision, rectangular collision, and circular collision

 

 

Point and rectangle collision

 

[Java] 
  1. /**
  2. *
  3. * @ Param x1
  4. * @ Param y1
  5. * @ Param x2 rectangular view x
  6. * @ Param y2 rectangular view y
  7. * @ Param w rectangular view width
  8. * @ Param h rectangular view height
  9. * @ Return
  10. */
  11. Public static boolean isCollsion (int x1, int y1, int x2, int y2, int w, int h ){
  12. If (x1> = x2 & x1 <= x2 + w & y1> = y2 & y1 <= y2 + h ){
  13. Return true;
  14. }
  15. Return false;
  16. }
    Rectangular collision

     

     

    [Java]
    1. /**
    2. * Checks whether two rectangles collide.
    3. * @ Return
    4. */
    5. Public boolean isCollsionWithRect (int x1, int y1, int w1, int h1,
    6. Int x2, int y2, int w2, int h2 ){
    7. If (x1> = x2 & x1> = x2 + w2 ){
    8. Return false;
    9. } Else if (x1 <= x2 & x1 + w1 <= x2 ){
    10. Return false;
    11. } Else if (y1> = y2 & y1> = y2 + h2 ){
    12. Return false;
    13. } Else if (y1 <= y2 & y1 + h1 <= y2 ){
    14. Return false;
    15. }
    16. Return true;
    17. }
      Point (x1, x2), center (x2, y2), radius r

       

       

      [Java]
      1. If (Math. sqrt (Math. pow (x1-x2, 2) + Math. pow (y1-y2, 2) <= r ){
      2. // If the distance between the vertex and the center is smaller than or equal to the radius, the collision is considered
      3. Return true;
      4. }

         

        Circle and circle

         

        [Java]
        1. /**
        2. * Round collision
        3. *
        4. * @ Param x1
        5. * X coordinate of the Center of circular 1
        6. * @ Param y1
        7. * X coordinate of the Center of Circular 2
        8. * @ Param x2
        9. * Y coordinate of the Center of circular 1
        10. * @ Param y2
        11. * Y coordinate of the Center of Circular 2
        12. * @ Param r1
        13. * Radius of circle 1
        14. * @ Param r2
        15. * Radius of circle 2
        16. * @ Return
        17. */
        18. Private boolean isCollisionWithCircle (int x1, int y1, int x2, int y2,
        19. Int r1, int r2 ){
        20. // Math. sqrt: Square
        21. // Math. pow (double x, double y): Power Y of X
        22. // Cartesian coordinate system, parallel line according to point 1 and point 2, | x1-x2 | for horizontal corner edge, | y1-y2 | for vertical straight angle edge according to the stock Theorem c ^ 2 = a ^ 2 + B ^ 2
        23. If (Math. sqrt (Math. pow (x1-x2, 2) + Math. pow (y1-y2, 2) <= r1 + r2 ){
        24. // If the center distance of the two circles is less than or equal to the radius of the two circles and the collision is considered
        25. Return true;
        26. }
        27. Return false;
        28. }

           

Related Article

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.