How to create a Quaternion rotation from a forward- and up- vector? [closed]

I need the rotation Quaternion of an object, I have it's foward and up directions (as 3D vectors), so I thought it would be easy to create a Quaternion rotation from that, but I can't seem to get it right.

I'm sure there are multiple ways to do this. (with or without matrices for example) I just need one that always works.

I'm working in C++, directx, so I have the DirectXMath libraries available to use.

Edit:

Turns out there's an easy way to do this with the directx, as there is a function that creates the TransformationMatrix from forward and upvector.

I'm leaving this open, as I'm still interested in knowing how to do this manually. (withouth directx)

closed as unclear what you're asking by John Douma, Riccardo.Alestra, GNUSupporter 8964民主女神 地下教會, Paul Frost, RamiroMar 8 at 23:50

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• What are forward and up directions? – John Douma Mar 8 at 14:48
• If the object has no rotation, it's forward direction would be (0,0,1), and it's up direction would be (0,1,0), because the Y-axis is the global Up-axis, and the Z-axis is the global Forward-axis. At least in my case. I don't really know how to explain it better, it's a 3d programming thing I guess. – Stef Mar 8 at 14:51

You calculate “direction-left” vector as $$v_{\mathrm{l}}=v_{\mathrm{u}}\times v_{\mathrm{f}}$$. Then by writing your vectors as three columns with the order forward—left—up, you obtain a rotation matrix $$M$$. Finally: $$q_0= \frac12\sqrt{1 + \mathrm{Tr}\ M},\\ q_1 = \frac{M_{32} - M_{23}}{4q_0},\qquad q_2 = \frac{M_{13} - M_{31}}{4q_0},\qquad q_3 = \frac{M_{21} - M_{12}}{4q_0}$$