bool Sphere::intersectionRay(Vec3f rayFrom, Vec3f rayOrientation, IntersectionEvent* retIntersection) { rayFrom += .0001*rayOrientation; Vec3f OC = center - rayFrom; //assert(center[0]-rayFrom[0] == OC[0] && center[1]-rayFrom[1] == OC[1] && center[2]-rayFrom[2] == OC[2]); //double check = norm(OC); //assert(fabs(check-sqrt(OC[0]*OC[0]+OC[1]*OC[1]+OC[2]*OC[2]))<.001); if (norm(OC) >= radius) { //double d = norm(rayOrientation.cross(rayFrom-center)); double t_ca = rayOrientation.ddot(OC); if (t_ca <0) return false; //else //double t_hc_sqr = radius*radius - d*d; //or double t_hc_sqr2 = radius*radius - pow(norm(OC),2) + t_ca*t_ca; //if (fabs(t_hc_sqr-t_hc_sqr2) >.001) //{ // cout << "diff t_hc_sqr " << t_hc_sqr << ", " << t_hc_sqr2 << endl; // cout << "diff d " << d << ", " << (pow(norm(OC),2)-t_ca*t_ca) << endl; //} if (t_hc_sqr2 < 0) return false; //else double t = t_ca-sqrt(t_hc_sqr2); if (retIntersection != NULL) { retIntersection->point = rayFrom + t*rayOrientation; retIntersection->normal = (retIntersection->point-center)/radius; retIntersection->dist = t;//I think //assert( fabs(t - norm(retIntersection->point-rayFrom)) < .001); retIntersection->so = this; retIntersection->inside = false; //cout << "sph intersection at dist: " << t <<endl; } //cout << "d: " << d << endl; //cout << "t_ca: " << t_ca << endl; //cout << "t_hc_sqr2: " << t_hc_sqr2 << endl; return true; } else { double t_ca = rayOrientation.ddot(OC); double t_hc_sqr2 = radius*radius - pow(norm(OC),2) + t_ca*t_ca; if (t_hc_sqr2 < 0) return false; //else double t = t_ca+sqrt(t_hc_sqr2); if (retIntersection != NULL) { retIntersection->point = rayFrom + t*rayOrientation; retIntersection->normal = -1*(retIntersection->point-center)/radius; retIntersection->dist = t;//I think //assert( fabs(t - norm(retIntersection->point-rayFrom)) < .001); retIntersection->so = this; retIntersection->inside = true; //cout << "sph intersection at dist: " << t <<endl; } } return false; }