From 03863d7a713c845ff23b750268ddd105b46fdc93 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 3 Jan 2025 14:13:33 +0100 Subject: [PATCH] Detect inside of sphere The sphere detection only ever considered the "closer" intersection point. This only works for ray that originate outside the sphere. --- src/scene.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/scene.c b/src/scene.c index 38e2203..290b404 100644 --- a/src/scene.c +++ b/src/scene.c @@ -172,19 +172,19 @@ double get_intersection_arg_cylinder double get_intersection_arg_sphere(const t_ray *ray, const t_sphere *sphere) { t_vec3 start; - double a; - double b; - double c; - double det; + t_pair intersection_args; start = vec_diff(ray->start, sphere->center); - a = vec_scalar_mul(ray->direction, ray->direction); - b = 2 * vec_scalar_mul(ray->direction, start); - c = vec_scalar_mul(start, start) - sphere->radius * sphere->radius; - det = b * b - 4 * a * c; - if (det < 0) + intersection_args = solve_quadratic( + vec_scalar_mul(ray->direction, ray->direction), + 2 * vec_scalar_mul(ray->direction, start), + vec_scalar_mul(start, start) - sphere->radius * sphere->radius); + if (intersection_args.first > 0) + return (intersection_args.first); + else if (intersection_args.second > 0) + return (intersection_args.second); + else return (NAN); - return ((-b - sqrt(det)) / (2 * a)); } double get_intersection_arg_bounded(const t_ray *ray, const t_object *object) -- 2.30.2