Created
August 24, 2016 00:09
-
-
Save BrandonDyer64/c8537f61cfd5fdbb975f4831776b458f to your computer and use it in GitHub Desktop.
This file contains 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 | |
} |
public static float dist(float LineStartX, float LineStartY, float LineEndX, float LineEndY)
{
return (float) Math.sqrt((LineEndX - LineStartX) * (LineEndX - LineStartX) + (LineEndY - LineStartY) * (LineEndY - LineStartY));
}
public static float getRayCast(float RayStartX, float RayStartY, float RayCosineDist, float RaySineDist, float LineStartX, float LineStartY, float LineEndX, float LineEndY) {
float RayEndX, RayEndY, LineXDiff, LineYDiff;
RayEndX = RayCosineDist - RayStartX;
RayEndY = RaySineDist - RayStartY;
LineXDiff = LineEndX - LineStartX;
LineYDiff = LineEndY - LineStartY;
float s, t;
s = (-RayEndY * (RayStartX - LineStartX) + RayEndX * (RayStartY - LineStartY)) / (-LineXDiff * RayEndY + RayEndX * LineYDiff);
t = (LineXDiff * (RayStartY - LineStartY) - LineYDiff * (RayStartX - LineStartX)) / (-LineXDiff * RayEndY + RayEndX * LineYDiff);
if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
// Collision detected
float x = RayStartX + (t * RayEndX);
float y = RayStartY + (t * RayEndY);
return dist(RayStartX, RayStartY, x, y);
}
return -1; // No collision
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you describe the arguments a little bit more clear?