The inverse of
q is not
-q/magnitude(q), that is completely wrong.
Rotations with quaternions imply that these 4D complex number equivalents have unitary norm, hence lie on the S3 unit sphere in that 4D space. The fact that a quat is unitary means that its norm is
norm(q)^2=q*conjugate(q)=1 and that means that the quat's inverse is its conjugate.
If a unit quaternion is written as
q=(w,x,y,z) = (cos(t),sin(t)v), then its conjugate is
conjugate(q)=(w,-x,-y,-z)=(cos(t),-sin(t)v), where t is half of the rotation angle and v is the rotation axis (as a unit vector, of course).
When that Hamilton dude decided to play around with complex number equivalents in higher dimensions, he also stumbled upon some nice properties. For example, if you employ a completely pure quaternion
q=(0,x,y,z) (no scalar part w !), you can consider that crap as being a vector (it's actually a quat on what people might call the equator of the S3 sphere, which is an S2 sphere!! - mind bending stuff if we consider how technically impaired the people in the 19th century seem to us eyePhone cowboys nowadays). So Hamilton took that vector in its quat form:
v=(0,x,y,z) and did a series of experiments considering the geometric properties of quats.. Long story short:
INPUT: _v=(x,y,z)_ a random 3D vector to rotate about an __u__ unit axis by an angle of _theta_
q = (cos(theta/2), sin(theta/2)*u)
conjugate(q) = inverse(q) = (cos(theta/2), -sin(theta/2)*u)
Observation: the q*(0,v)*conj(q) has to be another quat of the form (0,v'). I won't go through all that apparently complicated explanation of why this happens, but if you rotate a pure imaginary quaternion (or a vector in our case!) through this method,you must get a similar kind of object: pure imaginary quat.. and you take its imaginary part as your result. There you have it, the wonderful world of rotations with quaternions in a nut(ty)shell.
NOTE: to whomever jumps in with that overused phrase: quats are good because they avoid 'em gimbal lock.. should unlock their imagination first!! Quats are a mere "elegant" mathematical apparatus and can be avoided altogether by using other approaches, the one I find completely geometrically equivalent being the axis angle approach.
CODE: the C++ library I fancy is rather simplistic, but has all the matrix, vector and quat operations a 3D graphics experimentalist should need without having to waste more than 15 minutes to learn it.. You can test the things I wrote here using that in 15 minutes if you're not a C++ novice. Good luck!