Original Post
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: Now, 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 = 80 The 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.
Public Function Primitive_Intersect(R As Ray, ByRef Dist As Double) As Integer
Dim V As New Vector3
V.VAssignV VectorMath.VSubtract(R.Origin, Me.Origin)
Dim B As Double
B = VectorMath.VDotProduct(V, R.Direction) * -1
Dim Det As Double
Det = (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) Then
Dist = i2
Primitive_Intersect = RAY_INSIDE
End If
Else
If (i1 < Dist) Then
Dist = i1
Primitive_Intersect = RAY_HIT
End If
End If
End If
End If
End Function