Jump to content
  • Advertisement
Sign in to follow this  
thekiwimaddog

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.

If you intended to correct an error in the post then please contact us.

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.

collisioni.jpg



Thanks in advance!
David

Share this post


Link to post
Share on other sites
Advertisement
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 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
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!