Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  sphere - line intersection  (Read 2555 times)
0 Members and 1 Guest are viewing this topic.
Offline g666

Junior Member





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

Why doesnt this work Huh 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);
   }

desperately seeking sanity
Offline JAW

Senior Member


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
Offline Anon666

Junior Member




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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« 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.  Cry

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

Junior Member





« 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
Offline Anon666

Junior Member




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.
Offline Ask_Hjorth_Larsen

Junior Member




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 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:
1  
r = r0 + t (r1 - r0),   0 < t < 1

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

Junior Member




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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!