0

Given the unit vectors of the two axes of the object's rotated coordinate system, find the rotation matrix of the object's rotation. I have seen that many tools have implemented this function.

Take Unreal as an example. The following example is the pseudo-code grabbed from Unreal source to complete the calculation of the rotation matrix from the X-Z axis unit vector.

I would like to ask how this process is done. I hope someone could explain.


FORCEINLINE TVector<T> TVector<T>::operator^(const TVector<T>& V) const
{
    return TVector<T>
        (
        Y * V.Z - Z * V.Y,
        Z * V.X - X * V.Z,
        X * V.Y - Y * V.X
        );
}

// Given X and Z axes   
const TVector<T> OldX, OldZ;

const TVector<T> NewX = OldX
const TVector<T> NewZ = OldZ

const TVector<T> NewY = NewZ ^ NewX;
const TVector<T> NewZ = NewX ^ NewY;

// Target Rotation Matrix
TMatrix<T> M(3,3);
 
M[0][0] = NewX.X; M[0][1] = NewX.Y;  M[0][2] = NewX.Z;
M[1][0] = NewY.X; M[1][1] = NewY.Y;  M[1][2] = NewY.Z;
M[2][0] = NewZ.X; M[2][1] = NewZ.Y;  M[2][2] = NewZ.Z;
    
vicky Lin
  • 150
  • 1
  • 10
  • 1
    just stick those axis vectors into a matrix, either as rows or columns (transpose to taste). if they're ortho-normal, that'll be enough (I'll assume the `^` operator is a cross product). that is literally the code you present. are you asking to be given a chapter from a linear algebra class? – Christoph Rackwitz May 05 '23 at 11:34
  • Looks like you are right! I probably should check Wikipedia for more info about rotation matrix. – vicky Lin May 05 '23 at 11:37
  • 1
    that type of matrix is simple to read. assume you're calculating M * v and v is an axis vector for the i-th axis (1,0,0 for the first axis). the result will be the i-th column from M. that means the X axis gets mapped to the first column of M. -- inverting that matrix amounts to a transposition. if you wanted to map a certain vector to _become_ the X-axis, you put it in the matrix as the first row vector. -- everything normalized of course. if it's not, results are wild. – Christoph Rackwitz May 05 '23 at 12:21
  • Great and vivid Explanation! You enlightened me! How about posting your comment as the answer? This question is solved. – vicky Lin May 05 '23 at 12:30

0 Answers0