Sign in to follow this  
Glass_Knife

Circle Line collision trouble

Recommended Posts

I am studying collision detection, and I am currently trying to implement 2D circle/line collision.
I am using the following article: [url="http://www.gamasutra.com/view/feature/3383/simple_intersection_tests_for_games.php"]Simple Collision Detection[/url]

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.

[code]
Normal for (x,y) = (-y,x) or (y, -x)
[/code]


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?

Thanks for your time,

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
[quote name='luca-deltodesco' timestamp='1317312743' post='4867243']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this