Rotate 3D objects
Does anyone have any code to rotate 3d objects (or 2d) in free rotation? I have looked at numerous 3d programming FAQs, and searched the web for many hours, but still cannot figure this out! (The algorithim I am using rotates it, but the image becomes distorted...) Can someone please point me in the right direction?
Your image is probably being distorted due to rounding errors. When rotating, it is best to keep the original points stored, and transform to new ones from these, displaying the transformed ones.There was a problem with a 3D house program I had once where it seemed the SIN and COS were in the wrong place or something, and caused distortion. The later one did not have this problem.
There is a great 3D example in the FB Examples folder/Handbook/3D Folder/View3D.65.main It even has vertex selection/highliting.
But for something maybe more clear (except it is in C, so I doubt it) and standard, download the source to Rotater 3.5 (or any earlier versions, Rotate PPC a good example) from http://raru.adelaide.edu.au/rotater. There is source available, and older source from the ftp directory.
There are basically two ways to rotate things, with Euler angles, and Quaternions, according to the discourse I see in comp.graphics.algorithms.
Euler angles, basically separate transformations among the 3 axes of X,Y,Z (yaw,pitch,roll), and are subject to "gimbal lock" at certain values.
Quaternions merge all rotations together and are the way to go, but are more complicated to implement. You can however combine other transformations in the matrix using quaternions I believe. Even without using quaternions, matrices are the way to do it.
But try the FN below first to see if it will do what you want, I think it is a port of a Rotate PPC FN that I did a long time ago. Untested! Remember to transform your points to origin 0,0, unless you are rotating around an axis, instead of rotating the object itself. For 2D, leave out the Z stuff.
'#define RADIANS 3.1416/180 //RADIANS conversion value DIM RADIANS# DIM tempx,tempy,tempz 'temp vars for storage as to not affect vars in the algorithm DIM oldx,oldy,oldz DIM newx,newy,newz DIM xAngleR# DIM yAngleR# DIM zAngleR# DIM xAngle# DIM yAngle# DIM zAngle# DIM xSin# DIM xCos# DIM ySin# DIM yCos# DIM zSin# DIM zCos# END GLOBALS LOCAL FN Rotate(xangle,yangle,zangle) 'send it an xangle, yangle, zangle I think 'Assumes points at origin 0,0 RADIANS# = 3.1416/180 'int newx=oldx; //Initialize these variables to hold the old coordinate values 'int newy=oldy; 'int newz=oldz; 'C code;above are dimmed in globals xAngleR# = xAngle#*RADIANS# 'convert angle to radian value (for sin/cos) yAngleR# = yAngle#*RADIANS# zAngleR# = zAngle#*RADIANS# xSin = sin(xAngleR#)'Store the sin/cos values for use in formula xCos = cos(xAngleR#)'so that each is calculated only once ySin = sin(yAngleR#)' yCos = cos(yAngleR#)' zSin = sin(zAngleR#)' zCos = cos(zAngleR#)' tempy = newy*xCos - newz*xSin 'rotate around the x-axis tempz = newy*xSin + newz*xCos ' newy=tempy 'update vars needed in algorithm newz=tempz ' tempx = newx*yCos - newz*ySin 'rotate around the y-axis tempz = newx*ySin + newz*yCos ' newx=tempx 'update vars needed in algorithm again newz=tempz tempx = newx*zCos - newy*zSin 'rotate around the z-axis tempy = newx*zSin + newy*zCos newx=tempx 'update vars for final storage newy=tempy END FN