# Quaternion rotation

I have a problem understanding quaternions rotation in OpenGL. So far I have implemented all function and operators related to quaternions.They are definitely working right!. However I can't find where I'm making the mistake within rotation function.

Let's say I have a a cube in 3d space at (xyz)`(1, 5, 2)` if I am not wrong the position in 4D is `p = 0 + i + 5j + 2k`, and the orientation and rotation `(0, 0, 0, 0)` in 4D space is `q = 1 + 0i + 0j + 0k`

Now I want to rotate it by 30 degrees along y-axis.

``````q = cos(a / 2) + (ix + jy + kz)sin(a/2)
``````

So 30 degrees along y-axis is `q=0.96+0.25j` now:

``````Pout = q * Pin * con(q)

Q = q * Pin = (0.96 + 0.25j) * (0 + 1i + 5j + 2k) = -1.25 + 1.46 + 4.8 + 1.67
``````

Shall I normalize it now or not?

``````conj(q) = 0.96 - 0.25j
Pout = Q * con(q) = 0 + 1.81 + 4.92 + 1.32
``````

Normalized:

``````q = 0 + 0.33i + 0.91j + 0.22k
``````

This is definitely the wrong answer according to this site

Rotating the object along the Y-axis by 30 degrees from a point `(0,0,0)` gives the correct result just by computing `q`. If I use the above algorithm and multiply it by `Pin` I get `(0,0,0,0)`

Can you show me how to do it right step by step?

Also how do I to orbit one point along another. Lets say I have the given point above and I want it to orbit around the point `(2, 10, -2)` using quaternions.

And when I manage to get the correct quaternion, how do I use it in the proper way along with openGL `glRotatef`?

So far my program runs like this:

1. Quaternion = x, y, z of object
2. Compute Rotation by X degrees along axes.
3. Quaternion to Axis Angle
4. `glRotatef` (by result of point 3)
5. Quaternion = result of point 2
6. Go to point 2
7. Go to step 1

Do I always have to change quaternions to Axis-angles, or is there another way which allow me to represent the rotation without conversion?

-
a quaternion is just an orientation. when applied to a point, and or a vector then it has meaning. like saying 37 degrees about X-axis where did your start from. then your question on orbit. define your orbit plane (or are we talking about parametric equation orbits?) I might venture a ques that your using quaternions to learn about them, and because they "sound cool". – gardian06 May 5 '12 at 18:19
If you want a working quaternion implementation, I've made my own library awhile ago to use with OpenGL - github.com/ThePiachu/Space-Combat/blob/master/Space%20Combat/… . – ThePiachu May 5 '12 at 19:25

Your computations are correct. Maybe you missed it, but here's your vector, rotated 30 degrees around the Y axis:

``````Pout=Q*con(q)=0+1.81+4,92+1.32
``````

You must not normalise the result, that's all. Note that you seem to be having precision problems, because this is what I get:

``````             v = [1 5 2]
q = [.965926 0 .258819 0]
~q = [.965926 0 -.258819 0]
q * [0 v] = [-1.294095 1.483564 4.829629 1.673033]
q * [0 v] * ~q = [0 1.866025 5 1.232051]
v' = [1.866025 5 1.232051]
``````

If you want to orbit around a given point, just subtract that point from your source point, perform the rotation usings quaternion arithmetics, and add the point back at the end. Using your values above:

``````            p1 = [1 5 2]
p2 = [2 10 -2]

v = p1 - p2 = [-1 -5 4]
q * [0 v] * ~q = [0 1.133975 -5 3.964102]
v' = [1.133975 -5 3.964102]

p1' = p2 + v' = [3.133975 5 1.964102]
``````
-

Lets say I have a a cube in a point of 3d space (x=1,y=5,z=2) if I am not wrong the position in 4D is p=0+i+5j+2k orientation and rotation (0,0,0,0) in 4D q=1+0i+0j+0k now I want to rotate it by 30 degree along y axis.

Nothing in this statement after the words "if I am not wrong" made any kind of sense.

You do not use quaternions to encode positions (unless you're using dual-quaternions and you're not). A quaternion, for the purposes of graphics, is nothing more than a fancy way to encode a rotation matrix. It is just an encoding of an orientation in space.

So there is no quaternion position.

Since you're using fixed-function rendering, if you want to apply a quaternion to the matrix stack, then you do it by generating a matrix from it and applying it to the stack with `glMultMatrix`. If you want to rotate an object's position based on a quaternion, then you `glMultMatrix` the quaternion onto the stack, then `glTranslate` the position onto the stack.

And you should never "change quaternions to Axis angle". There is never any reason to do so.

-
I have to disagree with you here. Unit quaternions are used to encode an orientation in space. Quaternions as they are are just 4D objects, and it is perfectly valid to use the `w=0` hyperplane (which is a 3D subset of the 4D set of quaternions) to encode a position. – sam hocevar May 5 '12 at 18:56
@Sam: Mathematically valid, yes. But of zero utility in this situation. Do you honestly think that the OP understands the mathematical validity of the concept? – Nicol Bolas May 5 '12 at 19:04
True, OP seems to be confused about what quaternions are useful for. But other people may read that and get the wrong idea. As for “changing quaternions to axis angle”, I wouldn't really recommend against it since it's a quick way to generate a matrix on the stack with `glRotate`. But yeah, again, probably not the best advice to OP. – sam hocevar May 5 '12 at 19:11
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}