简体   繁体   中英

After GLM Rotation object is not centered anymore

I'm trying to rotate an object like

Model = glm::rotate(Model, glm::radians(15.f), glm::vec3(1.0f, 0.0f, 0.0f));

it is rotating, but the object seem also to be translated (it is higher). But: the decomposed translation show that the object is in his origin (0.0f,0.0f,0.0f).

How can I avoid this?

EDIT: I altered my code. I created the object at the position it should be, not origin. Now I create the object at origin and try to translate it afterwards. It is the same result, but now I can see the result.

This is my rotation code:

glm::vec3 scale;
glm::quat rotation;
glm::vec3 translation;
glm::vec3 skew;
glm::vec4 perspective;
this->Translate(glm::vec3(0.0f, 0.0f, 0.0f));
Model = glm::rotate(Model, angle,vector);
glm::decompose(Model, scale, rotation, translation, skew, perspective);

fprintf(stderr, "Translation: %f, %f, %f\n", translation.x, translation.y, translation.z);
fprintf(stderr, "Rotation: %f, %f, %f\n", rotation.x, rotation.y, rotation.z);

this->Translate(glm::vec3(0.0f, 0.0f, -10.0f));

glm::decompose(Model, scale, rotation, translation, skew, perspective);

fprintf(stderr, "Translation: %f, %f, %f\n", translation.x, translation.y, translation.z);
fprintf(stderr, "Rotation: %f, %f, %f\n", rotation.x, rotation.y, rotation.z);

At first I want to see the values for position and rotation after I rotate the object at origin. Which seems fine, tho The Values are:

Translation: 0.000000, 0.000000, 0.000000
Rotation: 0.000000, -0.130526, 0.000000

after that I use:

this->Translate(glm::vec3(0.0f, 0.0f, -10.0f)); //move backwards

and the values after that are:

Translation: -2.588191, 0.000000, -9.659258
Rotation: 0.000000, -0.130526, 0.000000

I don't know, why the X-values are also changed. Does somebody has an idea?

If I interpret your coordinates correctly, you have a plane at z=-10 . When you apply a rotation about the x-axis (through the origin) to this plane, it will also move. What you need to do is rotate it about an axis that passes through your plane. You can do this by combining the rotation with two translations that move the coordinate system to your plane. See eg this question for reference.

About the second part of your question: If you combine multiple rotations and translations, they affect each other. Let's consider the 2D case. If you rotate by 45° and then move along the (local, rotated) x-axis, the object moves diagonally. If you decompose the resulting matrix with glm, the decomposition assumes a certain order of matrices. While you calculated Rotation * Translation , glm will output the according values for Translation * Rotation . That's why you get different values than you expect. See eg this answer for reference.

No. Now I know my problem. When I use

this->Translate(glm::vec3(0.0f, 0.0f, -10.0f));

after the rotation, I move it along the Z-Axis on the object. Not the global Z-Axis. I just now have to figure out how to move an object on a global Axis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM