Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

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.

share|improve this answer
    
Thank you very much! I'll do this and let you know my results. –  Jarvis 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.