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?
Answer
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.
No comments:
Post a Comment