Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

ray sphere intersection equation

bool hit_sphere(const vec3& center, float radius, const ray& r){
    vec3 oc = r.origin() - center;
    float a = dot(r.direction(), r.direction());
    float b = 2.0 * dot(oc, r.direction());
    float c = dot(oc,oc) - radius*radius;
    float discriminant = b*b - 4*a*c;
    return (discriminant>0);
}
Source by viclw17.github.io #
 
PREVIOUS NEXT
Tagged: #ray #sphere #intersection #equation
ADD COMMENT
Topic
Name
3+5 =