diff --git a/src/main/java/eu/jonahbauer/raytracing/shape/Sphere.java b/src/main/java/eu/jonahbauer/raytracing/shape/Sphere.java index d08e346..e105f8a 100644 --- a/src/main/java/eu/jonahbauer/raytracing/shape/Sphere.java +++ b/src/main/java/eu/jonahbauer/raytracing/shape/Sphere.java @@ -24,16 +24,16 @@ public record Sphere(@NotNull Vec3 center, double radius) implements Shape { var oc = ray.origin().minus(center()); var a = ray.direction().squared(); - var b = 2 * ray.direction().times(oc); + var h = ray.direction().times(oc); var c = oc.squared() - radius * radius; - var discriminant = b * b - 4 * a * c; + var discriminant = h * h - a * c; if (discriminant < 0) return Optional.empty(); var sd = Math.sqrt(discriminant); - double t = (- b - sd) / (2 * a); - if (t < 0) t = (-b + sd) / (2 * a); + double t = (- h - sd) / a; + if (t < 0) t = (- h + sd) / a; if (t < 0) return Optional.empty(); return Optional.of(new HitResult(t, ray.at(t).minus(center))); }