   Ray intersection for spheres
  Hi all!I built in sprites into my engine (Planes for holding pictures of Actors, just like in Unreal) and now I want them to become selectable. Since that are planes which always face you, I would like to put a bounding sphere around it. But how do I calculate if if was hit? I have the origin and the direction of the ray to cast..   
  i supose that the sphere is at the origin with the center.bool Sphere::GetIntersection(const Ray& r, IntersectionData& i)const{mt::vec3drayToSphereCenter = r.x0;rayToSphereCenter.Flip();doublelengthRTSC2 = rayToSphereCenter * rayToSphereCenter;if (lengthRTSC2 < sqradius)return false;doubleclosestApproach = rayToSphereCenter * r.dx;if (closestApproach < 0.0 && lengthRTSC2 > sqradius)return false;doublehalfCord2 = (sqradius)- lengthRTSC2 + (closestApproach * closestApproach);if (halfCord2 < 0.0)return false;if (lengthRTSC2 > sqradius)lengthRTSC2 = closestApproach - sqrt(halfCord2);elselengthRTSC2 = closestApproach + sqrt(halfCord2);if (i.InvalidPoint(lengthRTSC2))return false;i.SetIntersectionLength(lengthRTSC2);i._normal.x = (r.x0.x + r.dx.x * i.GetFarDistance()) / radius;i._normal.y = (r.x0.y + r.dx.y * i.GetFarDistance()) / radius;i._normal.z = (r.x0.z + r.dx.z * i.GetFarDistance()) / radius;return true;};  Thanks... I don't get what some functions in that code do:mt::vec3drayToSphereCenter = r.x0; <- What is r.x0?rayToSphereCenter.Flip(); <- 1,1,1 becomes -1,-1,-1?doubleclosestApproach = rayToSphereCenter * r.dx; <- What is r.dx?if (i.InvalidPoint(lengthRTSC2)) <- What is "Invalid Point"? i.SetIntersectionLength(lengthRTSC2); <- What is that?i._normal.x = (r.x0.x + r.dx.x * i.GetFarDistance()) / radius; <- What is GetFarDistance()?Sorry :P;  Here is some C# code i wrote for my raytracer which is a little more generalIt supposes that ray.Direction is a non-normalized direction vector for the ray. (i.e. the end position would be r.Position + r.Direction)normal is only the normal for the sphere at the intersection point and can be omitted. public override bool Intersect(Ray r, ref Vector intersection, ref Vector normal) {   double t = 0;   // Move the ray relative to include the sphere's position   Vector rp = r.Position - position;   //Compute a, b and c coefficients  double a = Vector.Dot(r.Direction, r.Direction);  double b = 2 * Vector.Dot(r.Direction, rp);  double c = Vector.Dot(rp, rp) - radius * radius;  //Find discriminant  double disc = b * b - 4 * a * c;   // Cannot divide by zero, ray misses sphere  if (disc < 0) return false;  double discsq = Math.Sqrt(disc);  double q = b < 0 ? (-b - discsq) / 2 : (-b + discsq) / 2;  double t0 = q / a;  double t1 = c / q;   // Swap values if t0 is greater than t1  if (t0 > t1)  { double temp = t0; t0 = t1; t1 = temp;  }  // if t1 is less than zero, the object is in the ray's negative direction  // and consequently the ray misses the sphere  if (t1 < 0) return false;  // if t0 is less than zero, the intersection point is at t1   t = (t0 < 0 ? t1 : t0);   intersection = r.Position + r.Direction * t;   // Calculate surface normal   normal = (intersection - position).Normalize();   return true;   };  ok sorry for not explaining....rayToSphereCenter.Flip(); <- 1,1,1 becomes -1,-1,-1? <- truer.x0 is a vector (x,y,z) with the pivot of the ray,r.dx is a normalized vector (x,y,z) with the direction of the ray.Here is the updated codebool Sphere::GetIntersection(const Ray& r, IntersectionData& i)const{mt::vec3drayToSphereCenter = r.x0;rayToSphereCenter.Flip();doublelengthRTSC2 = rayToSphereCenter * rayToSphereCenter;if (lengthRTSC2 < sqradius)return false;doubleclosestApproach = rayToSphereCenter * r.dx;if (closestApproach < 0.0 && lengthRTSC2 > sqradius)return false;doublehalfCord2 = (sqradius)- lengthRTSC2 + (closestApproach * closestApproach);if (halfCord2 < 0.0)return false;if (lengthRTSC2 > sqradius)lengthRTSC2 = closestApproach - sqrt(halfCord2);elselengthRTSC2 = closestApproach + sqrt(halfCord2);i._distance = lengthRTSC2;//distance to the intersection pointi._normal.x = (r.x0.x + r.dx.x * i._distance) / radius;i._normal.y = (r.x0.y + r.dx.y * i._distance) / radius;i._normal.z = (r.x0.z + r.dx.z * i._distance) / radius;return true;};  Great, thank you both. :)   