2D Line Normals (or: What's your vector, Victor?)
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
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:
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:
It all depends on what normal you consider correct in 2D, therefore you must decide for yourself.
Image 1
Going from A-D-C-B in counter-clockwise fashion you would derive this equation:
N.x = V2.y - V1.yN.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.yN.y = V2.x - V1.x
It all depends on what normal you consider correct in 2D, therefore you must decide for yourself.
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
-cheers
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement