-
-
Save Oisann/5d78b9a3d4db357a30f2e83d9b8fdff0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static float dist(float x1, float y1, float x2, float y2) { | |
| return (float) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); | |
| } | |
| public static float getRayCast(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, s1_y, s2_x, 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, 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) { | |
| // Collision detected | |
| float x = p0_x + (t * s1_x); | |
| float y = p0_y + (t * s1_y); | |
| return dist(p0_x, p0_y, x, y); | |
| } | |
| return -1; // No collision | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment