sphere - line intersection

Why doesnt this work ??? it doesnt detect intersections when the line goes above or below the cirlce

/*
 *returns true if the specified line segment intersects the specified circle
*/
public final boolean lineIntersectsCircle(
	float x, float y, float x1, float y1,	//line segment from (x, y) to (x1, y1)
	float a, float b, float r){				//circle at (a, b) with radius r
	
	//gradient of the line
	float m = (y-y1)/(x-x1);
	
	//find the part to be sqrted
	float sqrtedPart = r*r - ((m*(x-x1)+y1)-b)*((m*(x-x1)+y1)-b);
	
	//check for negativeness
	if(sqrtedPart < 0) return false;
	
	//work out the square root part 
	float sqrtPart = (float) Math.sqrt(sqrtedPart);
	
	//positive root answer
	float intersectX1 = sqrtPart + a;
	
	//neg root answer
	float intersectX2 = (-1*sqrtPart) + a;
	
	//sort the x values of the line segment so x < x1
	if(x > x1){
		float temp = x;
		x = x1;
		x1 = temp;
	}
	
	//check intersection is within line segment bound
	return	(intersectX1 >= x && intersectX1 < x1)
		 || (intersectX2 >= x && intersectX2 < x1);
}

I just dont know the maths you need without looking it up somewhere.
But I know that java.awt.geom has Line2d and Ellipse2d which support intersects() methods to check for
Intersection.

So it would make you

Line2D.Double line = new Line2D.Double(x,y,x1,y1);
Ellipse2D.Double ellipse = new Ellipse2D.Double(a-r,b-r,2r,2r);

return line.intersects(ellipse.getBounds());

Ellipse is created with defining a surrounding rectangle.

You could check the SourceCode of Ellipse and Line to see if you can optimize the speed of this, but in my experience, it wont be any faster than the code java classes have.
If performed very often, a mathematical soultion might be better.

There should be dozens of collision detection articles in the web, which tell you the maths or algorithms.

-JAW

What you want is :-

if(java.awt.geom.Line2D.ptSegDist(line.x1, line.y1, line.x2, line.y2, circle.x, circle.y) < circle.radius)
{
   //collision has occured
}

Ofcourse, if you need the point[s] of intersection the java2d geom classes are of absolutely no use what-so-ever.

Somebody should post a RFE asking for the functionality of the geom class to be expanded, to include alot more functionality, as they realy are barebones atm.

Thats because they’re Java2d classes - unless you’re drawing with them they are next to useless (they’re also rather slow for the primitive intersection testing that they do have).

What you really want is a proper, seperate set of raw geometry classes. Unfortunatly there doesn’t appear to be any existing libraries to do this. :’(

I would actually like to know how to do this rather than just calling some (slow - but thats not the point here) methods. I worked it out by solving the equations of a line and a circle simultaneously. I almost works but when one of the line ends goes above or below the cirlce it doesnt detect the intersection for some reason.

ty

hmm, I have some code lying around at work that will do what you want - I will have a look for it tomorrow.

Incidentally you could just look at the source code for the method I highlighted above.

Use a parametric representation of the line, i.e.


x = x0 + a t
y = y0 + b t

Then you can determine the parameter such that the distance to the center of the line is the smallest possible (this can be done by constructing the perpendicular line protruding from the center of the circle and intersecting our line at a right angle, or it could be done by differentiating the expression for the distance between point and line. The line and circle intersect if and only if the determined value of the parameter t lies within the bounds as specified by the terminating points of the line segment. Both things should just result in a certain formula.

You could, for example, parametrize your line like this:


r = r0 + t (r1 - r0),   0 < t < 1

where r0 and r1 are the terminating points of the line.

Okay some quick math gives the desired parameter value


                       -x0 dx + a dx - dy y0 + dy b
                  T := ----------------------------
                                  2     2
                                dx  + dy


Where dx and dy are the differences in x and y, respectively, of the terminating points of the line, (a,b) is the center of the circle, and x0, y0 is one terminating point of the line. I hope I haven’t made any typos while doing this. The stylish formula was made with Maple.