PDA

View Full Version : Translation after rotation



manni
09-09-2010, 06:24 AM
Hi,
I have a question concerning basics: what do I have to do to translate in local coordinate system?
Some more details: when rotating and after that do some translations, the translations are in done in world coordinate system. but what if I want to do (the last translate) translate that object along x-axis which was rotated before (e.g. the x-axis rotated 90degrees) ? My operations are between push and popmatrix.
That's the order of transformations for the whole scene (yes I'm aware of order that goes "backwards"):
1.translate (to origin of world coordinatesystem) 2. rotate (e.g. 90degrees) 3.translate (to negative axis so i can see anything).
Now there is one special object with an additional translation: 1.translate 2.rotate 3.translate 4.translate.
the 4.translate is lets say [200,0,0]
..the object everytime is moved to the right, although the scene was rotated before.
That not what I wanted. I want to move that object along the rotated x-axis :D

If I apply the translation first, i would get strange rotations.. I have to do 1.translation first to get the object into the center of the world.

I'm going mad with this issue. Hope you can help me what to do to get the desired effect.

p.s. I'm a newb to 3d programming.


bye

marshats
09-09-2010, 10:24 AM
Did you try something like the following code (pseudo code since I don't know your exact axis, world coords, etc.)


glPushMatrix();
glTranslate(axis);
glRotate(90degrees);
glTranslate(World Coords);
if (special_case) glTranslate(200,0,0);
draw();
glPopMatrix();

manni
09-09-2010, 01:07 PM
I tried it and it works! Thankyou:)

Now another question:
What if I want to change the center of rotation to the center of a special (picked in scene) object? If i translate to a new object center, the scene would "jump" for the first time i chose an object, because of course there is a difference between the new and the old object middle.

What if I calculate the difference and then add to the modelView matrix?

So I tried it:


glPushMatrix();
glTranslate(axis);
glRotate(90degrees);
glTranslate(World Coords-->Change to new Center);
glTranslate(difference);
draw();
glPopMatrix();

1.) translate [difference] units along x,y and/or z-axis
2.) move center of object into the center of world. but wait, from what i have understood 1.) + 2.) translation are together moving the object [difference] units to the right of origin. that [difference] units to much :confused:

http://img163.imageshack.us/img163/4633/translatetranslate.png(scene after step 2)

3.) then it is rotated. but when the object is [difference] units to the right of world origin, it cannot be rotated around itself. it should be rotated around world (let's say) y-axis in a circle.

-->So now its jumping and rotating completely wrong. That was a fail.


Do you have an idea to change rotating center of the scene without having effects like scene jumping around?

marshats
09-09-2010, 10:14 PM
I don't quite understand your description. Are you trying to rotate the whole scene around a new center pivot point? Or are you trying to leave the selected object at its position but have it rotate in place there?

manni
09-10-2010, 03:00 AM
I don't quite understand your description. Are you trying to rotate the whole scene around a new center pivot point?

Yes, that's what i want. Rotate the whole scene around a new center. And that new center is the center of the selected object.

I tried several orders, but it doesn't work..

marshats
09-10-2010, 08:03 AM
In which coordinate system do you know the special object's center position or equivalently the rotation point? Is it global coordinates? local coordinates? or determined from some mouse click ie viewport coordinates?

Your problem sounds like you are subtracting a world coordinate and a rotation point in a different coordinate system.

ps are you aware of the gluLookAt (http://www.iro.umontreal.ca/~dift3355/GL/glu/lookat.html) function? This allows one to use a camera analogy which is more intuitive to work with.

manni
09-10-2010, 09:02 AM
In which coordinate system do you know the special object's center position or equivalently the rotation point? Is it global coordinates? local coordinates? or determined from some mouse click ie viewport coordinates?
It is in local coordinates. That is the coordinate system that i used for 3.transformation
glPushMatrix();
1.glTranslate(axis);
2.glRotate(90degrees);
3.glTranslate(World Coords); //world coord == 200,0,0
draw();
glPopMatrix();

Again, if i just try to substitute World Coords of transformation 3 let's say to 300,0,0 then I got a new rotating center and it works, because the scene is rotated around the new center. But still there is the effect of a jump at the beginning, when changing the center (3.).




ps are you aware of the gluLookAt (http://www.iro.umontreal.ca/~dift3355/GL/glu/lookat.html) function? This allows one to use a camera analogy which is more intuitive to work with.
Yes, I'm aware of that, but anyway, thanks for mention it again..

marshats
09-11-2010, 09:15 PM
glPushMatrix();
1.glTranslate(axis);
2.glRotate(90degrees);
3.glTranslate(World Coords); //world coord == 200,0,0
draw();
glPopMatrix();


Translates into mathematical notation:


M=M_topMatrix
1. M=M_topMatrix*M_translate_axis
2. M=M_topMatrix*M_translate_axis*M_rotate_90
3. M=M_topMatrix*M_translate_axis*M_rotate_90*M_trans late_200
R_world_coord=M_topMatrix*M_translate_axis*M_rotat e_90*M_translate_200*R_draw_local_coorids
M=M_topMatrix


Notice the right hand side is in local coordinates ie the coordinates that you specify all your objects in your scene. On the left hand side is the transformation into global coordinates ie your monitor view frame. As you stated your code, then, all operations are easiest thought of as happening in global coordinates. Do you have access to the openGL Redbook? Chapter three has a very good explanation of this. It doesn't solve the immediate problem of incorrectly mixing local and global coordinates.

If you have a simple glut or non-windows specific code showing the problem. I can take a look at it. I am in linux so it is difficult to debug non-cross platform code.

ps a better terminology may be to use the more standard "eye coords" and "object" coorids (respectively replacing global and local above). A worthwhile read is at songho tutorial (http://www.songho.ca/opengl/gl_transform.html).