# Cone-Sphere collision

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

## Recommended Posts

I am working on a problem to test detect if a cone (described by an apex, angle theta and axis) and a sphere (defined by a sphere centre and a radius) intersect. The concept seems like a riddle but an almost complete description can be found here which i have used: a pdf showing the principals then the .cpp code will make more sense Scroll down the page to the section: Intersection of spheres and cones (3D) I get the error: warning C4715: not all control paths return a value But the inner inner if statement reveals little. What does the: " return sqrlen <= radsqr; " line actually do ???? This is supposed to be a bool? If the sphere does intersect with the cone, i should get a boolean true value (from the second 'if' statement, which i could then pass to another function. Any advice? Thanks [Edited by - chrome68 on November 12, 2008 3:53:53 AM]

##### Share on other sites
you get this error because the compiler thinks it's possible for your function to not return a value. Ie it sees your function as something like..

bool ReturnSomething(int x){ if(x == 1){return true;}}

(obviously if x isn't one then this function won't return any values!)

As far as I can see it's ok, but try adding a return false as the very last line of the function (also step through it carefully with a debugger to make sure you're not hitting that line). The only bit of code that looks dodgy is the return sqrlen <= radsqr;

##### Share on other sites
I've set up a simple example of a cube (1cm x 1cm) centred at (0,0,0) with the sphere centred there too. You can literally draw the outcome if its going to "Intersect" or "Miss" given v, axis and theta (say 20 degrees) like below. The code below doesn't quite give me the same results. Which makes me think that there is something wrong with the math.

I've simplified the code slightly because i've realised that the second if statement is not applicable to what i'm doing (the sphere centre would never be in the region behind the cone apex).

bool coneIntersection() {	spherecentre = (0,0,0);	sphereradius = 0.2000; //centimetres			Vector3D v = (0.5,0.5,0.5); //a corner of the cube 		Vector3D axis = (0.0,-1.0,0.0); //-ve y direction		double angle = 30.0;			double theta = ((angle*3.14159)/180); 		double invsin = (1.0f/sin(theta));		double cossqr = cos(theta)*cos(theta);		Vector3D D = spherecentre - v;		Vector3D U = D + axis*(sphereradius*invsin);		double sqrlen = U.length();		double E = U.dotProduct(axis);		if(E > 0.0f && E*E >= sqrlen*cossqr) 		{			//centre is inside K" 			                        return true; //also cout << Intersection		       		}		else		{			//centre is outside K"                                                return false;  //also cout << Miss		}				return false;}

I've tried this for different values sphereradius and larger theta angles, unconvincingly.
Can anyone try the code out or if they understand simple math principles offer an explanation??

[Edited by - chrome68 on November 7, 2008 9:09:44 AM]

1. 1
2. 2
Rutin
18
3. 3
khawk
15
4. 4
A4L
14
5. 5

• 10
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633745
• Total Posts
3013667
×