0
\$\begingroup\$

I m having hard time to understand Quaternions and to use them in my engine for rotating object. I m looking for step by step explanation, actually a correction of my view of Quaternions.

Here is how I understand they work.

q = cos(40*) + sin(40)(i+j+k)

->what I understand is that i+j+k represents the axis of rotation. Basically if i = 1 that means we rotate object around X-axis.

Formula q * point * p-1, is what I dont quite understand. Lets for the sake say I understand why forumla goes like this. What I dont seems to get how would I apply this to my engine to rotate object.

What I tried is to create 3dVector with object position, example (0,0,-5). I would use quaternion that would rotate this vector around y axis, so q * ThisVector * q(conjugate), and then result I would put in translate matrix. But no work. I cant seem to find a way to implement them. I saw some articles on quaternion to matrices, But what do I put in that matrix, a result from qThisVectorq(conjugate) or it is just q that is transfromed in matrix?

EDIT What I want is to rotate object on Y-axis using quaternion. So far,because of lack of my understanding, I m failing to rotate. I created a class 3dVector ->three dimensional vector, and instantiate a new position (0,0,-5). This position I sent as translate matrix to opengl and multiplied with glPosition.

When I tried to rotate that object using quaternion. I just took that position (0,0,-5) and used it in formula q = q * p * q(conjugate). I manually calculated that but result I get didnt make object rotate on Y-axis around itself. Now I m not sure, but I think it actually rotated object but not around him self,he was moved to the right, so he was not in the same position anymore

EDIT_2:

I have cube that is on position [0,0,-5] and I want to rotate it around y-axis by some angle.

I tried to write code in python

poinToRotate = Quaternion(w=0,x=0,y=0,z=-5)

angle =45

sinus = math.sin(math.radians(angle));
q=Quaternion(
w=math.cos(math.radians(angle)),
x=sinus*0,
y=sinus*1,
z=sinus*)

newPoint = q.rotate(poinToRotate)

Rotation is somewhere good but it cube does not rotate around it self but around some point. But when

   p = Quaternion(w=0,x=0,y=0,z=0)

to make it to the center, result is all zeros.

\$\endgroup\$
4
  • \$\begingroup\$ "it is just q that is transfromed in matrix" Just the quaternion. Those are two separate ways of transforming a vector by a quaternion (1. converting to matrix, then multiplying by it, OR 2. "q * ThisVector * q(conjugate)"). \$\endgroup\$ – HolyBlackCat Jan 31 at 22:08
  • \$\begingroup\$ "But no work" isn't a lot for us to go on. What specific steps do you perform, what numeric results do you expect, and what results do you get instead? \$\endgroup\$ – DMGregory Jan 31 at 23:33
  • 1
    \$\begingroup\$ Your edit does not make it clearer to me what you tried and how the specific outcome differed from what you want. Please share the specific code and exact numerical inputs and outputs, compared to the values you expect. You can also share screenshots indicating the visual difference between the outcome you get and what you expect. \$\endgroup\$ – DMGregory Feb 1 at 0:34
2
\$\begingroup\$

Explaining why quaternions work the way they do and their relation to mainstream linear transformation rotations would require some more advanced mathematics knowledge regarding group theory and how it applies to complex numbers and matrices. For your needs however what you need to know is the following:

  • Quaternions are 4-dimensional (w,x,y,z) objects with some interesting properties which we can abuse in order to do rotations in 3-dimensional space (x,y,z).
  • A 3-dimensional vector or point in space can be represented as a quaternion if we create a quaternion where \$w=0\$ and we set the rest of \$x,y,z\$ to the point/vector coordinates
  • The above hacky quaternions have the following property: if a quaternion \$p\$, that represents a point in 3D space, is multiplied by another quaternion and its conjugate in a sandwich \$q*p*q^{-1}\$ a new quaternion results from this operation that also represents a point in 3D space, specifically one that has been rotated by using the following linear transformation matrix $$R=\begin{pmatrix} 1-2y^2-2z^2 & 2xy-2zw & 2xz+2yw \\ 2xy+2zw & 1-2x^2-2z^2 & 2yz-2xw \\ 2xz-2yw & 2yz+2xw & 1-2x^2-2y^2 \end{pmatrix}$$ where the \$w,x,y,z\$ values represent the values of the \$q\$ quaternion.
  • The above rotation transformation matrix represents a rotation around the axis represented by \$x,y,z\$ with angle \$2*cos^{-1}(w)\$

To summarize, if you have a 3D point \$x,y,z\$ that you want to rotate by angle \$θ\$ around the axis represented by vector \$[a,b,c]\$ using quaternions, what you need to do is:

  • Create a quaternion \$p=0 + xi + yj + zk\$
  • Create a quaternion \$q=cos(θ/2) + ai + bj + ck\$ where \$cos(θ/2)^2 + a^2 + b^2 + c^2 = 1\$ i.e. a unit quaternion. If your axis vector \$[a,b,c]\$ is a unit vector then it is easier to do \$q=cos(θ/2) + sin(θ/2) * (ai + bj + ck)\$
  • Do the sandwich multiplication \$p'=q*p*q^{-1}\$
  • Extract \$x',y'\$ and \$z'\$ from \$p'\$ and you now have the rotated 3D point \$(x', y', z')\$
\$\endgroup\$
5
  • \$\begingroup\$ Dont know what to say, amazing, thank you so much for dumbing it down and explaining it so clearly. \$\endgroup\$ – Filip Cacic Feb 3 at 23:03
  • \$\begingroup\$ Just one more question, when I use it like that, my object rotates around some imaginary center, it does not rotate around itself, meaning he is not staying on the same position. \$\endgroup\$ – Filip Cacic Feb 3 at 23:43
  • \$\begingroup\$ The object rotates around an axis which is represented as a vector. That vector starts from (0,0,0) and points to (x,y,z). If you want the object to rotate around itself, the axis vector should then start from (0,0,0) as all vectors do and point to coordinates (x,y,z) that correspond to the object's center. You decide where exactly in your object that is and it depends on its position in the 3D space. For example if you have a sphere with its center at (1,1,1) then your rotation axis would be the vector [1,1,1]. \$\endgroup\$ – PentaKon Feb 4 at 8:41
  • \$\begingroup\$ I m so sorry, i reall appreciate the help, but I just cant seem to understand, I guess I m missing lot of fundementals. Let say I have a cube that is in position [0,0,-5] and want to rotate it by 45 degrees? What I did at first was, axis quaternion =[cos(45/2),0,1,0], I wanted to rotate around y axis. I multiplied that qutaernion with position[0,0,-5] and with conjugate. Result was object was completely moved from its origin. Now I cant understand If make my quaternion axis equals [0,0,-5], how is that going to rotate him on y-axis \$\endgroup\$ – Filip Cacic Feb 4 at 11:42
  • \$\begingroup\$ The behavior you describe is correct. The rotation axis [0,1,0] is essentially the y axis of your 3D coordinate system, not the cube's y axis! Which is why the cube moves. It rotates around the "coordinate system y" if you will. In order to rotate around itself, mathematically what you do is first apply a translation transformation to the cube and move its center to [0,0,0] then rotate it around y as normal then apply a second translation to move it back to [0,0,-5]. \$\endgroup\$ – PentaKon Feb 4 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.