0

I have been attempting to rotate an object around its local coordinates and then move it based off based of the rotated coordinates but i have not been able to achieve the desired results, to explain the problem in a more in depth way i have an object at a certain point in space and i need to rotate it around its own origin(not the global origin) and then translate the object based off of the newly rotated axis's, after much experimenting i have discovered that i can either rotate the object around is origin but the coordinates will not be rotated with it or i can have the objects local coordinates be transformed with it but it will then rotate around the global origin.

currently my rotation/translation/scaling code looks like this

glm::mat4 myMatrix = glm::translate(glm::mat4(1.0f),trans);
glm::mat4 Model = glm::mat4(1.f);
glm::mat4 myScalingMatrix = glm::scale(sx, sy ,sz);
glm::vec3 myRotationAxis( 0, 1, 0);
glm::mat4 myRotationMatrix =glm::rotate(glm::mat4(1.0f),rot, myRotationAxis);

Model= myScalingMatrix* myRotationMatrix*myMatrix;
glm::mat4 MVP = Projection* View * Model;

I believe this is the problem code specifically the second line from the bottom but i could be wrong and will be post more code if its needed.

i have also attempted to create an inverse matrix and use that at the start of the calculation but that appears to do nothing(i can add the code that i attempted to do this with if needed) If any kind of elaboration is needed regarding this issue feel free to ask and i will expand on the question Thanks.

EDIT 1: Slightly modified code that was suggested in the answers section, still giving the same bug though.

glm::mat4 Model = glm::mat4(1.f);
glm::mat4 myScalingMatrix = glm::scale(sx, sy ,sz);
glm::vec3 myRotationAxis( 0, 1, 0);
glm::mat4 myRotationMatrix =glm::rotate(glm::mat4(1.0f),rot, myRotationAxis);

glm::vec4 trans(x,y,z,1);
glm::vec4 vTrans = myRotationMatrix* trans ;
glm::mat4 myMatrix = glm::translate(glm::mat4(1.0f),vTrans.x,vTrans.y,vTrans.z);
Model= myScalingMatrix* myRotationMatrix*myMatrix;
I Phantasm I
  • 1,651
  • 5
  • 22
  • 28

1 Answers1

-1

You need to apply your rotation matrix to the translation vector (trans).

So, assuming trans is a vec4, your code will be:

glm::mat4 Model = glm::mat4(1.f);
glm::mat4 myScalingMatrix = glm::scale(sx, sy ,sz);
glm::vec3 myRotationAxis( 0, 1, 0);
glm::mat4 myRotationMatrix =glm::rotate(glm::mat4(1.0f),rot, myRotationAxis);

glm::vec4 vTrans = myRotationMatrix * trans;


glm::mat4 myMatrix = glm::translate(glm::mat4(1.0f), vTrans.xyz);

Model= myScalingMatrix* myRotationMatrix*myMatrix;
glm::mat4 MVP = Projection* View * Model;

convert vec4 to vec3

So to complete the answer, if the model center is not (0,0,0) , you will have to compute bounds of your model and translate it by half of it less model bottom left vertex.

It's well explicated here: model local origin

According to supplied code, the answer is the best available... if you wants more details, supply some screenshots and details on your Projection and view matrix calculations

Community
  • 1
  • 1
j-p
  • 1,622
  • 10
  • 18
  • Slight issue that is making it so i cant compile the code, trans is a vec3 which is easily solved by converting it over to a vec4 but apparently vec4 isn't compatible with glm translate – I Phantasm I Mar 19 '14 at 04:41
  • take the 3 first components: – j-p Mar 19 '14 at 04:45
  • Got the code working but it rotates my model around the global origin rather than its own local origin and then translates it based off of the rotation so the translation is what im aiming for but the rotation isnt. – I Phantasm I Mar 19 '14 at 04:54
  • It's rotating around the model center. if it's vertices are not centered on (0,0,0), you have to translate it prior to rotation by the difference betweed modelCenter and (0,0,0). So you would have 2 new translations (for and back) to get the effective model center on (0,0,0) – j-p Mar 19 '14 at 05:02
  • Back and forth (sorry for my English leaks) – j-p Mar 19 '14 at 05:15
  • hmm well my model is centred to the origin and i can get the rotation to work as intended by simply changing the order of the fourth line in your example so,glm::vec4 vTrans = trans*myRotationMatrix; makes the rotation work as intended but brakes the translation. also your english is fine! – I Phantasm I Mar 19 '14 at 05:55
  • If you compute the average of all your vertices just after uploading or setting up your model, do you realy get (0,0,0) as result? And also your model global orientation (independently from it's position) should be as expected in both case, so trans*rot and rot*trans should give the same orientation to the model, it's just modifying it's position. – j-p Mar 19 '14 at 06:22
  • i got (0,0,0) when i added up all the vertices, any other ideas? – I Phantasm I Mar 19 '14 at 07:14
  • glm::vec4 vTrans = trans * myRotationMatrix is not the same as glm::vec4 vTrans = myRotationMatrix * trans; (I double check that but I'm quite sure matrix * vector is different than vector * matrix) – j-p Mar 19 '14 at 07:23
  • [this confirm the order significance](http://reference.wolfram.com/mathematica/tutorial/MultiplyingVectorsAndMatrices.html) – j-p Mar 19 '14 at 07:33
  • Maybe I confuse you when saying rot*trans and trans*rot, I was pointing to Matrix (RotationMatrix * TranslationMatrix), not variables in code. – j-p Mar 19 '14 at 07:54
  • nope the problem still persists, could it be something other then that part of the code? like the way im calculating the view and projection matrix? do you want me to update the question with my newly updated code from your answer to make sure i haven't missed anything? – I Phantasm I Mar 19 '14 at 12:11
  • alright question updated, thanks for your time so far ive had this issue for so long hopefully you will be able to solve the mystery! – I Phantasm I Mar 19 '14 at 12:36
  • I don't see any mistake there, show maybe more code (proj and model matrix calculations) and some screen shots. – j-p Mar 19 '14 at 12:42
  • According to supplied code, the answer is the best available... if you wants more details, supply some screenshot and details on your Projection and view matrix calculations – j-p Mar 20 '14 at 02:26