1

I have been attempting to get rotation working and it seems to work, my issue occurs when I then try to move the object, I was expecting the axis of the object to rotate with the mesh but instead it keeps the global axis and continues to move the object on that axis. Is my understanding of rotation flawed? or is it the code that is causing this to occur, also if my understanding is the issue is there a way to translate the objects axis with the object. my code is as follows.

Projection= glm::perspective(45.f, 4.0f / 3.0f, 0.1f, 100.0f);

//position and direction are calculated based on mouse position
View      = glm::lookAt(
position,
position+direction,
up
);
glm::mat4 myMatrix = glm::translate(x,y,z);
glm::mat4 myScalingMatrix = glm::scale(0.2f, 0.2f ,0.2f);
glm::vec3 myRotationAxis( 0, 1, 0);
glm::mat4 tempModel = glm::mat4(1.f);
glm::mat4 myRotationMatrix =glm::rotate( tempModel,45.f, myRotationAxis );

glm::mat4 Model= myMatrix* myRotationMatrix *myScalingMatrix;

glm::mat4 MVP = Projection* View * Model;
I Phantasm I
  • 1,651
  • 5
  • 22
  • 28
  • 1
    The order you do your transformations in determines the end result, it is very important to have a strong mental model of how it works: http://www.glprogramming.com/red/chapter03.html – Patashu Jun 12 '13 at 04:14

3 Answers3

2

Look up on matrix multiplication order, because the order matters. In your case, you first scale, then rotate your object, and in the end you move it according to the global axises. By switching the multiplication you can first move, then rotate the whole local coordinate system by the angle you want, resulting not only an obejct rotation, but the movement axis rotation too.

glm::mat4 Model= myRotationMatrix * myMatrix *myScalingMatrix;

Some reference on the subject.

Matzi
  • 13,770
  • 4
  • 33
  • 50
2

Since the rotation matrix only works for those rotations centered to the origin (i.e., (0,0,0)), you need to switch the origin to the center of your object in a back-and-forth manner.

Say your object locates at (X, Y, Z). Then if you would like to rotate centered to that object, you need to first translate(-X, -Y, -Z), then do the rotation, then translate(X, Y, Z) back to the original position in order to perform rotation center to your object.

This picture shows how it actually works: enter image description here

Here is the link containing the above picture, which also includes some basics about rotation.

keelar
  • 5,814
  • 7
  • 40
  • 79
  • nice figures. where/how did you do them? – axon Jun 11 '13 at 23:30
  • @axon: as for how to find them, I use keyword `computer graphics rotation translation centered to a position` in google images. – keelar Jun 12 '13 at 04:01
  • hmm interesting, would i need to apply the translation to the center render a frame and then send it back? i followed Matzi's answer and it worked but now my translation is in the wrong spot im guessing this is the cause. – I Phantasm I Jun 12 '13 at 04:11
  • @IPhantasmI Yes, you need to apply the translation twice. As the pictures illustrate, the first one is for rotation, the second one is for restoring the original position. – keelar Jun 12 '13 at 04:20
  • @keelar woops my mistake :p, after some further inspection it appears to reset the rotation after i move it back, any idea what im doing wrong? – I Phantasm I Jun 12 '13 at 04:34
  • @IPhantasmI reset the rotation ... did you happen to include the rotation matrix when you try to move it? That's the only thing I can think about xD – keelar Jun 12 '13 at 04:36
  • @IPhantasmI Oh, it depends on how you manipulate the object's location. If you calculate it on the fay, says translate(x, y, z) everytime, then you probably only need to do myRotationMatrix * glm::translate(x, y, z) since the object's location is always (0,0,0) and you translate it on the fly. If you change the object location instead of calculating it on the fly, then you need to glm::translate(-x, -y, -z) * myRotationMatrix * glm::translate(x, y, z). – keelar Jun 12 '13 at 04:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31618/discussion-between-i-phantasm-i-and-keelar) – I Phantasm I Jun 12 '13 at 04:56
0

In my case i was just drawing a simple triangle and playing with the transformations. When my triangle had the coordinates:

{0.0,2.0,-10.0,
-2.0,-2.0,-10.0,
2.0,-2.0,-10.0};

the scaling went as you said, since it scaled with respect to the center 0.0, 0.0, 0.0. The rotation along the y-axis messed up too. The obvious fix was to move the camera backward and place the object in the center.

{0.0,2.0,0.0,
-2.0,-2.0,0.0,
2.0,-2.0,0.0};

This is a trivial case that can be easily overlooked while focusing on all the maths. So make sure that your object is in the center.

Ash Catchem
  • 911
  • 11
  • 13