PDA

View Full Version : Rotating about a single axis



gottijay
07-19-2011, 05:16 AM
Not particularly sure if the title of this thread is correct...but what i a want to achieve is to be able to rotate the object about the x, y or z axis .

i already got the models rotating in all the axis but the point is they tend to move away from their original position when rotating, what i want is for them to stay in the same position while i rotate them in any angle i want



GLfloat rotMast[3];



void Draw(){
...........
glPushMatrix();
glTranslatef(positions[0].x, positions[0].y, positions[0].z);
glRotatef(rotMast[0], 1.0f, 0.0f, 0.0f);
glRotatef(rotMast[1], 0.0f, 1.0f, 0.0f);
glRotatef(rotMast[2], 0.0f, 0.0f, 1.0f);

drawMast();
glPopMatrix();
..........
}
void OnKey(unsigned char key, int x, int y) {
switch(key){
case 'k': case 'K':
if (selected_index==0){
rotMast[0]+= amount;
rotMast[1]+= amount;
rotMast[2]+= amount;
}
}
}

i would also like them to rotate in any angle so i could achieve any rotation desired...seems my method cannot achieve that.
please your ideas and possibly a short snippet would be very helpful....
thanks

MaxH
07-19-2011, 10:42 AM
My first suggestion is to use a different key for each rotation.
That will give you more control over the rotations.
The code below would do that.
It won't solve all your problems, but it's a big set in the right direction.




GLfloat amount = 2.0, xr = 0.0, yr = 0.0, zr = 0.0;

void Draw(void) {

glPushMatrix();

glTranslatef (positions[0].x, positions[0].y, positions[0].z);
glRotatef (xr, 1,0,0);
glRotatef (yr, 0,1,0);
glRotatef (zr, 0,0,1);
drawMast ();

glPopMatrix();
}

void OnKey (unsigned char key, int x, int y) {

switch (key) {
case 'x': xr += amount; break;
case 'X': xr -= amount; break;
case 'y': yr += amount; break;
case 'Y': yr -= amount; break;
case 'z': zr += amount; break;
case 'Z': zr -= amount; break;
}
}

gottijay
07-19-2011, 11:09 AM
thanks MaxH, i made your adjustments and also put the rotation functions before the translation function....but i still moves out of position while rotating....any ideas how i could fix that...

thanks for your help

MaxH
07-19-2011, 12:35 PM
Sounds like your model is not rotating about it's geometric center (or the point you want it to rotate around). Say the desired rotation point for the model has coordinates (xc,yc,zc). You have to translate the model putting this point at the origin, perform the rotations, then translate the model back to where you want it in the scene (see code below). Let's say it's a plane flying around, which you want to show banking, pitching, etc. You want it to bank and pitch about its center of mass (xc,yc,zc). Otherwise it doesn't look right. The 2 translates below surrounding the rotations do this. You can use your original translate to actually move the plane through the scene.



GLfloat amount = 2.0, xr = 0.0, yr = 0.0, zr = 0.0;

void Draw(void) {

glPushMatrix();

glTranslatef (positions[0].x, positions[0].y, positions[0].z);

glTranslatef (xc, yc, zc); // Put object back at original location.
glRotatef (xr, 1,0,0);
glRotatef (yr, 0,1,0);
glRotatef (zr, 0,0,1);
glTranslatef (-xc, -yc, -zc); // Put object center at origin.
drawMast ();

glPopMatrix();
}

void OnKey (unsigned char key, int x, int y) {

switch (key) {
case 'x': xr += amount; break;
case 'X': xr -= amount; break;
case 'y': yr += amount; break;
case 'Y': yr -= amount; break;
case 'z': zr += amount; break;
case 'Z': zr -= amount; break;
}
}

gottijay
07-19-2011, 03:23 PM
thanks MaxH,
how do i get the objects center??

MaxH
07-19-2011, 04:49 PM
The two 'glTranslate' commands I added to the code above do the centering. You have to know, or figure out, what (xc, yc, zc) is, which are simply the coordinates of the point you want to rotate you object around. It's probably going to be a point somewhere in the object. Do you know what the coordinate ranges of each object are? Where did the objects come from? Did you create them? If so, you should know what (xc,yc,zc) are. Good luck.

gottijay
07-20-2011, 04:22 AM
problem solved....i calculated the center in opengl and just followed exactly your method ....thanks alot MaxH