src/scene.cc
branchpyrit
changeset 56 d4481fc43952
parent 53 228cb8bfdd54
child 69 303583d2fb97
--- a/src/scene.cc	Sat Mar 29 12:09:50 2008 +0100
+++ b/src/scene.cc	Sat Mar 29 17:54:27 2008 +0100
@@ -114,13 +114,24 @@
 bool Sphere::intersect(const Ray &ray, Float &dist) const
 {
 	Vector3 V = ray.o - center;
-	register Float d = dot(V, ray.dir);
+	register Float d = -dot(V, ray.dir);
 	register Float Det = d * d - (dot(V,V) - sqr_radius);
+	register Float t1,t2;
 	if (Det > 0) {
-		d = -d - sqrtf(Det);
-		if (d > 0 && d < dist)
+		Det = sqrtf(Det);
+		t1 = d - Det;
+		t2 = d + Det;
+		if (t1 > 0)
 		{
-			dist = d;
+			if (t1 < dist)
+			{
+				dist = t1;
+				return true;
+			}
+		}
+		else if (t2 > 0 && t2 < dist)
+		{
+			dist = t2;
 			return true;
 		}
 	}