PDA

View Full Version : Rotations



02-11-2000, 05:57 PM
I'm doing some rotations that didn't seem very hard to do in my head, but implementing has been a pain in the a**. Anyway, I want my draw function to do a 2D rotation around a point on the screen. In between calls to this function the point of rotation could change, but the new rotation must take into account the old rotations. Here's some I'm some code I'm trying if it helps...

if(firstTime){ //initailize matrix
glLoadIdentity();
glTranslatef(0.0, 0.0, -30.0);
glRotatef(zAngle, 0.0, 0.0, 1.0);
}else
glPopMatrix();

glTranslatef(xRotPos, RotPos, 0.0);
drawShape(0,0,0.6f,1.2f,0.6f,1.2f,0);
glRotatef(zChngAngle, 0.0, 0.0, 1.0);
glTranslatef(-xRotPos, -yRotPos, 0.0);
glPushMatrix();

Hec
02-11-2000, 08:26 PM
Hi Melissa http://www.opengl.org/discussion_boards/ubb/smile.gif, the first problem i saw is in th if part, you ask
if(firstTime){
load identity, and do trasnformations
BUT YOU NEVER SAY ITS BEEN THE firstTime
}

so i think it is always the firstTime... well i'm a little tired now :0 i'll come back tomorrow, and see somo more.

02-12-2000, 04:23 AM
Sorry, I didn't include that part of my code and didn't notice it till later. The variable firstTime is initialized to TRUE and set to FALSE at the end of this code. It's really close to working. If zAngle is 0 degrees it works fine (zAngle is the total amount rotated, zChngAngle is constant), but if it's a different angle then the point of rotation gets translated incorrectly. If I move it straight up, it moves slightly to the left as well(due to zAngle being 30 degrees in this example). This is my only problem. Sorry I didn't mention it before but I tried to keep it short. Thanks for your help Hec.

MikeC
02-14-2000, 02:00 AM
Hi Melissa,

The easiest way to do this is to track position (xPos, yPos)and rotation (zAngle) separately. When you move, only update the position. When you rotate, only update the rotation angle.

Now, EVERY time you want to draw the shape,
do the following:

glLoadIdentity();
glTranslatef( xPos, yPos, 0.0f );
glRotatef( zAngle, 0.0f, 0.0f, 1.0f );

This way the translation isn't messed up by any previous rotations. This approach is also a lot more numerically stable - if you apply repeated transformations to the same matrix, rounding errors in the float math will eventually cause it to squash or distort. By starting with a clean (identity matrix) slate each time, you don't get this problem.