# problem with glRotatef()

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

## Recommended Posts

Alright, i have an object that i want always to face 0,0,0. initially, if the object is drawn at 0,0,-15 it will face 0,0,0 but if its drawn at 10,0,-15 it will face 10,0,0. so i figured out to make the object face me i should do something like: glPushMatrix(); //mouth and other face features glRotatef(asin(x/z), 1, 0, 0); glColor3f(0.0,0.0,0.0); glLineWidth(3.0); glBegin(GL_LINES); glVertex3f(x-0.5,y,z+0.9); glVertex3f(x+0.5,y,z+0.9); glEnd(); glBegin(GL_LINES); glVertex3f(x-0.3,y-0.3,z+1.0); glVertex3f(x+0.3,y-0.3,z+1.0); glEnd(); glPopMatrix(); problem is, my object doesn't want to rotate. why won't it rotate towards 0,0,0?

##### Share on other sites
Couple of things:

1. Make sure you've got glMatrixMode(GL_MODELVIEW) in there somewhere.
2. asin() returns the angle in radians, but glRotatef() expects the angle in degrees.

If nothing else, the second thing is almost certainly causing you problems - you'll need to convert the return value of any c++ function to degrees before submitting it to OpenGL. Beyond that, I'm not sure if your algorithm is right, but I'm not quite clear enough on what you're doing to tell you what to do instead.

##### Share on other sites
The first thing is already done. Thank you for your second thing, and i changed it accordingly, but it still doesn't work. here's my code.

glPushMatrix();     //head                  glTranslatef(x,y,z);                  glRotatef((atan(x/z))*180/PI, 1, 0, 0);                  glScalef(0.85,1.0,1.0);                  glutSolidSphere(1.0,160,20);                  glPopMatrix();                                    glPushMatrix();           //body                  glTranslatef(x,y-1.2,z);                  glRotatef((atan(x/z))*180/PI, 1, 0, 0);                  glScalef(0.85, 1.0, 1.0);                  glutSolidSphere(1.0,160,20);                  glPopMatrix();                                    glPushMatrix();        //right arm                  glTranslatef(x+0.9,y-1.2,z+0.3);                  glRotatef((atan(x/z))*180/PI, 1, 0, 0);                  glutSolidSphere(0.4,160,20);                  glPopMatrix();                                    glPushMatrix();             //left arm                  glTranslatef(x-0.9,y-1.2,z+0.3);                  glRotatef((atan(x/z))*180/PI, 1, 0, 0);                  glutSolidSphere(0.4,160,20);                  glPopMatrix();                                    glPushMatrix();            //right leg                  glTranslatef(x+0.4,y-2,z+0.0);                  glRotatef((atan(x/z))*180/PI, 1, 0, 0);                  glScalef(0.8, 2.3, 1.0);                  glutSolidSphere(0.5,160,20);                  glPopMatrix();                                    glPushMatrix();           //left leg                  glTranslatef(x-0.4,y-2,z+0.0);                  glRotatef((atan(x/z))*180/PI, 1, 0, 0);                  glScalef(0.8, 2.3, 1.0);                  glutSolidSphere(0.5,160,20);                  glPopMatrix();                                    glPushMatrix();           //mouth and other face features                  glRotatef((atan(x/z))*180/PI, 10, 0, 0);                  glColor3f(0.0,0.0,0.0);                  glLineWidth(3.0);                  glBegin(GL_LINES);                       glVertex3f(x-0.5,y,z+0.9);                       glVertex3f(x+0.5,y,z+0.9);                  glEnd();                  glBegin(GL_LINES);                  glVertex3f(x-0.3,y-0.3,z+1.0);                  glVertex3f(x+0.3,y-0.3,z+1.0);                  glEnd();                  glPopMatrix();

I'm trying to make what i draw face 0,0,0. so it should rotate accordingly. for example, if an object is at 10,0,20 then i want it to face 0,0,0 so i would rotate the object atan(10/20)*180/PI degrees and then the other three parameters would be 1,0,0 because i want it to rotate sideways.(PI is definied as 3.1415926535898) I'm 95% sure that my math is correct, but the glRotatef() function is not doing what i want it to. its as if its not there. why is that?

##### Share on other sites
Maybe the outcome of atan(x/z) is such a low number (few degrees) that you just cant see the rotation.

##### Share on other sites
may be the x&z not change value?
and
may be not execute glutSwapBuffers() in display func?
and
glutInitDisplayMode() must specify GLUT_DOUBLE parameter
or
PFD structure specify PFD_DOUBLEBUFFER parameter

##### Share on other sites
Another thing is that you compute the angle from x,z co-ordinates, so I assume you want to do a heading (say you want to rotate around the y axis):
glRotatef((atan(x/z))*180/PI, 0, 1, 0);
Well, your code shows glRotates using the x axis:
glRotatef((atan(x/z))*180/PI, 1, 0, 0);
The fact that you position the head and torso at the same x,z but different y co-ordinates enforces me in this meaning.

Another point to mention (but I asume you already know it) is that atan(x/z) is good if your original (i.e. zero angle) orientation is along the negative principal z axis. I.e. it faces the target already if the model is moved along positive z. If not you have to use an angle offset.

Please notice that atan works fine as long as only one of x or z changes its sign, and z being not 0, of course. You may run into trouble with it since atan could not distinct between -x/z and x/-z. Please take into account to use atan2 instead.

Please notice furthur that your choose of rotation could face the principal y axis (0,y,0) only instead of the origin point (0,0,0). If you actually want to look at the _point_ (0,0,0) then there is more to do.

(EDIT: Have removed the paragraph about negating the rotation angle.)

[Edited by - haegarr on January 10, 2006 5:05:58 AM]

##### Share on other sites
let me verify EXACTLY what i want. I'm at 0,0,0. I'm in a FP view(i can't move). I want what i draw to always face me. so if its at 10,0,-15, it should rotate towards me with the above function. I still don't get why it doesn't work.

##### Share on other sites

Try this:

http://www.opengl.org/resources/faq/technical/lookat.cpp

It makes any piece of geometry face a point you chose :)
You should definetely give opengl.org a try, the next time you have this sort of question! They have very compreensive FAQs. You just gotta love their attitude of having everything oriented towards the developers.

##### Share on other sites
Quote:
 Original post by bballmitchlet me verify EXACTLY what i want. I'm at 0,0,0. I'm in a FP view(i can't move). I want what i draw to always face me. so if its at 10,0,-15, it should rotate towards me with the above function.

All I've posted above still holds. To clarify my explanations, let me give some examples. I assume a right handed co-ordinate system as OpenGL uses natively.

The first question is where your model _looks initially_ to, since I assume it not being identical from front and back. Often models are created a way so that they look along the z-axis. If you move the model in the (x,z) plane to (0,0,-10) then it faces (0,0,0) w/o any rotation (or better, w/ a 0 degree rotation). If the model is moved to (0,0,+10) instead, then it has to be rotated by 180 degree around the y axis. If, on the other hand, your model looks initially along -z, then moving to (0,0,-10) needs the 180 degree rotation, and moving to (0,0,+10) needs the 0 degree rotation. This is an rotation _offset_ dependent on the model's geometry!

Next, a rotation is totally described by a rotation axis and a rotation angle. This is just the way glRotate expects its parameters. The "look-at" vector of your model is in a given orientation, and you desire it to be in another orientation. If thinking of these both vectors emanating from the model's origin, they spread out a "plane of rotation". E.g. as long as the model is translated in the (x,z) plane only, the both look-at vectors have no y component, so plane of rotation is the (x,z) plane itself. The rotation axis is perpendicular to this plane, to be computed by the cross product (and normalization) of the both look-at vectors. In the example above that yield in the y axis, so you have to invoke glRotate with (0,1,0) as rotation axis in that situation!

The angle of rotation is computed by the atan function. Lets assume the model is translated to (10,0,5). Then you compute the angle to atan(10/5) = +63.44 degree. Assume you rotate the model correctly by +63.44 degree and hence it faces the (0,0,0). Now translate it to (-10,0,-5), say to the exact _opposite_ of the previous translation. Then the atan yield in atan(-10/-5) = +63,44 degree! But rotating the model by the same angle will now yield in backward facing! Moreover, computing atan(x/0) will crash. In conclusion, using the atan function isn't wise. It would be better to use atan2(x,z) instead. (Of course, perhaps it plays no role whether the model faces false in behind since you _never_ could see it, but you haven't told so.)

However, to come to the third point, Rotating around a _fixed_ axis like (0,1,0) could force the model to face the (0,y,0) axis only in general, but not the point (0,0,0). In other words, the model may head to (0,y,0) but it will not bent the neck to actually look down at the point (0,0,0). That doesn't matter if e.g. the model is the avatar of a person, and it should look at the avatar of another person. But it does matter if the avatar of an elephant should look at the avatar of a mouse.

So a general solution to your problem has to compute the rotation axis and angle by other ways, e.g. by using the cross product and dot product. But then special situations have to be considered to let the whole work well under all circumstances. So, either you implement a general solution, or else make use of some constraints I've hinted at above?! Your code so far reflects some constraints, so jyk was right to ask for more details.

I still suggest you to first have a look at your choice of the rotation axis as mentioned above.

1. 1
Rutin
36
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 98
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009654
• ### Who's Online (See full list)

There are no registered users currently online

×