#### Archived

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

# Cone-Sphere intersection problem

This topic is 5233 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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)
{
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. 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 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 on other sites
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
Good for you. Don''t cross post.

1. 1
Rutin
26
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002135
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!