Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Line - Sphere Intersection Code Not Working (VB6)

Started by Kustaz May 27, 2006 at 9:57 PM 0 replies 500+ views
Original Post
Kustaz
Kustaz
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 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



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.
tthibault
tthibault
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 = 1	VectorNormalize(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;}

...and do not wildly extrapolate. Just because Saddam Hussein gassed Kurds in 1990 doesn't mean he eats babies' brains.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.