Skip to content

Instantly share code, notes, and snippets.

@jcayzac
Created September 26, 2011 08:09
Show Gist options
  • Save jcayzac/1241829 to your computer and use it in GitHub Desktop.
Save jcayzac/1241829 to your computer and use it in GitHub Desktop.
Sqrt-less sphere-cone intersection test
// V=(sphere.center - cone.apex_location)
// dotProduct_V_V = dotProduct(V, V)
// a = dotProduct(V, cone.direction_normal)
int cbloom_test(float dotProduct_V_V, float a, float sphere_radius, float cone_sin, float cone_cos) {
const float x(cone_cos*sqrt(dotProduct_V_V - a*a) - a*cone_sin);
return (fabsf(x)>=sphere_radius)?(x<0):-1;
}
int jcayzac_test(float dotProduct_V_V, float a, float sphere_radius, float cone_sin, float cone_cos) {
const float p(a*cone_sin);
const float q(cone_cos*cone_cos*dotProduct_V_V-a*a);
const bool tmp(q<0);
float lhs[2];
lhs[0]=(sphere_radius*sphere_radius - q);
lhs[1]=-lhs[0];
return (lhs[(p<sphere_radius) || !tmp]<2.f*sphere_radius*p)?-1:tmp;
}
@jcayzac
Copy link
Author

jcayzac commented Sep 26, 2011

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