Collision_-_LinesegCircleCollide.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "raylib.h"
  2. #include <math.h>
  3. static bool linecirclecollide(int sx1, int sy1, int sx2, int sy2, int cx, int cy, float cr);
  4. int main(void)
  5. {
  6. // Initialization
  7. //--------------------------------------------------------------------------------------
  8. const int screenWidth = 800;
  9. const int screenHeight = 450;
  10. InitWindow(screenWidth, screenHeight, "raylib example.");
  11. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  12. //--------------------------------------------------------------------------------------
  13. // Main game loop
  14. while (!WindowShouldClose()) // Detect window close button or ESC key
  15. {
  16. // Update
  17. //----------------------------------------------------------------------------------
  18. Vector2 linea=(Vector2){GetMouseX(),GetMouseY()};
  19. Vector2 lineb=(Vector2){linea.x-100,linea.y+50};
  20. Vector2 circle=(Vector2){400,200};
  21. float circleradius = 50;
  22. //----------------------------------------------------------------------------------
  23. // Draw
  24. //----------------------------------------------------------------------------------
  25. BeginDrawing();
  26. ClearBackground(RAYWHITE);
  27. DrawLine(linea.x,linea.y,lineb.x,lineb.y,RED);
  28. DrawCircle(circle.x,circle.y,circleradius,YELLOW);
  29. if(linecirclecollide(linea.x,linea.y,lineb.x,lineb.y,circle.x,circle.y,circleradius))
  30. DrawText("Something is a foot..",10,10,30,BLUE);
  31. EndDrawing();
  32. //----------------------------------------------------------------------------------
  33. }
  34. // De-Initialization
  35. //--------------------------------------------------------------------------------------
  36. CloseWindow(); // Close window and OpenGL context
  37. //--------------------------------------------------------------------------------------
  38. return 0;
  39. }
  40. //'
  41. //' Line(segment) to Circle Collision//
  42. //'
  43. bool linecirclecollide(int x1, int y1, int x2, int y2, int xc, int yc, float r){
  44. float xd = 0.0f;
  45. float yd = 0.0f;
  46. float t = 0.0f;
  47. float d = 0.0f;
  48. float dx = 0.0f;
  49. float dy = 0.0f;
  50. dx = x2 - x1;
  51. dy = y2 - y1;
  52. t = ((yc - y1) * dy + (xc - x1) * dx) / (dy * dy + dx * dx);
  53. if(0 <= t && t <= 1)
  54. {
  55. xd = x1 + t * dx;
  56. yd = y1 + t * dy;
  57. d = sqrt((xd - xc) * (xd - xc) + (yd - yc) * (yd - yc));
  58. return d <= r;
  59. }
  60. else
  61. {
  62. d = sqrt((xc - x1) * (xc - x1) + (yc - y1) * (yc - y1));
  63. if(d <= r)
  64. {
  65. return true;
  66. }
  67. else
  68. {
  69. d = sqrt((xc - x2) * (xc - x2) + (yc - y2) * (yc - y2));
  70. if(d <= r)
  71. {
  72. return true;
  73. }
  74. else
  75. {
  76. return false;
  77. }
  78. }
  79. }
  80. }