1. ## 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, ScePspFVector3 vector)
{
float vectorQuaternion,resultQuaternion;
ScePspFVector3 resultVector;
vectorQuaternion = vector.x;
vectorQuaternion = vector.y;
vectorQuaternion = vector.z;
vectorQuaternion = 0.0f; float inverseQuaternion = {-quaternion,-quaternion,-quaternion,quaternion};
mult(resultQuaternion,quaternion, vectorQuaternion);
mult(resultQuaternion,resultQuaternion, inverseQuaternion);
resultVector.x = resultQuaternion;
resultVector.y = resultQuaternion;
resultVector.z = resultQuaternion;
return resultVector;
}```
What am I doing wrong? 2. 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. 3. 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 = // stuff depending on lhs and rhs
output = // stuff depending on lhs and rhs```
by the time you calculate output you will have modified lhs aswell.

This can cause some trouble for you. 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``` 5. Here is my mult function. It does not normalize any quaternions.
Code:
```void mult(float* C,float A[], float B[])
{
C = A*B + A*B + A*B - A*B;
C = A*B + A*B + A*B - A*B;
C = A*B + A*B + A*B - A*B;
C = A*B - A*B - A*B - A*B;
}``` 6. 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. 7. 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);``` Popular pages Recent additions [code] 