Skip to content

Instantly share code, notes, and snippets.

@tuket
Created July 10, 2020 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuket/7f604c2ea16f62af5a0d4778c80ac8c9 to your computer and use it in GitHub Desktop.
Save tuket/7f604c2ea16f62af5a0d4778c80ac8c9 to your computer and use it in GitHub Desktop.
ray_vs_circle
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;
}
@tuket
Copy link
Author

tuket commented Jul 10, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment