Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (612)
Games in Android Showcase (172)
games submitted by our members
Games in WIP (658)
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  
  Collision between circle and line  (Read 3396 times)
0 Members and 1 Guest are viewing this topic.
Offline Aaron

Junior Newbie

Java games rock!

« Posted 2005-06-04 01:31:12 »

I'm working on a collision method that detects at what time a moving circle makes contact with a line over the interval [0, 1] (it returns 1.0 if there's no collision).  I'm able to determine whether or not the circle is moving toward the line and if the circle's velocity could ever move itself close enough to the line for there to be collision.  However, I can't figure out how to find the distance the circle would have to go to hit the line, which I'd get my time from.

Here's pseudo code for the part of my method that works.  I assume the line is infinite:


double calculateCollisionTime(Line2D line, Circle circle) {

// Get unit vector of line's normal pointing to side of line circle is on,
// as well as shortest distance between line and circle's centre coordinate.

if (circle.velocityMagnitude() < circle_line_distance - circle.radius())
   return 1.0;

// Get unit vector of circle's velocity.
// Get dot product of circle's unit velocity vector and line's unit normal vector.
// This corresponds to the angle between the circle's velocity and the line.

if (dotprod <= 0) // Circle parallel to or going away from line.
   return 1.0;

// This is the part I'm having trouble with.
// Find distance between circle and line parallel to circle's velocity.  If it's less
// than the circle's velocity, expect a collision.  Divide this distance by the circle's
// velocity to get time.

return time.



I have searched on the internet, and the few articles I found didn't help me.  Does anyone here have any suggestions?

Thank you.
Offline c_lilian

Senior Devvie

Projects: 1

Java games will probably rock someday...

« Reply #1 - Posted 2005-06-04 06:48:30 »

Here is a usefull resource :

a "simple" method to find the intersection is :
 - find the point on the circle closest to the line :
     * find the perpendicular projection of the center of the circle on the line
     * draw a line from the center to this point
     * find the point on this line at radius distance from the center.
 - find the intersection of a line going through this point and whith the same direction as the circle speed (line - line intersection : easy)
Hope it helps


Offline Aaron

Junior Newbie

Java games rock!

« Reply #2 - Posted 2005-06-04 14:33:21 »

Thanks for the site, Lilian.

I've been looking at the "distance from a point to a line" part with interest, but what do they mean by "L**2"?  Is that L squared?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline c_lilian

Senior Devvie

Projects: 1

Java games will probably rock someday...

« Reply #3 - Posted 2005-06-04 15:41:39 »


Offline Aaron

Junior Newbie

Java games rock!

« Reply #4 - Posted 2005-06-04 21:49:40 »


Anyway, that link you gave me helped me optimize some of my code, but I'm still having problems.

What I'm doing now is look for the possible intersection point on the circle's radius, which is where the line normal would cross if reaching for the circle's centre.  I came up with two ways of calculating the distance between the line and this intersection point parallel to the circle's velocity; one from the web site you gave me Lilian and another site at

First I get the intersection point:
// Point of possible intersection on ball.
double intsect1X = A.x() + (-Nx * A.radius());
double intsect1Y = A.y() + (-Ny * A.radius());

Getting the distance based on the second site:
double d = (line.getX1() * Nx) + (line.getY1() * Ny);
double actDist = d - ((intsect1X * Nx) + (intsect1Y * Ny));

Getting the distance based on the first site:
double intsect2X = A.x() + (-Nx * A.radius()) + A.dx();
double intsect2Y = A.y() + (-Ny * A.radius()) + A.dy();

// Find distance between intersection point on ball and line parallel to ball velocity.
double denom = ((line.getX2() - line.getX1()) * (intsect2Y -  intsect1Y)) - ((line.getY2() - line.getY1()) * (intsect2X - intsect1X));
if (denom == 0) // Lines are parallel.
    return 1.0;
double r2 = (((line.getY1() - intsect1Y) * (intsect2X - intsect1X)) - ((line.getX1() - intsect1X) * (intsect2Y - intsect1Y))) / denom;
// Z is intersection point.
double Zx = line.getX1() + (r2 * (line.getX2() - line.getX1())) - intsect1X;
double Zy = line.getY1() + (r2 * (line.getY2() - line.getY1())) - intsect1Y;
// Distance ball actually has to move.
double actDist = Math.sqrt((Zx * Zx) + (Zy * Zy));

Both of these are followed by this:
if (dV <= Math.abs(actDist))
    return 1.0;
return Math.abs(actDist) / dV;

The problem is first of all that it looks like my line is "one-sided," the ball is only blocked from one side.  It can still pass through the other side.  Even on the side where it's blocked, it seems to be still possible for the ball to pass through if I press the forward key long enough (I'm controlling the ball with the keyboard).
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Roquen (13 views)
2015-08-29 11:30:54

GamerC4 (23 views)
2015-08-22 20:38:50

GamerC4 (22 views)
2015-08-22 20:37:18

GamerC4 (26 views)
2015-08-22 20:37:01

Kefwar (30 views)
2015-08-22 18:07:24

GamerC4 (24 views)
2015-08-22 01:00:24

GamerC4 (37 views)
2015-08-22 01:00:17

GamerC4 (24 views)
2015-08-22 00:57:35

GamerC4 (25 views)
2015-08-22 00:56:59

BurntPizza (30 views)
2015-08-21 01:38:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30 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‑
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!