• 12
• 12
• 9
• 10
• 13

# at my wits end -- glrotate changes based upon order

This topic is 4143 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

glNewList(3, GL_COMPILE);
glColor3f(0.6,0.55,0.9);
glRotatef(fish1.anglex,1.0,0.0,0.0);
glRotatef(fish1.angley,0.0,1.0,0.0);
glRotatef(fish1.anglez,0.0,0.0,1.0);

glutWireSphere(.3,7,7);
glTranslatef(0.0,0.0,-.5);
glutWireCone(.2,.3,5,5);
glEndList();


When I move around the order of the glRotate commands, the result of the 3 rotations actually comes out differently. Can anyone explain what I am doing wrong here? Thanks!

##### Share on other sites
I think in OpenGL you have to translate than rotate.

##### Share on other sites
yes well anyways your rotation is affected by the translation so you want to push the matrix, load identity, do the translation and rotation, and than pop it and it should be ok I think when you do the second part.

##### Share on other sites
Quote:
 I think in OpenGL you have to translate than rotate.

Not quite true. You can have them anyway around you wish, however it is good to follow a guideline and stick with it through out your project otherwise things will end up where you don't expect them to!

ktuluorion: The modelview matrix is cumulative (big word eh?) meaning that one operation on the matrix followed by another can lead to a completely different result if you switched the operations around. Consider this:

This shows a rotation followed by a translation:

This shows a translation followed by a rotation:

The blue line shows the direction of the translation, which in all cases is the X axis. However rotating causes the axis of the object to rotate also.

##### Share on other sites
Rotation operations don't commute. That means that rotating about the X axis and then rotating about the Y axis gives you different results than rotating about Y first and then X. So there's nothing wrong with the fact that it happens. Maybe if you provide a more thorough description of what you're trying to accomplish, people around here could help you construct and order your matrices in the way that gets the effect you want?

##### Share on other sites
Quote:
 When I move around the order of the glRotate commands, the result of the 3 rotations actually comes out differently.

You're using Euler angles for rotation; this is a well-known issue with that particular method. You work around it by always doing your rotations in a fixed order (always XYZ, always ZXY, whatever, just pick one).

You fix the problem by using a better method entirely, such as directly using axis-angle rotations or by using quaternions. This will require more work on your end, however, as neither method is a magic bullet for fixing rotational issues. You simply need to understand the mathematics involved and learn how to make it work for you, instead of the other way around.

##### Share on other sites
I'm trying to aim a fish in a certain direction based upon a vector that it is moving in. Here's the other relevent source:

  fish1.vx=0.000f;  fish1.vy=0.000f;  fish1.vz=0.0001f;  fish1.x+=fish1.vx;  fish1.y+=fish1.vy;  fish1.z+=fish1.vz;    mag=sqrt(pow(fish1.vx,2)+pow(fish1.vy,2)+pow(fish1.vz,2)); //magnitude of vector  fish1.anglex=deg(acos((fish1.vx/mag)));  fish1.angley=deg(acos((fish1.vy/mag)));  fish1.anglez=deg(acos((fish1.vz/mag)));

##### Share on other sites
Quote:
 Original post by ktuluorionI'm trying to aim a fish in a certain direction based upon a vector that it is moving in. Here's the other relevent source:*** Source Snippet Removed ***

Well, I can't help too much here, as this has just moved into unfamiliar territory for me (maths! :(), although until somebody who knows something about maths comes along and can give a deffinite answer, can you upload your code and I'll have a look for you see if I can figure it out?

EDIT: Sorry, I know it's pretty obvious, although I've made the same daft mistakes ;), but are you using glLoadIdentity anywhere?

Another EDIT: Here found this, maybe it will help? Something similar?

##### Share on other sites
itll never work with glrotate (unless u limit the rotations) the problem is due to gimbal lock, search for that + u should find solutions as well

##### Share on other sites
Quote:
 Original post by NewBreedNot quite true. You can have them anyway around you wish, however it is good to follow a guideline and stick with it through out your project otherwise things will end up where you don't expect them to!

It does matter because OpenGL internally multiplies matrices with the values and matrix multiplication is not commutative. Ie AB != BA so the order you do it is important. It is SRT (Scale Rotation and Translation) and in OpenGL I always do TRS.