Collision_-_PolyvsPoly_Collide.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "raylib.h"
  2. bool get_line_intersect(float p0_x, float p0_y, float p1_x, float p1_y, float p2_x ,float p2_y, float p3_x, float p3_y);
  3. bool polypolycollide(Vector2 poly1[],int as1, Vector2 poly2[], int as2);
  4. int debug=0;
  5. int main(void)
  6. {
  7. // Initialization
  8. //--------------------------------------------------------------------------------------
  9. const int screenWidth = 800;
  10. const int screenHeight = 450;
  11. InitWindow(screenWidth, screenHeight, "raylib example.");
  12. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  13. //--------------------------------------------------------------------------------------
  14. // Main game loop
  15. while (!WindowShouldClose()) // Detect window close button or ESC key
  16. {
  17. // Update
  18. //----------------------------------------------------------------------------------
  19. //----------------------------------------------------------------------------------
  20. // Draw
  21. //----------------------------------------------------------------------------------
  22. BeginDrawing();
  23. ClearBackground(RAYWHITE);
  24. //
  25. // Create a Vector2 array and create some points in it
  26. //
  27. Vector2 pol1[3];
  28. pol1[0] = (Vector2){200,100};
  29. pol1[1] = (Vector2){20,300};
  30. pol1[2] = (Vector2){280,300};
  31. Vector2 pol2[3];
  32. pol2[0] = (Vector2){50+GetMouseX()-50,50+GetMouseY()-50};
  33. pol2[1] = (Vector2){20+GetMouseX()-50,150+GetMouseY()-50};
  34. pol2[2] = (Vector2){90+GetMouseX()-50,150+GetMouseY()-50};
  35. if(polypolycollide( pol1,sizeof(pol1)/sizeof(pol1[0]),
  36. pol2,sizeof(pol2)/sizeof(pol2[0]))==true){
  37. DrawText(FormatText("Collision"),0,0,20,RED);
  38. }
  39. DrawText(FormatText("%i",debug),0,20,20,RED);
  40. DrawTriangle(pol1[0],pol1[1],pol1[2],RED);
  41. DrawTriangle(pol2[0],pol2[1],pol2[2],YELLOW);
  42. EndDrawing();
  43. //----------------------------------------------------------------------------------
  44. }
  45. // De-Initialization
  46. //--------------------------------------------------------------------------------------
  47. CloseWindow(); // Close window and OpenGL context
  48. //--------------------------------------------------------------------------------------
  49. return 0;
  50. }
  51. bool polypolycollide(Vector2 poly1[],int as1, Vector2 poly2[], int as2){
  52. //'first check if the second polygon is inside the first polygon
  53. int x=-5000;
  54. int y=-5000;
  55. int cnt=0;
  56. for(int i=1;i<as1;i++){
  57. if(get_line_intersect( x,
  58. y,
  59. poly2[0].x,
  60. poly2[0].y,
  61. poly1[i-1].x,
  62. poly1[i-1].y,
  63. poly1[i].x,
  64. poly1[i].y))cnt++;
  65. }
  66. if(get_line_intersect( x,y,
  67. poly2[0].x,poly2[0].y,
  68. poly1[as1-1].x,poly1[as1-1].y,
  69. poly1[0].x,poly1[0].y))cnt++;
  70. if( (cnt % 2) > 0 )return true;
  71. //'Now check if the first polygon is inside the second polygon
  72. x=-5000;
  73. y=-5000;
  74. cnt=0;
  75. for(int i=1;i<as2;i++){
  76. if(get_line_intersect( x,y,
  77. poly1[0].x,poly1[0].y,
  78. poly2[i-1].x,poly2[i-1].y,
  79. poly2[i].x,poly2[i].y))cnt++;
  80. }
  81. if(get_line_intersect( x,y,
  82. poly1[0].x,poly1[0].y,
  83. poly2[as2-1].x,
  84. poly2[as2-1].y,
  85. poly2[0].x,poly2[0].y))cnt++;
  86. //'
  87. if((cnt % 2) > 0)return true;
  88. //'
  89. //' Now check if any of the lines of the two polygons touch
  90. for(int i=1;i<as1;i++){
  91. for(int j=1;j<as2;j++){
  92. if(get_line_intersect( poly2[j-1].x,poly2[j-1].y,
  93. poly2[j].x,poly2[j].y,
  94. poly1[i-1].x,poly1[i-1].y,
  95. poly1[i].x,poly1[i].y))return true;
  96. }}
  97. for(int j=1;j<as2;j++){
  98. if(get_line_intersect( poly2[j-1].x,poly2[j-1].y,
  99. poly2[j].x,poly2[j].y,
  100. poly1[0].x,poly1[0].y,
  101. poly1[as1-1].x,poly1[as1-1].y))return true;
  102. }
  103. for(int j=1;j<as1;j++){
  104. if(get_line_intersect( poly1[j-1].x,poly1[j-1].y,
  105. poly1[j].x,poly1[j].y,
  106. poly2[0].x,poly2[0].y,
  107. poly2[as2-1].x,poly2[as2-1].y))return true;
  108. }
  109. //'There was no Collision between the two polygons ___ Return
  110. return false;
  111. }
  112. // This function was originally by andre la moth.
  113. bool get_line_intersect(float p0_x, float p0_y, float p1_x, float p1_y, float p2_x ,float p2_y, float p3_x, float p3_y){
  114. float s1_x;
  115. float s1_y;
  116. float s2_x;
  117. float s2_y;
  118. s1_x = p1_x - p0_x;
  119. s1_y = p1_y - p0_y;
  120. s2_x = p3_x - p2_x;
  121. s2_y = p3_y - p2_y;
  122. float s;
  123. float t;
  124. s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
  125. t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);
  126. if(s >= 0 && s <= 1 && t >= 0 && t <= 1) return true;
  127. return false; // No collision
  128. }