# quaternion rotation issues

• 01-15-2013
quaternion rotation issues
Hi. I am using quaternions to do vector rotation. For some reason it is not working properly. When I try to rotate the vector 0,0,1 90 degrees along the x axis with this quaternion-0.70710678, 0, 0, 0.70710678(with the last number being w) the output is 0, -1,
-0.207107 which should be 0,-1,0. Here is my code for the vector rotation function.
Code:

```ScePspFVector3 multvector(float quaternion[4], ScePspFVector3 vector) {     float vectorQuaternion[4],resultQuaternion[4];     ScePspFVector3 resultVector;     vectorQuaternion[0] = vector.x;     vectorQuaternion[1] = vector.y;     vectorQuaternion[2] = vector.z;     vectorQuaternion[3] = 0.0f; float inverseQuaternion[4] = {-quaternion[0],-quaternion[1],-quaternion[2],quaternion[3]};     mult(resultQuaternion,quaternion, vectorQuaternion);     mult(resultQuaternion,resultQuaternion, inverseQuaternion);     resultVector.x = resultQuaternion[0];     resultVector.y = resultQuaternion[1];     resultVector.z = resultQuaternion[2];     return resultVector; }```
What am I doing wrong?
• 01-16-2013
Shakti
Perform the calculation by hand first, with pen and paper (and check to make sure you get the correct result). Then step through the code with a debugger and check where you get the wrong values.
• 01-16-2013
Shakti
Just hit me when i was preparing some dinner (yah yah my mind is weird sometimes...). I'm almost willing to bet money the problem is the second mult, where resultQuaternion works as both input and output variable. If you modify the output before completely calculating it, i.e. doing something like this
Code:

```mult(output, lhs, rhs)     output[0] = // stuff depending on lhs and rhs     output[1] = // stuff depending on lhs and rhs```
by the time you calculate output[1] you will have modified lhs aswell.

This can cause some trouble for you.
• 01-16-2013
BobMcGee123

EDIT:
My guess is you've made a mistake in Mult, also possibly not normalizing in the right location.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The code below is the Quaternion implementation from Octave 3.2.4 (basically open source MatLab). The syntax is simple so hopefully the language difference won't be a problem as this is a math question. I removed the error checking portions from the code but attached the full source in a zip file for you.

Code:

```function v = qtrans (v, q)   v = qmult (q, qmult (v, qinv (q))); endfunction```
Code:

```function retval = qmult (a, b)   [a1, b1, c1, d1] = quaternion (a);   [a2, b2, c2, d2] = quaternion (b);     ri = b1*c2 - c1*b2 + d1*a2 + a1*d2;   rj = c1*a2 - a1*c2 + d1*b2 + b1*d2;   rk = a1*b2 - b1*a2 + d1*c2 + c1*d2;   rr = -(a1*a2 + b1*b2 + c1*c2) + d1*d2;     retval = quaternion (ri, rj, rk, rr); endfunction```
Code:

```function retval = qinv (q)   if (norm (q) != 0)     retval = qconj (q) / sum (q .* q);   else     error ("qinv: zero quaternion passed!");   endif endfunction```
Code:

```function retval = qconj (q)   [a, b, c, d] = quaternion (q);   retval = quaternion (-a, -b, -c, d); endfunction```
• 01-16-2013
Here is my mult function. It does not normalize any quaternions.
Code:

```void mult(float* C,float A[], float B[]) {   C[0] = A[3]*B[0] + A[0]*B[3] + A[1]*B[2] - A[2]*B[1];   C[1] = A[3]*B[1] + A[1]*B[3] + A[2]*B[0] - A[0]*B[2];   C[2] = A[3]*B[2] + A[2]*B[3] + A[0]*B[1] - A[1]*B[0];   C[3] = A[3]*B[3] - A[0]*B[0] - A[1]*B[1] - A[2]*B[2]; }```
• 01-16-2013
Shakti
See my second post. When C and A point to the same array you will modify A when you write to C thus corrupting your calculations.
• 01-17-2013
BobMcGee123
Yeah, there's that too, which I managed to totally miss and is probably your problem :)

Code:

```    mult(resultQuaternion,quaternion, vectorQuaternion);     mult(resultQuaternion,resultQuaternion, inverseQuaternion);```