# Rotating a quaternion around its z-axis to point its x-axis towards a given point

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!

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).

• Thank you, i can calculate a quaternion about the z-axis with the x-axis pointing towards a given point. How can i use this to rotate an already existing quaternion about the z-axis? – user102937 May 2 '17 at 8:04