Sign in to follow this  

Circle-Line Collision

Recommended Posts

Hi everyone, I was wondering if anyone could point me in the right direction or give me some example code on how to work out the collision point between a Line segment and a circle.
I have Points C1, C2, P1, P2 and the radius of the circle.
I need to find out ithe position of X when the edge of the circle hits the line.


Thanks in advance!

Share this post

Link to post
Share on other sites
Let v = c2-c1, q = p2-p1, n = perp(q)/|q|, a = c1-p1

then solving for x = c1 + t.v for unknown scalar value t

there are 2 cases to consider; the circle hits a vertex first, or the circle hits the line between the vertices first. Note that if you draw a line perpendicular to the line segment through the centre of the circle at x, then it will touch the point where the circle/line touch, so that gives you how to determine the time for the circle->edge case

For the circle to vertices case you have (for both vertices) |c1 + tv - p|[sup]2[/sup] = |tv + c|[sup]2[/sup] = r[sup]2[/sup] => t[sup]2[/sup]|v|[sup]2[/sup] + 2t.a.v + |a|[sup]2[/sup] - r[sup]2[/sup] = 0.
For the circle to edge case you have ((c1 + tv - p1).n)[sup]2[/sup] = ((tv+a).n)[sup]2[/sup] = r[sup]2[/sup] => t[sup]2[/sup](v.n)[sup]2[/sup] + 2t(a.n)(v.n) + (a.n)[sup]2[/sup] - r[sup]2[/sup] = 0

so you have 3 quadratic equations in 't'. solve all 3 of them and choose the minimum value >= 0, and this is the 'time' of collision.


for the circle-edge case you'd also want to check that the intersection between the line containing c1,c2 and that containing p1,p2 is actually on the line segment before considering this case.

Share this post

Link to post
Share on other sites
Another cheap trick is to offset the line by the circle radius and then intersect the shifted line with the trajectory of the circles center. This reduces the circle-edge case to a simple line-line intersection problem. You still have to check for intersection with the endpoints though.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this