# Circle-Line Collision

This topic is 2654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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.

David

##### 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.

---edit:

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 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 on other sites
Does anyone have any example code of either of these methods working?
My math isn't as good as both of yours so I'm a bit baffled by your equations.

Thanks Again!
David

1. 1
Rutin
52
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633412
• Total Posts
3011729
• ### Who's Online (See full list)

There are no registered users currently online

×