[Math] Rotating a quaternion around its z-axis to point its x-axis towards a given point

linear algebramatricesquaternionsrotationsvectors

How can i rotate a quaternion around its z-axis in order to make the x-axis point towards a certain (x,y)-point?

I have been able to calculate quaternions by rotating a unit vector [0,0,1] onto another unit vector, thus getting the direction along the z-axis i need. Now i would like to rotate that quaternion further to make its x-axis point (in the x-y plane) towards a given (x,y)-point.

I used Jur van den Berg's method from Calculate Rotation Matrix to align Vector A to Vector B in 3d?

I hope you can help, thank you!

Best Answer

The axis would be $[0,0,1]$.

The angle would be $\theta = \texttt{atan2}(y, x)$.

Then the quaternion would be $q = \left[ \cos\frac{\theta}{2}, \sin\frac{\theta}{2}[0,0,1]\right]$.

Or in your case $q = \left[ \cos\frac{\theta}{2}, 0, 0, \sin\frac{\theta}{2}\right]$

EDIT:

To use this to rotate another quaternion $r = [0, x, y, z]$, just use the following formula:

$$r_{rot} = qrq^{*}$$

Where $q^*= \left[ \cos\frac{\theta}{2}, 0, 0, -\sin\frac{\theta}{2}\right]$ is the conjugate of $q$ (i.e. the vector part is negated).