Converting a direction vector to a quaternion rotation

Caustic picture Caustic · Apr 8, 2013 · Viewed 25.2k times · Source

I can find a ton of questions about turning a quaternion into a direction vector but none for the other way around which makes me think I'm doing something wrong, but bear with me.

What I'm trying to do is simply display the direction of a directional light using an arrow model.

The directional light's direction is a unit vector but models are rotated using quaternions.

So.. How do I rotate this model to match the direction of the light?

Or am I crazy and I can't really do that, given that the light has no position but the model does?

Answer

joojaa picture joojaa · Apr 8, 2013

A direction vector is not a defined rotation, it still has an infinite number of possible solutions. See there is no information for how to rotate around the axis. Two, vectors is possible as is a vector and a rotation (with a meaningful center) and in fact a fully defined matrix.

Because in your case the rotation is rather meaningless, just use the up vector for reference (that is unless your light is shining top down or vice-versa then you need another reference).

So take the cross product of your direction vector D and up vector U for the side vector S then cross D and S for a new Un. Then use D, Un, S as rows (or columns depending on how your calculation rule is set up) as a matrix. Matrix to quaternion is well known math.