Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (622) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 sphere - line intersection  (Read 2913 times) 0 Members and 1 Guest are viewing this topic.
g666

Junior Devvie

 « Posted 2005-10-24 20:45:08 »

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

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);

float intersectX1 = sqrtPart + a;

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);
}

desperately seeking sanity
JAW

Senior Devvie

Medals: 2

 « Reply #1 - Posted 2005-10-25 10:23:42 »

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,2*r,2*r);

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
Anon666

Junior Devvie

aka Abuse/AbU5e/TehJumpingJawa

 « Reply #2 - Posted 2005-10-25 12:19:04 »

What you want is :-

 1  2  3  4 `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 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.
Orangy Tang

JGO Kernel

Medals: 57
Projects: 11

 « Reply #3 - Posted 2005-10-25 13:53:35 »

Ofcourse, if you need the point 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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
g666

Junior Devvie

 « Reply #4 - Posted 2005-10-25 20:08:05 »

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

desperately seeking sanity
Anon666

Junior Devvie

aka Abuse/AbU5e/TehJumpingJawa

 « Reply #5 - Posted 2005-10-25 22:21:20 »

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.

Junior Devvie

Java games rock!

 « Reply #6 - Posted 2005-10-30 03:46:10 »

Use a parametric representation of the line, i.e.
 1  2 `x = x0 + a ty = 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:
 1 `r = r0 + t (r1 - r0),   0 < t < 1`

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

Junior Devvie

Java games rock!

 « Reply #7 - Posted 2005-10-30 03:55:11 »

Okay some quick math gives the desired parameter value
 1  2  3  4 `                       -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.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 BurntPizza (27 views) 2015-04-23 03:42:11 theagentd (30 views) 2015-04-22 16:23:07 Riven (44 views) 2015-04-16 10:48:47 Duke0200 (52 views) 2015-04-16 01:59:01 Fairy Tailz (38 views) 2015-04-14 20:13:12 Riven (41 views) 2015-04-12 21:36:37 bus hotdog (58 views) 2015-04-10 02:39:32 CopyableCougar4 (61 views) 2015-04-10 00:51:04 BurntPizza (62 views) 2015-04-06 22:06:58 ags1 (62 views) 2015-04-02 10:58:48
 theagentd 27x BurntPizza 16x wessles 15x 65K 11x Rayvolution 11x alwex 11x kingroka123 11x KevinWorkman 9x kevglass 8x phu004 8x Roquen 7x chrislo27 7x Hanksha 7x Riven 7x Olo 7x ra4king 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org