0

I have an object which I'm trying to apply some transformation. For example, with something like an airplane. If I translate the object, it translates normally. However, if I try rotating it at its current position after translating it, it still rotates around the initial origin.

I don't really know if the order of my transformation matrix is the issue.

m_Model = mat4(1.f);

//m_Model = translate(m_Model, -this->position);
m_Model = rotate(m_Model, radians(this->rotation.x), vec3(1.f, 0.f, 0.f));
m_Model = rotate(m_Model, radians(this->rotation.y), vec3(0.f, 1.f, 0.f));
m_Model = rotate(m_Model, radians(this->rotation.z), vec3(0.f, 0.f, 1.f));
m_Model = translate(m_Model, this->position);
m_Model = glm::scale(m_Model, this->scale);    
Kyle
  • 3,935
  • 2
  • 30
  • 44
  • 1
    _i dont really know if the order of my transformation matrix is the issue._ It is in fact. Matrix transformations are not commutative (except it were only translations or only scalings). Rotation and scalings happen about origin always. Hence, if something has to be rotated about any other center in world, you translate it to the origin (negative center), rotate it, and translate it back to its position (center). – Scheff's Cat Apr 27 '21 at 16:27
  • Concerning the order of transformations, I once fiddled with image transformations. (The principle is the same but it's one dimension less, of course.) Maybe, it helps to understand the issue: [SO: Rotate an image in C++ without using OpenCV functions](https://stackoverflow.com/a/56985104/7478597) – Scheff's Cat Apr 27 '21 at 16:35
  • Thank u sir.... But i wanna ask how do i translate to origin , rotate it and translate it back to its position with opengl – Oracle247 Aug 04 '21 at 10:37
  • Assuming the original position of the model is pos. If you want to rotate locally (about center pos), just rotate(axis, angle). If You want to orbit around origin, then translate(-pos), rotate(angle), translate(pos). In matrices: Mmodel' = Mmodel * Mrot, or Mmodel' = Mpos * Mrot * MposInv * Mmodel. (MposInv is the inverse matrix of Mpos but it is as well the translation matrix for -pos. -- Inverse matrix is a rather expansive operation in general.) – Scheff's Cat Aug 04 '21 at 12:56
  • I must admit that I often (but not always) struggle with the correct order as well as the signs. Hence, after writing the expressions, I test them with a simple example where I can follow the numbers "by eyes". ;-) – Scheff's Cat Aug 04 '21 at 12:57
  • To "read" a matrix (which contains trans./rot. only) is not that complicated if you know the trick: The last column is just the position (and a 1). The first three columns are just the rotated 3 unit axes x, y, z (and a 0). So, for testing, you should limit it to one plane, say the x-y plane. Then, with a little bit practice... It's not that complicated than it looks at the first glance... :-) (Actually, this still works if the matrix contains scaling, shearing and projection as well, but I would start with trans./rot. first.) – Scheff's Cat Aug 04 '21 at 13:00
  • Am still a bit confused sir, i dont know if it will better that i show u the entire code mayb on github or smth... – Oracle247 Aug 05 '21 at 23:02
  • Am actually building an airplane simulator but am stuck with this rotation and translation issue....and am using the library glm for all my translation so i dont really have in-depth knowledge abt matrix....so i think it will be more helpful if u could show me a simple example with a translating and rotating a 3D object so it can work d way i desire it to....thank u sir i really appreciate – Oracle247 Aug 05 '21 at 23:06
  • One I have at hand: [How do I rotate my camera correctly in my 3D world?](https://stackoverflow.com/a/60576857/7478597) – Scheff's Cat Aug 06 '21 at 05:54
  • Sir i think this is a bit different...this has to do with camara transformation....i really dont have issues with that....my issue is to apply transformation on the object... So it can rotate around its own center and translate towards its foward vector – Oracle247 Aug 08 '21 at 15:31
  • Actually, there is not much difference between camera transformation and object transformation. Please, note that the camera pos./ori. of the first person view is shown in the bottom right top-down view as red arrow. – Scheff's Cat Aug 08 '21 at 18:41
  • Pls sir can u drop a platform i can chat u privately..? – Oracle247 Aug 15 '21 at 23:17

0 Answers0