# How to calculate a 3x3 rotation matrix from 2 direction vectors?

I've got 2 direction vectors, for the X axis and the Y axis of the object. How do I calculate the rotation matrix of the object using these?

The basic idea is to use a cross product to generate the extra orthogonal axes of your rotation matrix, based upon the axes that you already have.

Matrix3x3 MakeMatrix( Vector3 X, Vector3 Y )
{
// make sure that we actually have two unique vectors.
assert( X != Y );

Matrix3x3 M;
M.X = normalise( X );
M.Z = normalise( cross_product(X,Y) );
M.Y = normalise( cross_product(M.Z,X) );
return M;
}


Note that the above does not make assumptions about X and Y vectors (apart from them not being identical), and does a lot of extra math that it might not have to do in your situation.

For example, in this code I'm doing a second cross-product to be sure our matrix gets an orthogonal Y axis, instead of blindly trusting that the input X and Y axes are precisely 90 degrees apart. If in your situation you are sure that your input axes really are orthogonal to each other, then you can skip the second cross product, and just assign the input Y vector directly, instead of recalculating it.

Note that I'm assuming that your matrix representation has accessible 'X, Y, Z' vector members. Some implementations just expose an array of nine floats instead, in which case the 'X' vector will be either elements 0, 1, and 2, or 0, 3, and 6, depending upon whether the matrix is row-major or column-major. In this (annoying) situation, I usually find that it's easier to just try both ways and see which one works, rather than to search through documentation to try to figure out which ordering your particular matrix implementation is using. :)

Finally, note that depending on your 3D coordinate system's handedness, you may need to multiply M.Z by negative one, in order to generate a legal rotation matrix for your 3D engine.

• Thank you very much! I'll do this and let you know my results. – Robinicks Nov 23 '11 at 4:02
• Thank you for the very helpful answer! I'm curious, though: isn't the X vector always elements 0, 1, and 2 regardless of the matrix being row- or column-major? 0, 3, and 6 would imply accessing the first column of a row-major matrix, or vice-versa. – fbrereto Jun 13 '12 at 23:02
• +1 for at least assert()ing that the two vectors are not equal, but I think you should also require that they are not zero vectors. – hans_meine Jul 10 '14 at 19:06
• Note that this does not take into account a case where X is -Y. – Rafał Cieślak Sep 15 '16 at 17:29