Created
July 10, 2020 22:09
-
-
Save tuket/7f604c2ea16f62af5a0d4778c80ac8c9 to your computer and use it in GitHub Desktop.
ray_vs_circle
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
bool rayVsCircle(float& depth, | |
glm::vec2 rayOrig, glm::vec2 rayDir, | |
glm::vec2 circlePos, float circleRad) | |
{ | |
const float R = circleRad; | |
const glm::vec2 op = circlePos - rayOrig; | |
if(dot(op, op) < R*R) { // is rayOrigin inside the circle ? | |
depth = 0; | |
return true; | |
} | |
const float D = dot(rayDir, op); | |
const float H2 = dot(op, op) - D*D; | |
const float K2 = R*R - H2; | |
if(K2 >= 0) { | |
depth = D - sqrt(K2); | |
if(depth > 0) | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/tuket/demo_ray_vs_circle/blob/master/README.md