-
Notifications
You must be signed in to change notification settings - Fork 7
/
Collision_-_Get_Lines_Intersect.c
92 lines (68 loc) · 3.22 KB
/
Collision_-_Get_Lines_Intersect.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Segmented line vs circle collision.
#include "raylib.h"
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);
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib example.");
// Create 4 vectors for 2 lines..
Vector2 line1a=(Vector2){100,100};
Vector2 line1b=(Vector2){200,200};
Vector2 line2a;
Vector2 line2b;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// The second line is below the mouse pointer.
line2a.x = GetMouseX();
line2a.y = GetMouseY();
line2b.x = line2a.x - 100;
line2b.y = line2a.y + 100;
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// Here we draw the first line.
DrawLine(line1a.x,line1a.y,line1b.x,line1b.y,RED);
// Here we draw the second line, the one below the mouse pointer.
DrawLine(line2a.x,line2a.y,line2b.x,line2b.y,GREEN);
// Here we check if the two lines on the screen touch each other.
if(get_line_intersect(line1a.x,line1a.y,line1b.x,line1b.y,line2a.x,line2a.y,line2b.x,line2b.y)){
DrawText("Collision between lines!!",0,0,40,RED); // Let the user know there has been a collision.
}else{ // If no lines touch then show a message.
DrawText("Move the mouse line on the other line!!",0,0,20,BLACK);
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
// This function was originally by andre la moth.
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){
float s1_x;
float s1_y;
float s2_x;
float s2_y;
s1_x = p1_x - p0_x;
s1_y = p1_y - p0_y;
s2_x = p3_x - p2_x;
s2_y = p3_y - p2_y;
float s;
float t;
s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);
if(s >= 0 && s <= 1 && t >= 0 && t <= 1) return true;
return false; // No collision
}