rotating in 3d
is there a proper way to rotate in 3d and have all rotations relative to eachother? example:
OPEN GL CODE:
Rotatef( xa, 1, 0, 0 );
Rotatef( ya, 0, 1, 0 );
Rotatef( za, 0, 0, 1 );
the y is relative to the x but not z
the z is relative to both
x is relative to neither
HELP!
There are two accepted methods. One is traditional (slower), and the other is with matrices. This is off the top of my head, so don''t expect this to be entirely accurate.
Traditional: Say your point you wish to rotate is in the variables x0, y0, and z0. Say the angles you want to rotate it by are xr, yr, and zr. Say that the variable "rad" is equal to pi/180. Here''s the code:
x1=x0 // Pitch (X axis)
y1=y0*cos(xr*rad)-z0*sin(xr*rad)
z1=y0*sin(xr*rad)+z0*cos(xr*rad)
x2=z1*sin(xr*rad)+x1*cos(yr*rad) // Yaw (Y axis)
y2=y1
z2=z1*cos(yr*rad)-x1*sin(yr*rad)
x3=x2*cos(zr*rad)-y2*sin(zr*rad) // Roll (Z axis)
y3=x2*sin(zr*rad)+y2*cos(zr*rad)
z3=z2
The final point is stored in x3,y3,z3. This rotates around the origin. It assumes you supply angle measures in angles, yet the sin/cos functions work in radians. It is simplified (not optimized) for clarity. Don''t ask me to explain it because I don''t remember (and don''t wish to remember) the
derivation.
Now, I don''t know how good your math is, but with matrices, if you take your point (in a 1x3 matrix), and you multiply it by the following matrices, you rotate that point. Here''s the matrices:
Roll (Z)
/ cos(m) sin(m) 0 /
/ -sin(m) cos(m) 0 /
/ 0 0 1 /
Pitch (X)
/ 1 0 0 /
/ 0 cos(m) sin(m) /
/ 0 -sin(m) cos(m) /
Yaw (Y)
/ cos(m) 0 -sin(m) /
/ 0 1 0 /
/ sin(m) 0 cos(m) /
"m" is the angle - I got lazy of using xr/yr/zr. You can simplify this by combining it into one matrix, which is useful if you have a number of points to rotate by the same angle - mighty useful in a 3D real-time environment. This Derivation I DO remember, yet it would take too much space. If you don''t understand how to use this, just read up on it. Many sources use this. Again, if I am mistaken, I apologize. Hope this helps. If I ever have the time, I''ll
explain this in more detail.
-Ender Wiggin
Traditional: Say your point you wish to rotate is in the variables x0, y0, and z0. Say the angles you want to rotate it by are xr, yr, and zr. Say that the variable "rad" is equal to pi/180. Here''s the code:
x1=x0 // Pitch (X axis)
y1=y0*cos(xr*rad)-z0*sin(xr*rad)
z1=y0*sin(xr*rad)+z0*cos(xr*rad)
x2=z1*sin(xr*rad)+x1*cos(yr*rad) // Yaw (Y axis)
y2=y1
z2=z1*cos(yr*rad)-x1*sin(yr*rad)
x3=x2*cos(zr*rad)-y2*sin(zr*rad) // Roll (Z axis)
y3=x2*sin(zr*rad)+y2*cos(zr*rad)
z3=z2
The final point is stored in x3,y3,z3. This rotates around the origin. It assumes you supply angle measures in angles, yet the sin/cos functions work in radians. It is simplified (not optimized) for clarity. Don''t ask me to explain it because I don''t remember (and don''t wish to remember) the
derivation.
Now, I don''t know how good your math is, but with matrices, if you take your point (in a 1x3 matrix), and you multiply it by the following matrices, you rotate that point. Here''s the matrices:
Roll (Z)
/ cos(m) sin(m) 0 /
/ -sin(m) cos(m) 0 /
/ 0 0 1 /
Pitch (X)
/ 1 0 0 /
/ 0 cos(m) sin(m) /
/ 0 -sin(m) cos(m) /
Yaw (Y)
/ cos(m) 0 -sin(m) /
/ 0 1 0 /
/ sin(m) 0 cos(m) /
"m" is the angle - I got lazy of using xr/yr/zr. You can simplify this by combining it into one matrix, which is useful if you have a number of points to rotate by the same angle - mighty useful in a 3D real-time environment. This Derivation I DO remember, yet it would take too much space. If you don''t understand how to use this, just read up on it. Many sources use this. Again, if I am mistaken, I apologize. Hope this helps. If I ever have the time, I''ll
explain this in more detail.
-Ender Wiggin
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement