Archived

This topic is now archived and is closed to further replies.

DeepCover

Cone-Sphere intersection problem

Recommended Posts

DeepCover    122
Edit:: I just figured it out, I got it working. See next message. I got this psuedocode from magic software (http://www.magic-software.com/Documentation/IntersectionSphereCone.pdf), I'm trying to implement it:
bool SphereIntersectsCone (Sphere S, Cone K)
{
   U = K.vertex - (Sphere.radius/K.sin)*K.axis;
   D = S.center - U;
   if ( Dot(K.axis,D) >= Length(D)*K.cos )
   {
      // center is inside K’’
      D = S.center - K.vertex;
      if ( -Dot(K.axis,D) >= Length(D)*K.sin )
      {
         // center is inside K’’ and inside K’
         return Length(D) <= S.radius;
      }
      else
      {
         // center is inside K’’ and outside K’
         return true;
      }
   }
   else
   {
       // center is outside K’’
       return false;
   }
}
       
here's what I have so far, don't mind the unoptimized code. I'm just trying to get this thing to work. I'm first trying to get a portion of it to work as you see I Thave the center commented out, but I'm getting weired results. he first dot-product I take is rediculously large and will always be larger than what it is compared to in the first if-statement. The result is that every single test passes. Does it look like I'm coding the psuedocode out correctly? I use the center of my frustum as the axis. I have a field of view of 60, so I'm using 30 degrees in my cos/sin calculations. the vertex of the cone is where the camera is.
 

//my dot product function

float Vertex::dot(Vertex V)
{
	return ((this->x * V.x) + (this->y * V.y) + (this->z * V.z));
}


bool coneSphereIntersect()
{
	Vector U, D;
	float len;

	//U = K.vertex - (Sphere.radius/K.sin)*K.axis

	U = coneVertex - (sphereRadius/sin) * viewAxis.normalize(); ;

	//D = S.center - U;

	D = sphereCenter - U;

	//the length of the vector D

	len = sqrt((D.x * D.x) +
	           (D.y * D.y) +
		   (D.z * D.z));


	//if ( Dot(K.axis,D) >= Length(D)*K.cos )

	if(viewAxis.dot(D) >= (len* cos))
	{
		// center is inside K’’

		/*
		D = S.center - K.vertex;
		if ( -Dot(K.axis,D) >= Length(D)*K.sin )
		{
			// center is inside K’’ and inside K’
			return Length(D) <= S.radius;
		}
		else
		{
			// center is inside K’’ and outside K’
			return true;
		}
		*/
		return true;
	}
	else
	{
		// center is outside K’’

		return false;
	}
}
[edited by - DeepCover on March 20, 2004 10:44:17 PM] [edited by - DeepCover on March 21, 2004 5:16:29 PM] [edited by - DeepCover on March 21, 2004 5:17:17 PM] [edited by - DeepCover on March 21, 2004 5:18:56 PM] [edited by - DeepCover on March 21, 2004 5:58:23 PM] [edited by - DeepCover on March 21, 2004 5:59:00 PM] [edited by - DeepCover on March 21, 2004 6:44:07 PM]

Share this post


Link to post
Share on other sites