1

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Fabian Orue
  • 193
  • 2
  • 14
  • Why is half of this indented? Is this your actual code? – flakes Sep 22 '14 at 12:32
  • What type is "matrix"? I think the problem is that you should be assigning matrix = matrix.translate(...) or matrix.rotateX(...). But we cant know till you tell use what type it is. Assuming its the glm mat4 I think this is in fact the problem. – Daniel Sep 22 '14 at 12:46
  • @Calpratt I assume it is to distinguish variable devlaration/initialisation from the algorithm. – Baldrickk Sep 22 '14 at 13:09
  • @user2303826 The niave way is to perform a translation to centre the object over `(0,0,0)`, perform the rotation and then reverse the traanslation on the object to put it back where it was. A more complex (but computationally shorter) method is to just modify your rotation transformation with the position vector. I forget exactly how to do it now (so not posting as answer), but a half decent geometry textbook will have the method listed. – Baldrickk Sep 22 '14 at 13:14
  • the matrix class, is only a glm interface, this is correctly instanced. the matrix class work fine, the problem is only with the objects that are not centered relative to the origin. – Fabian Orue Sep 22 '14 at 23:27

1 Answers1

2

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

M=inverse(inverse(M)*rotation_matrix);

[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

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • thank you very much Spektre, just one more question, when you refer to "M" (the transformation matrix of the object) you mean that the "M" should be applied translate, scale and rotation transformations? – Fabian Orue Sep 24 '14 at 12:04
  • @user2303826 added edit1 to answer. I do not use sequence of glTranslate,glRotate per each frame/object instead I have accumulated transform M for each object, if I want to move it then I am adding direction vectror*step to its origin once if I want to rotate then I rotate it once ... – Spektre Sep 24 '14 at 14:35
  • @user2303826 but this way I have to ensure orthonormality once in a while to not loose precision, so I take Z axis make it length =1 then compute cross product between x,z -> y and y,z -> x ... and also make them unit vectors ... this way z axis is the same (mine main axis) as was and x,y are aligned so they are perpendicular to eachother – Spektre Sep 24 '14 at 14:42