how to rotate all objects by their own centers and then translate them to the real position (it isn't working)

我的梦境 提交于 2019-11-27 07:32:44

问题


What is the way to make all the objects that are not aligned with the origin center (vector3(0.0f,0.0f,0.0f)) , rotate about its own central axis?

the problem in pseudo code:

vector3 vector3 objectCenter = (10,5,0); // current object center
vector3 vector3 objectPosition = (40,5,0); // Place to translate the object
vector3 objectRotation; = 45.0f;

    matrix.loadIdentity ();
    matrix.translate (objectCenter);

    //apply transformations
    matrix.rotateX (objectRotation);

    matrix.translate (-objectCenter);

    //itś work correctly until here
    //when i try to translate the object to the real position, the rotation is incorrect.

    matrix.translate (objectPosition);

I use C++, glm (to matrix manage) and OpenGL.


回答1:


if you want to rotate object locally to its own coordinate system then do this:

M=inverse(inverse(M)*rotation_matrix);
  • M is your object transform matrix
  • rotation_matrix is any rotation (glRotate())
  • inverse is function that computes the inverse matrix you can use mine inverse matrix computation or this rotation around LCS x (lrotx) implementation in C++ (at the bottom of the answer)

[edit1] more about relation between M and object which coordinate system it represents

look here: transform matrix anatomy

M origin is usually the middle of object (or point which is center of rotation movements). Axises of M are usually aligned with object for example in airplanes the X axis is usually the fly direction. I am more used to:

  • +z axis as forward direction movement
  • +x as right, -x as left
  • +y as up and -y as down

pith,yaw,roll are then object local rotations around x,y,z



来源:https://stackoverflow.com/questions/25974144/how-to-rotate-all-objects-by-their-own-centers-and-then-translate-them-to-the-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!