0
\$\begingroup\$

I have object moving from A to B on x-axis and there is no translation of object apart from it. Now, while moving, i want to rotate it around y-axis and the motion should change accordingly, i mean if i rotate it right when moving from -x to +x axis, it should move towards near plane. I have variable in gltranslatef which is modified in the loop after that i have glscalef to scale whole object which is made of hierarchical structure. Now i tried following code to achieve the expected result but its not working properly

   glTranslatef(move, 0, 0);

// If I comment these 3 lines, it does not affect the output
     glTranslatef(-move, 0, 0);
glRotatef(rotate,0,1,0);
glTranslatef(move, 0, 0);

glScalef(0.2, 0.2, 1.0);
\$\endgroup\$
1
\$\begingroup\$

Transformations (scale,rotation, translation) are applied in the reverse order of their invocation and transformations do not change the object's position or orientation but the coordinates system that will be used to render it. Imagine the 3D coordinates system as 3 axis ( X,Y,Z ) perpendicular to each other. In case your object lies along the X axis, if you rotate the coordinates around the Y axis, your object will come towards the near or far plane because it lies in a position on the X axis. If you want to rotate the object around itself, you should place it in the center of the coordinates system.

In your code:
You scale the coordinates meaning that when you draw an object it would appear scaled, then you move the object on the X axis and then rotate its coordinate system ( this is why the object is moving towards the near plane).
The correct order of your transformations should be:

glTranslatef(move, 0, 0);
glRotatef(rotate,0,1,0);
glScalef(0.2, 0.2, 1.0);
draw;
\$\endgroup\$
2
  • \$\begingroup\$ It rotates around the center but the motion is along x-axis. If it rotates 90 in clockwise while moving along +x axis, it should take a turn and move towards near plane. \$\endgroup\$ Oct 11 '13 at 20:54
  • \$\begingroup\$ For rotating around the object's center, i believe the order of the invocations are correct. By the order that you suggest, if "move" is constant, it will put the object in an orbit with radius "move" and center at <-move,0,0>. The transformations are applied in the reverse order, so an object from "object-space" should first be scaled, then rotated ( since its center is still <0,0,0> ) and then translated to the desired position. \$\endgroup\$ Oct 14 '13 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.