# Angle between two quaternions

I am struggling to get my head around what I believe should be a simple problem.

I am getting a quaternion wxyz from an IMU (inertial measurement unit), I want to check the angle between the quaternion and a given unit vector.

I am very new to understanding quaternions but from doing research I understand the steps to be as follows

• Let $$A$$ be the quaternion from the IMU
• Let $$B$$ be the quaternion created from my target vector but with a scalar (W) of zero.
• Calculate $$C = A*conj(B)$$
• Calculate $$\theta = 2 * atan2(norm(C_{xyz}),C_w)$$

This appears to almost work but is sensitive to rotation of the axis not just the direction it is pointed at. I feel this kind of makes sense based on the result C being a transformation to get from A to B, but I don't know how to remove one axis of sensitivity in my result.

• Welcome to the site. It would be better if you could type your formulas in MathJax, see here for a tutorial: math.meta.stackexchange.com/questions/5020/… Feb 22, 2022 at 14:07
• Sorry, an IMU is an inertial measurement unit. So to get the number I am after is it more a case of I should be somehow extracting a vector from the quaternion and going from there or should I be applying some extra operations to get rid of the axis I don't want to compare. Feb 22, 2022 at 14:31
• I am still baffled. Exactly what angle are you supposed to calculate. We can define angles in 4D space, but somehow I suspect that you are looking for an angle between two 3D-vectors. Feb 22, 2022 at 14:35
• Your $C$ is another quaternion. It describes the rotation you get by first doing a 180 degree rotation about the vector yielding the quaternion $B$ followed by the rotation from the IMU. That combined rotation then has an axis and an angle of rotation. $\theta$ seems to be angle of that combined rotation. Is this the angle you wanted to calculate? Feb 22, 2022 at 14:41
• Big picture what I am trying to figure out is how to take the quaternion spat out by my IMU and a vector and figure out how well they are aligned not caring about rotation about the axis. You are right I am more or less trying to get back to the angle between two vectors. But I don't know how to convert my quaternion back to being a vector. Will need to pick this up in the morning, have stayed up way too late trying to crack it. Feb 22, 2022 at 14:48