Reputation: 1681
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;
Upvotes: 1
Views: 2762
Reputation: 921
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.
Upvotes: 0
Reputation: 6026
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:
Here is the link containing the above picture, which also includes some basics about rotation.
Upvotes: 2
Reputation: 13925
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.
Upvotes: 2