View Full Version : How to rotate objects around global axis not local axis

silvermeteor

11-10-2017, 03:21 AM

I'm newbie in openGL.

I'v searched for an examples from google, stackoverflow and opengl.org but I don't found an exactly result yet.

My issue is

1. I'v rotated a box in x axis by 45 degrees, the result is OK.

2. I'v rotated same box in y axis by 45 degrees, the result is weird.

It's not rotated in global y axis as my expect but rotated in own y axis.

How should I do? Are there any examples?

This is my code

...

model = glm::rotate(model, glm::radians(45.0f), glm::vec3(1.0, 0.0, 0.0));

model = glm::rotate(model, glm::radians(45.0f), glm::vec3(0.0, 1.0, 0.0));

myShader.setMat4("model", model);

myModel.Draw(myShader);

...

GClements

11-10-2017, 02:08 PM

Try reversing the order of the rotations.

The first rotation is rotates everything (including the axes) about the initial (global) axes, the second rotation rotates about the axes established by the first rotation.

So in your case, the second rotation is rotating about a Y axis which has itself been rotated by 45 degrees about the global X axis. If you change the order, the first rotation will rotate about the global Y axis, the second about an X axis which has itself been rotated by 45 degrees about the global Y axis.

If that isn't what you want, another option is to use rotation vectors: represent each rotation as a vector whose direction is the rotation axis and the length proportional to the rotation angle. Sum the vectors, then convert the final result to a length and a normalised vector, then pass those to glm::rotate(), e.g.

glm::vec3 x = glm::vec3(1,0,0)*glm::radians(45.0f);

glm::vec3 y = glm::vec3(0,1,0)*glm::radians(45.0f);

glm::vec3 r = x+y;

glm::mat4 m;

m = glm::rotate(m, glm::length(r), glm::normalize(r));

silvermeteor

11-11-2017, 06:14 PM

Try reversing the order of the rotations.

The first rotation is rotates everything (including the axes) about the initial (global) axes, the second rotation rotates about the axes established by the first rotation.

So in your case, the second rotation is rotating about a Y axis which has itself been rotated by 45 degrees about the global X axis. If you change the order, the first rotation will rotate about the global Y axis, the second about an X axis which has itself been rotated by 45 degrees about the global Y axis.

If that isn't what you want, another option is to use rotation vectors: represent each rotation as a vector whose direction is the rotation axis and the length proportional to the rotation angle. Sum the vectors, then convert the final result to a length and a normalised vector, then pass those to glm::rotate(), e.g.

glm::vec3 x = glm::vec3(1,0,0)*glm::radians(45.0f);

glm::vec3 y = glm::vec3(0,1,0)*glm::radians(45.0f);

glm::vec3 r = x+y;

glm::mat4 m;

m = glm::rotate(m, glm::length(r), glm::normalize(r));

Thanks for your suggestion, it seems worked at 45 degrees of both x and y axes.

but when I changed a rotation degree of some axis ex. x=45 deg. , y=80 deg.

the given result is still tilted. Have any ideas?

GClements

11-12-2017, 10:41 AM

In that case, you'll need to explain exactly what you mean.

didydriver

12-04-2017, 11:10 AM

Any rotations done prior to the object being called are held by the state. There for at the point that you have assigned is where the translation is occurring. Change your point of draw accordingly. If you want it to spin draw from dead center. End over end center bottom front or another side. Topple then choose a corner to draw from. If you want it to orbit then draw a specified distance from the point of rotation. Unless I'm missing the complication which is quite likely.

Powered by vBulletin® Version 4.2.3 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.