1
$\begingroup$

Today I coded the multiplication of quaternions and vectors in Java. This is less of a coding question and more of a math question though:

Quaternion a = Quaternion.create(0, 1, 0, Spatium.radians(90));
Vector p = Vector.fromXYZ(1, 0, 0);
System.out.println(a + " * " + p + " = " + Quaternion.product(a, p));
System.out.println(a + " * " + p + " = " + Quaternion.product2(a, p));

What I am trying to do is rotate a point $\mathbf{p}$ using the quaternion $\mathbf{q}$. The functions product() and product2() calculate the product in two different ways, so I am quite certain that the output is correct:

(1.5707964 + 0.0i + 1.0j + 0.0k) * (1.0, 0.0, 0.0) = (-1.0, 0.0, -3.1415927)
(1.5707964 + 0.0i + 1.0j + 0.0k) * (1.0, 0.0, 0.0) = (-1.0, 0.0, -3.1415927)

However, I can't wrap my head around why the result is the way it is. I expected to rotate $\mathbf{p}$ 90 degrees around the y-axis, which should have resulted in (0.0, 0.0, -1.0).

Wolfram Alpha's visualization also suggests the same: https://www.wolframalpha.com/input/?i=(1.5707964+%2B+0.0i+%2B+1.0j+%2B+0.0k)

So what am I doing wrong here? Are really both the functions giving invalid results or am I not understanding something about quaternions?

$\endgroup$
1
$\begingroup$

I dont well understand your code, but it seems that you have multiplied only one way the quaternion by the vector, and this is wrong.

The rotation of the vector $\vec v = \hat i$ by $\theta=\pi/2$ around the axis $\mathbf{u}=\hat j$ is represented by means of quaternions as ( see Representing rotations using quaternions):

$$ R_{\mathbf{u},\pi/2}(\vec v)= e^{\frac{\pi}{4}\hat j}\cdot \hat i \cdot e^{-\frac{\pi}{4}\hat j} $$ where the exponential is given by the formula ( see Exponential Function of Quaternion - Derivation): $$ e^{\frac{\pi}{4}\hat j}=\cos\frac{\pi}{4} +\hat j \sin\frac{\pi}{4} $$ so we have: $$ R_{\mathbf{u},\pi/2}(\vec v)=\left( \frac{\sqrt{2}}{2}+\hat j\frac{\sqrt{2}}{2} \right)(\hat i) \left(\frac{\sqrt{2}}{2}-\hat j\frac{\sqrt{2}}{2} \right)= $$ $$ =\frac{1}{2}\hat i-\frac{1}{2}\hat k-\frac{1}{2}\hat k-\frac{1}{2}\hat i=-\hat k $$

Note we need two multiplications by a unitary quaternion and its inverse, with an angle that is one half of the final rotation angle.

$\endgroup$
  • $\begingroup$ I have one question about all that though: Given that p' = q * p * q^(-1), is the quaternion q actually the way that quaternions are meant to be stored? I was told that the real part of a quaternion represents the angle and the imaginary part the axis of the rotation. So is q just some kind of special form you have to convert everything into before applying rotation? $\endgroup$ – Jan Schultke Oct 27 '16 at 23:08
  • 1
    $\begingroup$ @JanSchultke Whoever told you that may have meant that the real part is uniquely determined by the angle of rotation and that the vector part has the same direction as the rotation axis, but the formula given in this answer is the correct and full expression for the quaternion. $\endgroup$ – Muphrid Oct 28 '16 at 15:02
  • $\begingroup$ @JanSchultke Also observe that quaternions of the form $q=e^{\alpha \hat u}$ used here are unit (norm) quaternions, so you always get their inverse $q^{-1}$ as the conjugate quaternion (=flip the signs of all the terms other than the real part). So $q^{-1}=e^{-\alpha\hat u}$. $\endgroup$ – Jyrki Lahtonen Oct 28 '16 at 19:19
  • $\begingroup$ @JanSchultke: A 3D rotation is identified by the three components of a unit vector $\mathbf{ u}$ that orients the axis of rotation and by a number $\theta$ that is the angle of rotation (usually positive oriented counterclockwise). These four numbers are not the components of the quaternion that can be used to perform this rotation. As you can see from my answer the quaternion that represents the rotation can be represented in polar form as $e^{\mathbf{u}\theta/2}=\cos \theta/2 +\mathbf{u}\sin \theta/2$, and this identify the quaternion and its inverse. $\endgroup$ – Emilio Novati Oct 28 '16 at 19:24
0
$\begingroup$

The short answer is that you have to conjugate by a half-angle quaterion instead of simply multiplying to effect a rotation. See this question or this Wikipedia article for details.

$\endgroup$

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.