# How do you rotate a vector by a unit quaternion?

Given a 3-variable right-handed vector v that is a translation measured in local space and a unit quaternion representing an orientation from local to world space, how do you use the quaternion to rotate the vector from local space to world space?

For ease of use, the values are:

Vector v = $[1.0, 0.0, 0.0]$

Quaternion $q = [W: 0.7071068, X: 0, Y: 0.7071068, Z: 0]$, which I understand to be a rotation $90^\circ (\frac{\pi}{2})$ around the $Y$-axis and which converts from the local space to the world space. (That is, the resulting vector is $[0.0, 0.0, 1.0]$, and if this was the nose of a spaceship, it’d be pointing to the right in world coordinates)

Thanks.

To answer the question simply, given:

 P  = [0, p1, p2, p3]  <-- point vector
R  = [w,  x,  y,  z]  <-- rotation
R' = [w, -x, -y, -z]


For the example in the question, these are:

 P  = [0, 1, 0, 0]
R  = [0.707, 0.0,  0.707, 0.0]
R' = [0.707, 0.0, -0.707, 0.0]


You can calculate the resulting vector using the Hamilton product H(a, b) by:

 P' = RPR'
P' = H(H(R, P), R')


Performing the calculations:

        H(R, P)      = [0.0, 0.707, 0.0, -0.707]
P' = H(H(R, P), R') = [0.0, 0.0,   0.0, -1.0  ]


Thus, the example above illustrates a rotation of 90 degrees about the y-axis for the point (1, 0, 0). The result is (0, 0, -1). (Note that the first element of P' will always be 0 and can therefore be discarded.)

For those unfamiliar with quaternions, it’s worth noting that the quaternion R may be determined using the formula:

a = angle to rotate
[x, y, z] = axis to rotate around (unit vector)

R = [cos(a/2), sin(a/2)*x, sin(a/2)*y, sin(a/2)*z]


See here for further reference.