Collision_-_Get_Lines_Intersect.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Segmented line vs circle collision.
  2. #include "raylib.h"
  3. 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);
  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. // Create 4 vectors for 2 lines..
  12. Vector2 line1a=(Vector2){100,100};
  13. Vector2 line1b=(Vector2){200,200};
  14. Vector2 line2a;
  15. Vector2 line2b;
  16. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  17. //--------------------------------------------------------------------------------------
  18. // Main game loop
  19. while (!WindowShouldClose()) // Detect window close button or ESC key
  20. {
  21. // Update
  22. //----------------------------------------------------------------------------------
  23. // The second line is below the mouse pointer.
  24. line2a.x = GetMouseX();
  25. line2a.y = GetMouseY();
  26. line2b.x = line2a.x - 100;
  27. line2b.y = line2a.y + 100;
  28. //----------------------------------------------------------------------------------
  29. // Draw
  30. //----------------------------------------------------------------------------------
  31. BeginDrawing();
  32. ClearBackground(RAYWHITE);
  33. // Here we draw the first line.
  34. DrawLine(line1a.x,line1a.y,line1b.x,line1b.y,RED);
  35. // Here we draw the second line, the one below the mouse pointer.
  36. DrawLine(line2a.x,line2a.y,line2b.x,line2b.y,GREEN);
  37. // Here we check if the two lines on the screen touch each other.
  38. if(get_line_intersect(line1a.x,line1a.y,line1b.x,line1b.y,line2a.x,line2a.y,line2b.x,line2b.y)){
  39. DrawText("Collision between lines!!",0,0,40,RED); // Let the user know there has been a collision.
  40. }else{ // If no lines touch then show a message.
  41. DrawText("Move the mouse line on the other line!!",0,0,20,BLACK);
  42. }
  43. EndDrawing();
  44. //----------------------------------------------------------------------------------
  45. }
  46. // De-Initialization
  47. //--------------------------------------------------------------------------------------
  48. CloseWindow(); // Close window and OpenGL context
  49. //--------------------------------------------------------------------------------------
  50. return 0;
  51. }
  52. // This function was originally by andre la moth.
  53. 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){
  54. float s1_x;
  55. float s1_y;
  56. float s2_x;
  57. float s2_y;
  58. s1_x = p1_x - p0_x;
  59. s1_y = p1_y - p0_y;
  60. s2_x = p3_x - p2_x;
  61. s2_y = p3_y - p2_y;
  62. float s;
  63. float t;
  64. s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
  65. t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);
  66. if(s >= 0 && s <= 1 && t >= 0 && t <= 1) return true;
  67. return false; // No collision
  68. }