Skip to main content
GameDev.net gamedev.net
🔒 Locked

polygon rotation

Started by HyperMan Nov 20, 2001 at 10:35 AM 2 replies 1.3k views
Original Post
HyperMan
HyperMan
i need to rotate a polygon so that its normal is 0,0,1. how can i do that?
Fruny
Fruny
Let N be your normal and Z = {0,0,1}
Assume N is normalized ||N|| = 1
Let U = N cross Z, and x the angle between N and Z.

cos(x) = N dot Z;
x = arc cos( N dot Z )

You can then call glRotate( -x, U )
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
HyperMan
HyperMan
sorry, but i dont want to rotate it on the screen, i want to
rotate it by changing its values so that all its z-values are the same (so that the normal is 0,0,1) without deforming it.
grhodes_at_work
grhodes_at_work
quote:
Original post by HyperMan
sorry, but i dont want to rotate it on the screen, i want to
rotate it by changing its values so that all its z-values are the same (so that the normal is 0,0,1) without deforming it.


That''s exactly what Fruny''s code does. Nowhere does he say that Z is a screen Z direction. He defines it to be the normal that you desire, (0,0,1). You rotate about an axis perpendicular to both your current normal N and your desired normal (which Fruny called Z). That''s axis is defined by the U vector that Fruny calculates with the cross product. And the amount you rotate is the angle that he calculates using arc cos(N dot Z). The glRotate() call updates your model view transformation matrix (in OpenGL) to cause the polygon rotation that you desire.

What glRotate() really does is to construct a rotation matrix representing a rotation of angle -x, in radians, about axis U. Then it multiplies that matrix by the current model view matrix to get the updated matrix that when applied to the vertices of your polygon results in the entire polygon being rotated, without deforming its shape.

This information is available elsewhere, in tutorials and articles. Check out the "Articles & Resources" section of this site.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net

Topic Locked

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

Sign in to reply to this topic.