# Circle Line collision trouble

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

## Recommended Posts

I am studying collision detection, and I am currently trying to implement 2D circle/line collision.
I am using the following article: Simple Collision Detection

I am able to get the algorithm to work, but I have a problem, and I'm not sure how to solve it.
To calculate the normal of the line, I am just picking a perpendicular vector. There are two, if
I understand correctly, and I could use either of them.

 Normal for (x,y) = (-y,x) or (y, -x) 

Which normal I pick seems to determine which side of the line the collision is calculated.
But I need to pick the normal that is on the same side as the initial circle. I have read a lot of information
about the cross product in 2D, but I am finding lots of information and not all of it makes sense.

I am not even sure if picking the normal is the best way. Does anyone have any experience with this?

##### Share on other sites
What exactly do you want to calculate?
If you know the collision point, you can easily get the surface normal of the circle as normalize(collisionPoint - circleCenter), as every point on the circle is its own normal relative the circle center.

As for line normals, you can easily determine which side of the line you're on, and invert the normal if you're on the wrong side. If you have a line from p0 to p1, and the normal nx, ny calculated from that, calculate the line equation 'ax + by + c = 0'. a, b are the same as nx, ny, and you get c by plugging in a point on the line for x, y and solve for c. Then when you use any point x,y with that equation you get a number that is zero if the point is on the infinite line, or positive or negative depending on which side you're on.

##### Share on other sites
I am trying to make the circle bounce off the line. So I am taking the start and end positions of the circle, and then figuring out where the circle center should be so that the edge of the circle hits the line. I can then figure out reflection vectors and update the direction the circle is moving. But depending on which side of the line the circle starts, I think I need to determine which normal is on that same side.

I am not sure if that is even what I should do. I'm just guessing, and since I'm learning, I thought I'd find out if this makes sense, or is there a better way.

I will look at your solution for determining if the starting point and normal are on the same side of the line.

##### Share on other sites
You can also simply take the dot-product of the normal and the direction the circle is moving. If the dot product (nx * dx + ny * dy) > 0 then the normal is pointing in the same direction as the movement, and you probably want to invert the normal in the reflection calculation.

##### Share on other sites
Actually, the direction of the normal makes absolutely no difference to the reflection calculations; nor should it make any difference in the intersection calculations

To reflect the velocity we do: v' = v - 2n(n.v)
If the normal is negated, then v - 2(-n)((-n).v) = v - 2n(n.v) = v'

so makes no difference for the reflection.

##### Share on other sites

Actually, the direction of the normal makes absolutely no difference to the reflection calculations; nor should it make any difference in the intersection calculations

To reflect the velocity we do: v' = v - 2n(n.v)
If the normal is negated, then v - 2(-n)((-n).v) = v - 2n(n.v) = v'

so makes no difference for the reflection.

Here is how I am testing. I have a simple app running. First I click the mouse once for one point, and then a second time for a
second point. I use this for a line. Next I click once to add a circle starting center, and then another click for the circle ending center.
When I click one circle on one side of the line, and the other circle on the other side of the line, I calculate the collision point and draw the
circle who's edge is touching the line. Everything works fine.

However, it doesn't matter if I put the first circle on the left of the line and the second on the right, or I swap them and
put the first on the right and the second on the left, the algorithm always puts the collided circle on the left (just for example).

If I draw the line backwards, however, clicking the 2nd point first, then the algorithm puts the circle on the other side. I cannot figure out
how to always calculate the circle collision so it is on the same side of the line as the first circle.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26