简体   繁体   中英

3D rotation, given two specified directions

I have two perpendicular unit vectors, p and q; which I think of as being embedded in some rigid object in 3D space. I wish to rotate the object so that p now points along the positive x-axis and q points along the positive y-axis.

I am programming in c# and using quaternions to compute rotations. How do I create a quaternion which will perform the desired rotation?

If you imagine the inverse operation of your quaternion, then you know that point (1,0,0) goes to p, and (0,1,0) goes to q. Similarly, point (0,0,1) will go to a point z equal to the cross product of p and q.

Therefore you actually have the matrix representation of your inverse rotation. The first column is p, the second q, the third p cross q.

So the matrix for the rotation you want is given by the transpose of this matrix, ie the first row is p, the second row is q, the third row is p cross q.

So I recommend you simply call a library routine to convert from this matrix to the quaternion form.

You may be interested in this document about Doom3 explaining the algorithm Id software used for converting matrix to quaternion - including optimised x86 assembler code.

Orthogonal matrices

If we have a matrix M with columns (a,b,c) forming an orthonormal set, then we know that a'a = b'b = c'c = 1, a'b = ... = c'a = 0.

Therefore M'M = I.

If we multiply on the left by M, and on the right by inv(M) we find:

M.M'.M.inv(M) = M.inv(M)
=> M.M'.(M.inv(M)) = M.inv(M) = I
=> M.M' = I

And so, if we have orthonormal columns, we must necessarily also have orthonormal rows, and the matrix M is called an orthogonal matrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM