# Rotating a point using a quaternion, then using an inverse quaternion to rotate back?

• 11-11-2011
fuse
Rotating a point using a quaternion, then using an inverse quaternion to rotate back?
I have a function that takes a unit quaternion <w,x,y,z> and a point <x,y,z> and spits out the rotated 3d point.
void rotate(const float *quaternion, const float *point, float *newpoint)

This code works OK:
void rotate(quaternion, point, newpoint)

I would now like to rotate the point back to where it was originally. I understand this is possible using the inverse of the quaternion (which for a unit quaternion is equivalent to the conjugate, right?):

This is how I do this this:
inverse_quaternion[4];
inverse_quaternion[0] = quaternion[0]; //w
inverse_quaternion[1] = -quaternion[1]; //x
inverse_quaternion[2] = -quaternion[2]; //y
inverse_quaternion[3] = -quaternion[3]; //z
void rotate(inverse_quaternion, rotated_point, final3dpoint)

Unfortunately this is not doing what I expect. What am I missing?
• 11-11-2011
bernt
What you have will only work if the quaternion is normalized, otherwise you've got to multiply by 1/[magnitude squared] (or 1/[Q dot Q]):
Code:

```float dot = quaternion[0]*quaternion[0] + quaternion[1]*quaternion[1] + quaternion[2]*quaternion[2] + quaternion[3]*quaternion[3]; inverse_quaternion[0] = quaternion[0] / dot; //w inverse_quaternion[1] = -quaternion[1] / dot; //x inverse_quaternion[2] = -quaternion[2] / dot; //y inverse_quaternion[3] = -quaternion[3] / dot; //z```
And if this isn't the problem, could you post your code for rotate()? It can be hard to catch mistakes in all the similar-looking arithmetic involved in this sort of stuff.
• 11-11-2011
fuse
Ok, I modified my multiplication code and now it works out (sans some small math errors. Hmm, guess I better figure out what's going on there! When mapped, the z and y axis are correct, but the x axis appears to be inversed. ;)
• 11-14-2011
VirtualAce
Quote:

Originally Posted by fuse
Ok, I modified my multiplication code and now it works out (sans some small math errors. Hmm, guess I better figure out what's going on there! When mapped, the z and y axis are correct, but the x axis appears to be inversed. ;)

This sounds like a problem with your world matrix or the way you are constructing it from the up, right, and look vectors.