# Circle-Line Collision

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

## Recommended Posts

Hello,

I'm trying to follow the tutorial on this website about circle-line collision.

The following is a copy from that tutorial:

[color="#283769"][font="Arial, sans-serif"]Find several other points:[/font]
• The point of intersection between line and the movement vector of the circle. (a)
• The closest point on the line to the endpoint of the movement vector of the circle. (
• The closest point on the movement vector to [color="#006000"][font="monospace"](x1, y1)[/font]. ©
• The closest point on the movement vector to the other endpoint. (d)[color=#283769][font=Arial, sans-serif]Using the points, it is then possible to eliminate many positions that would not result in a collision. A collision is possible if:[/font]
• a is less than [color="#006000"][font="monospace"]r[/font] away from each endpoint on the movement vector and on the line.
• b is less than r away from the endpoint of the movement vector and on the line segment.
• c is less than r away from [color="#006000"][font="monospace"](x1, y1)[/font] and on the movement vector.
• d is less than r away from [color="#006000"][font="monospace"](x2, y2)[/font] and on the movement vector.
[color=#283769][font=Arial, sans-serif][color="#000000"][font="arial, verdana, tahoma, sans-serif"]Now, my problem is that my tests mostly fail on these last four points even when it is obvious that there is a collision. Take the above image for an example.[/font]
[color="#000000"][font="arial, verdana, tahoma, sans-serif"] [/font]
[color="#000000"][font="arial, verdana, tahoma, sans-serif"]Is this tutorial wrong/incomplete or am I missing something?
[/font][/font]

##### Share on other sites
Based on a single read-through, I can't really confirm whether the approach described in the tutorial is valid. (Maybe it is - it's just not immediately clear to me one way or the other.)

Here however is a method that's guaranteed to produce the correct results (provided all potential numerical issues are handled correctly, of course).

To determine whether a collision will occur, and if so, when it will occur and what the contact point and normal will be, intersect a line segment representing the motion of the circle with a capsule for which the line segment you're testing against is the medial segment, and for which the radius is the circle radius. This approach takes a little work to implement, but it'll handle all cases correctly.

(Again, maybe the method you're using now works, although on first read at least it seems a little questionable. Maybe someone else can comment on that further.)

##### Share on other sites
Wow, thats a great help! Thanks!

I will let you know how it went.

##### Share on other sites
After reading the tutorial I second jyk's suggestion, because for the most part it reduces to a rather easy computation: distance between two possibly intersecting segments. If the distance between the trajectory of the circle center and the wall segment is smaller than the circle radius, they collide at some point of the considered movement.

Finding the time/place of impact is little extra work: the approach based on similar triangles found in the tutorial is simple and correct, although described with some confusion between segments and whole lines.

##### Share on other sites
This capsule method works great indeed. Still, at some point when the circle moves very slow the circle goes right through the wall. External forces are applied before the collision detection and do not alter the position of the circle, so that should not be the problem. Any ideas?

##### Share on other sites

This capsule method works great indeed. Still, at some point when the circle moves very slow the circle goes right through the wall. External forces are applied before the collision detection and do not alter the position of the circle, so that should not be the problem. Any ideas?

Can't say for sure without seeing the code, but numerical problems can arise if the magnitude of the displacement is very low. (This problem is unavoidable even from a purely mathematical standpoint, since it makes little sense to compute the time of intersection for a non-moving object, unless that time is zero of course.)

I wouldn't expect the test to fail though unless the magnitude of the displacement vector was very low or (potentially) the circle was moving parallel or nearly parallel to the capsule axis. If you're finding that the test is failing in some cases, I'd recommend doing some debugging to try and determine where exactly the failure is occurring. (It may be due to an error in your implementation, or simply due to not handling the various potential numerical issues correctly.)

##### Share on other sites
Hi there,

Check out my old article about circle vs capsule collision detection - very similar to this problem:

http://www.pfirth.co.uk/collision.html#CircleCapsule

Cheers, Paul.

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631780
• Total Posts
3002314
×