Question

I am attempting to calculate the unit vector which points out of my opengl camera. Given a rotation quaternion (w,x,y,z), how would I calculate the rotation of a unit vector around that quaternion?

In this case, the unit vector can be limited to (0,0,-1).

EDIT: Final solution

For rotation of (0,0,1):

vec.x=2*x*z - 2*y*w;
vec.y=2*y*z + 2*x*w;
vec.z=1 - 2*x*x - 2*y*y;

Note that the matrix needs to be transposed for use with OpenGL.

Was it helpful?

Solution

Convert the quaternion to a 3x3 rotation matrix and apply this rotation to your vector.

For a unit (w, x, y, z) quaternion, this matrix is:

      ( 1 - 2 * ( y * y + z * z )      2 * ( x * y - z * w )      2 * (x * z + y * w ) )
  R = (     2 * ( x * y + z * w )  1 - 2 * ( x * x + z * z )      2 * (y * z - x * w ) )
      (     2 * ( x * z - y * w )      2 * ( y * z + x * w )  1 - 2 * (x * x + y * y ) )

If your vector has such a simple form as (0, 0, -1), you will not need to compute all the 9 coefficients of the rotation matrix since the result of the matrix vector multiplication only uses some of the coefficients (the last column of R).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top