Sign in to follow this  
inquisition

2D Line Normals (or: What's your vector, Victor?)

Recommended Posts

Hello, I am having a small bit of trouble with normals to a line in 2D. I have been doing a bit of searching on the subject and I understand what a normal is and how to find them. The only thing that really confuses me is the fact that every resource I have read thus far has only talked about one normal (In one direction). Isn't there two possible normals for a given line segment (one off each side)? How do I know which of these to use? I tried to draw a diagram to figure it out but I came up with four possibilities between the ball and its point of impact; either above or below and either left or right. Is there an easy way to only get the normal for the side on which the ball is approaching? I apologize if this has been covered before but I was unable to find it if it was. Thanks in advance. -cheers

Share this post


Link to post
Share on other sites
You're correct, there are 2 different normals to a line in 2D; normals in 2D are dependent on your winding order for vertices. Generally people work with counter-clockwise (CCW) vertices. Thus to achieve the normals given in this image:

Image 1
Going from A-D-C-B in counter-clockwise fashion you would derive this equation:

N.x = V2.y - V1.y
N.y = V1.x - V2.x

Now if you applied the equation above to clockwise (CW) vertices, you would obtain the "wrong" normal, as shown in this image:

Image 2
Going from A-B-C-D in clockwise fashion. Thus to achieve "correct" normals from Image 1 while maintaining the use of clockwise vertices, you would need to derive the following equations to go from A-B-C-D:

N.x = V1.y - V2.y
N.y = V2.x - V1.x


It all depends on what normal you consider correct in 2D, therefore you must decide for yourself.

Share this post


Link to post
Share on other sites
Ah, I see. How would I find the normal that is on the same side of the line as my ball? I was thinking just checking the sign of the difference between the X and Y components but that leaves me with six possibilities and only two vectors to consider. There must be an easier way to do it, or at least a more elegant method. Thanks again.

-cheers

Share this post


Link to post
Share on other sites
Find the dot product of a normal and the vector from either of the line's endpoints to the ball's position. If the result is negative, negate the normal.

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