Collision Detection help frequently used in games

Source: Internet
Author: User

Collision Detection is often used in games. General game engines also carry their own collision detection classes, but sometimes they are not flexible. At this time, we hope we can customize some collision detection classes. Top Tong...

  1. Import Android. Graphics. rect;


  2. Public class collisionutil {

  3. /**

  4. * The rectangle collision detection parameter is X, Y, width, and height.

  5. *

  6. * @ Param X1

  7. * X of the first rectangle

  8. * @ Param Y1

  9. * Y of the first rectangle

  10. * @ Param W1

  11. * W of the first rectangle

  12. * @ Param H1

  13. * H of the first rectangle

  14. * @ Param X2

  15. * X of the second rectangle

  16. * @ Param Y2

  17. * Y of the second rectangle

  18. * @ Param W2

  19. * W of the second rectangle

  20. * @ Param H2

  21. * H of the second rectangle

  22. * [Url = home. php? MoD = Space & uid = 7300] @ return [/url] collision?

  23. */

  24. Public static Boolean isrectcollision (float X1, float Y1, float W1,

  25. Float H1, float X2, float Y2, float W2, float H2 ){

  26. If (X2> X1 & X2> X1 + W1 ){

  27. Return false;

  28. } Else if (X2 <X1 & X2 <x1-W2 ){

  29. Return false;

  30. } Else if (Y2> Y1 & Y2> Y1 + H1 ){

  31. Return false;

  32. } Else if (Y2 <Y1 & Y2 <Y1-H2 ){

  33. Return false;

  34. } Else {

  35. Return true;

  36. }

  37. }


  38. /**

  39. * The rectangle collision detection parameter is a rect object.

  40. *

  41. * @ Param r1

  42. * The first rect object

  43. * @ Param r2

  44. * Second rect object

  45. * @ Return collision?

  46. */

  47. Public static Boolean isrectcollision (rect R1, rect R2 ){

  48. Return isrectcollision (r1.left, r1.top, r1.right-r1.left, r1.bottom

  49. -R1.top, r2.left, r2.top, r2.right-r2.left, r2.bottom

  50. -R2.top );

  51. }


  52. /**

  53. * Round Collision Detection

  54. *

  55. * @ Param X1

  56. * The center of the first circle X

  57. * @ Param Y1

  58. * Y of the center of the first circle

  59. * @ Param r1

  60. * Radius of the first circle

  61. * @ Param X2

  62. * The center of the second circle X

  63. * @ Param Y2

  64. * Y of the center of the second circle

  65. * @ Param r2

  66. * Radius of the second circle

  67. * @ Return collision?

  68. */

  69. Public static Boolean iscirclecollision (INT X1, int Y1, int R1, int X2,

  70. Int Y2, int R2 ){

  71. // The distance between the two points and the radius of the circle is greater than 2

  72. If (math. SQRT (math. Pow (x1-X2, 2) + math. Pow (Y1-Y2, 2)> R1 + R2 ){

  73. Return false;

  74. }

  75. Return true;

  76. }


  77. /**

  78. * Circular and rectangular Collision Detection

  79. *

  80. * @ Param X1

  81. * X of the first rectangle

  82. * @ Param Y1

  83. * Y of the first rectangle

  84. * @ Param W1

  85. * Width of the first rectangle

  86. * @ Param H1

  87. * Height of the first rectangle

  88. * @ Param X2

  89. * Circle center x

  90. * @ Param Y2

  91. * Circle center y

  92. * @ Param r2

  93. * Circle radius R

  94. * @ Return collision?

  95. */

  96. Public static Boolean isc2rcollision (INT X1, int Y1, int W1, int H1,

  97. Int X2, int Y2, int R2 ){

  98. If (math. Abs (x2-(X1 + W1/2)> W1/2 + R2)

  99. | Math. Abs (Y2-(Y1 + H1/2)> H1/2 + R2 ){

  100. Return false;

  101. }

  102. Return true;

  103. }


  104. /**

  105. * Multi-rectangle collision

  106. *

  107. * @ Param rarray1

  108. * @ Param rarray2

  109. * @ Return collision?

  110. */

  111. Public static Boolean isrectscollision (rect [] rarray1, rect [] rarray2 ){

  112. For (rect rt1: rarray1 ){

  113. For (rect rt2: rarray2 ){

  114. If (isrectcollision (rt1, rt2 )){

  115. Return true;

  116. }

  117. }

  118. }

  119. Return false;

  120. }


  121. }


Copy code

Http://www.eoeandroid.com/forum-863-1.html

Www.ogengine.com



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.