   Line - Sphere Intersection Code Not Working (VB6)
  Hi there I adapted the following piece of code from information I gathered off the internet. It appeared to work, until I started using it extensively. It returns a collision no matter where I point my line, unless I point it in the opposite direction (Eg. Negative Z axis). Here is the code below:Public Function Primitive_Intersect(R As Ray, ByRef Dist As Double) As IntegerDim V As New Vector3V.VAssignV VectorMath.VSubtract(R.Origin, Me.Origin)Dim B As DoubleB = VectorMath.VDotProduct(V, R.Direction) * -1Dim Det As DoubleDet = (B * B) - VectorMath.VDotProduct(V, V) + (Me.Radius * Me.Radius)Primitive_Intersect = RAY_MISS    If (Det > 0) Then   Det = Sqr(Det)   Dim i1 As Double   i1 = B - Det   Dim i2 As Double   i2 = B + Det  If (i2 > 0) Then   If (i1 < 0) Then   If (i2 < Dist) ThenDist = i2    Primitive_Intersect = RAY_INSIDE   End If   ElseIf (i1 < Dist) Then   Dist = i1Primitive_Intersect = RAY_HIT End IfEnd IfEnd IfEnd IfEnd FunctionNow, all my vector functions work. I have used the extensively as well, but with no problems. The values I am testing this function with are:the Ray:R.Direction = (-400, -300, 2)R.Origin = (0,0,0)Dist = 80The Sphere:Me.Origin = (0, 0, 5)Me.Radius = 3 Even if you think there is nothing wrong with it, please tell me so. I've been trying to get this to work for some time.   
  Quake3/*================RaySphereIntersections================*/int RaySphereIntersections( vec3_t origin, float radius, vec3_t point, vec3_t dir, vec3_t intersections[2] ) {float b, c, d, t;//| origin - (point + t * dir) | = radius//a = dir[0]^2 + dir[1]^2 + dir[2]^2;//b = 2 * (dir[0] * (point[0] - origin[0]) + dir[1] * (point[1] - origin[1]) + dir[2] * (point[2] - origin[2]));//c = (point[0] - origin[0])^2 + (point[1] - origin[1])^2 + (point[2] - origin[2])^2 - radius^2;// normalize dir so a = 1VectorNormalize(dir);b = 2 * (dir[0] * (point[0] - origin[0]) + dir[1] * (point[1] - origin[1]) + dir[2] * (point[2] - origin[2]));c = (point[0] - origin[0]) * (point[0] - origin[0]) +(point[1] - origin[1]) * (point[1] - origin[1]) +(point[2] - origin[2]) * (point[2] - origin[2]) -radius * radius;d = b * b - 4 * c;if (d > 0) {t = (- b + sqrt(d)) / 2;VectorMA(point, t, dir, intersections[0]);t = (- b - sqrt(d)) / 2;VectorMA(point, t, dir, intersections[1]);return 2;}else if (d == 0) {t = (- b ) / 2;VectorMA(point, t, dir, intersections[0]);return 1;}return 0;}