• ### Announcements

#### Archived

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

# Cone Sphere intersection problem.

## Recommended Posts

DeepCover    122
EDIT::: I just figured it out, I got it working. See next message. I originally posted this in the math&physics section. I can't figure out what I'm doing wrong here. It's probbly something stupid, but my dot-product will always be positive and huge and the test always passes no matter what. Has anyone done this before, I've been spending way too much time on this one little thing...... 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)
{
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’
}
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 (using degrees). 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 = 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’
}
else
{
// center is inside K’’ and outside K’
return true;
}
*/
return true;
}
else
{
// center is outside K’’

return false;
}
}

[edited by - DeepCover on March 21, 2004 6:38:30 PM] [edited by - DeepCover on March 21, 2004 6:46:03 PM]

##### Share on other sites
DeepCover    122
The problem was that for my axis I was using my frustum center. But I fixed it by having the axis be:

Axis = frustumCenter - coneVertex;

works now.

##### Share on other sites
Doc    586
Good for you. Don''t cross post.