# Thread: 2D collision between a moving circle and a fixed line segment

1. ## 2D collision between a moving circle and a fixed line segment

In the context of a game program, I have a moving circle and a fixed line segment. The segment can have an arbitrary size and orientation.
• I know the radius of the circle: r
• I know the coordinates of the circle before the move: (xC1, yC1)
• I know the coordinates of the circle after the move: (xC2, yC2)
• I know the coordinates of the extremities of the line segment: (xL1, yL1) - (xL2, yL2)

I am having difficulties trying to compute:
• A boolean: If any part of the circle hits the line segment while moving from (xC1, yC1) to (xC2, yC2)
• If the boolean is true, the coordinates (x, y) of the center of the circle when it hits the line segment (I mean when circle is tangent to segment for the first time)

2. ## Re: 2D collision between a moving circle and a fixed line segment

Originally Posted by joeljoel
In the context of a game program, I have a moving circle and a fixed line segment. The segment can have an arbitrary size and orientation.
[LIST][*]I know the radius of the circle: r[*]I know the coordinates of the circle before the move: (xC1, yC1)[*]I know the coordinates of the circle after the move: (xC2, yC2)[*]I know the coordinates of the extremities of the line segment: (xL1, yL1) - (xL2, yL2)

I am having difficulties trying to compute:
• A boolean: If any part of the circle hits the line segment while moving from (xC1, yC1) to (xC2, yC2)
• If the boolean is true, the coordinates (x, y) of the center of the circle when it hits the line segment (I mean when circle is tangent to segment for the first time)
Here is how I would do this question:

1. Determine the equation of the line-segment:

$\dfrac{y-yL1}{x-xL1} = \dfrac{yL2-yL1}{xL2-xL1}$

Re-write this equation into the form

$Ax + By + C =0$

2. If the circle touches the line the center of the circle has the distance r from the line. The distance of a point P(p, q) from the line is calculated by:

$d = \dfrac{Ap+Bq - C}{\sqrt{A^2+B^2}}$

(You see here why it is essential that you transform the equation of the line)

3. Obviously the center of the circle is moving on straight line too such that the y-coordinate of the center is a constant. Set d = r and solve for the x-coordinate of the new center.

4. You'll get 2 different circles which satisfy the given conditions.