Jump to content
• Advertisement

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

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

If you intended to correct an error in the post then please contact us.

## 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

##### Share on other sites
Advertisement
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.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.

#### Share this 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

##### 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

##### Share on other sites
Ah, ok. Thanks again for the info.

-cheers

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5
• Advertisement

• 12
• 16
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632659
• Total Posts
3007692
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!