Skip to main content
gamedev.net
🔒 Locked

Normals

Started by MattS423 May 21, 2003 at 11:24 AM 7 replies 1.2k views
Original Post
MattS423
MattS423
Ok...when i construct a normal to a side of a 2D polygon, is there any way to guarentee that it points away from the polygon? (see ascii drawing below)
Like this...
__________
|         |
|         | 
|         |------------------>
|         |   ^Normal^ -- Should point "out"
|         |
|_________|


 
Programmers of the world, UNTIE!
S1CA
S1CA
The order you supply the source vectors to the cross product you use to create your normal determine which way the normal points.

A search for "The right hand rule" should get you some diagrams to illustrate, such as: http://mathworld.wolfram.com/Right-HandRule.html

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
MattS423
MattS423
I was talking 2D...all the resources i could find were 3D...
Programmers of the world, UNTIE!
Zipster
Zipster
It works in 2D as well, however you have to use a slighly different process. Let''s say you have your points in clockwise order:


v1 v2
.................
. .
. .
. .
. .
. .
. .
. .
.................
v4 v3


What you first do is create a vector normal to your polygon. Since you''re working in 2D, you don''t even need any math. This vector can be (0,0,1). Now, if you go around and create vectors with your points, (v2 - v1), (v3 - v2), (v4 - v3), and (v1 - v4), you can take the crossproduct between the normal vector you created, and each of the side vectors, and your result will be a vector within the 2D plane perpendicular to the side, exactly what you''re looking for. All you have to do is normalized it. So, for an N-sided polygon, you have your normal vector (0,0,1), and your side vectors (v2 - v1), (v3 - v2), (v4 - v3), ... (vN - vN-1), (v1 - vN). Take the crossproduct between (0,0,1) and each of these vectors, and normalize.
MattS423
MattS423
Thats great, zipster, but is it pointing away from the polygonal region, like in my diagram?
Programmers of the world, UNTIE!
Digitalfragment
Digitalfragment
I remember trying a similar thing at one time. From what i can recall, i just peformed a point test to see if the normal was pointing inwards. Or just enforce clockwise winding then your normals will always point outwards when using the cross-product
MattS423
MattS423
How''d you do the point test?
Programmers of the world, UNTIE!
Zipster
Zipster
quote:
Original post by MattS423
Thats great, zipster, but is it pointing away from the polygonal region, like in my diagram?

Yes. However it depends on two things, a) the winding of your points, and b) which order you do the crossproduct in. If your points are in clockwise order, and you do the crossproduct between the artificial normal and the side vector (in that order), then the normal will point outwards. If your points are counter-clockwise, then you need to switch the order of the crossproduct, so that it's the side and then the artificial normal.

[edited by - Zipster on May 22, 2003 12:35:35 PM]
MattS423
MattS423
humm...
is it a real big deal to require the points to be passed in clockwise order? right now, i have no restrictions on ''em.

...i could re-order them...
Programmers of the world, UNTIE!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.