Skip to content

Instantly share code, notes, and snippets.

@jcayzac
Created September 26, 2011 08:31
Show Gist options
  • Save jcayzac/1241858 to your computer and use it in GitHub Desktop.
Save jcayzac/1241858 to your computer and use it in GitHub Desktop.
My algorithm for sphere-cone intersection
V = sphere.center - cone.apex_location
a = dotProduct(V, cone.direction_normal)
p = a * cone_sin
q = cone_cos * cone_cos * dotProduct(V, V) - a*a
r = q - sphere_radius * sphere_radius
if ((p < sphere_radius) || (q > 0)) {
if (r < 2 * sphere_radius * p)
return -1; // Sphere and cone enveloppes intersect
else if (q < 0)
return 1; // Sphere is totally included in cone
else
return 0; // Shere and cone don't intersect at all
}
else {
if ( -r < 2 * sphere_radius * p)
return -1; // Shpere and cone enveloppes intersect
else if (q < 0)
return 1; // Sphere is totally included in cone
else
return 0; // Sphere and cone don't intersect at all
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment