1

I'm learning Unity and came across a situation where rotations are represented as Quaternions. I've heard that they where used in computer graphics, but never had to use them until now. What I can't understand is, how do quaternions represents rotations in the three dimensional space?

I know that complex numbers represents rotations in the 2 dimensional space (such as multiplying a number by $i$ would move it by $\frac \pi 2$). This seems logical in the sense that $\Bbb C$ has two unit vectors $i$ and $1$, like the two dimensional space. Why would we need a four dimensional set to represent rotations on a three dimensional space? And what kind of rotation would a quaternion (for example $i+j+k+1$) represent?

Remark:

On the tutorial I'm following to learn Unity, I've used the Quaternion.Euler(float x, float y, float z) function to create a rotation. From the documentation about the later:

Returns a rotation that rotates z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order).

Note that the fourth dimension isn't used to define that rotation.

moray95
  • 1,037
  • I know it's not relavant, but I assume you are making 3D fractals? – AvZ Mar 04 '15 at 15:36
  • @AvZ Not at all, I was just trying to create some animations in a game. – moray95 Mar 04 '15 at 15:37
  • Now you've sparked my interest. How are you making them using quaternions? – AvZ Mar 04 '15 at 15:38
  • @AvZ Haha, it's kind of a broad subject but as I wrote in the question, I'm using a game engine called Unity and C# language. The rotation animation I asked about is explained in this tutorial around 19:00. – moray95 Mar 04 '15 at 15:47
  • 2
    There's a fairly detailed answer on Wikipedia in the article on SO(3). And even more detail in Charts on SO(3) § Parametrizations. And even a full article: Quaternions and spatial rotation . The standard graphics textbook answer it that quaternions avoid gimbal lock. – the gods from engineering Mar 04 '15 at 15:57
  • Also, your last question seems to be missing the obvious fact that that constructor is a conversion from Euler angles to quaternions. – the gods from engineering Mar 04 '15 at 16:15
  • @RespawnedFluff These articles are too technical for me to understand, but as far as I could, with a 3 dimensional representation of rotations in a 3D space, there could be some cases where a rotation can be denoted by two different numbers. Is that so? For the constructor, it may be a conversion from Euler angles but the documentation doesn't refer to Euler angles at any point. – moray95 Mar 04 '15 at 16:19
  • Well, at the level of analogy, unit-length quaternions represent 3D rotations much like unit-length complex numbers represent 2D rotations. But the group structure of rotations in 3D, SO(3), is not as simple as that of SO(2). I'm not sure it's possible to explain the issues without understanding groups and projective geometry... Maybe after reading these comments someone will enter an answer at your desired level of explanation. – the gods from engineering Mar 04 '15 at 16:29
  • (continued) The analogy with unit-length complex numbers is imprecise however because the unit-length quaternions are not SO(3), but SU(2), a covering group for SO(3). – the gods from engineering Mar 04 '15 at 16:36
  • There are a lot of videos on youtube offering one explanation or another for how quaternions represent rotations... I can't say I have the patience to watch them to recommend one that's both intuitive and doesn't gloss over any essential math aspects. In my opinion it would be more beneficial to you to learn a few rudiments of group theory and projective geometry to understand what happens rather than wade through a lot of dumbed-down explanations of questionable accuracy... – the gods from engineering Mar 04 '15 at 16:53
  • Gimbal lock, being an easier/visual topic, is reasonably explained here: https://www.youtube.com/watch?v=zc8b2Jo7mn – the gods from engineering Mar 04 '15 at 18:10
  • Rida Farouki has a talk about quaternions [but not only that], which I found worth watching: https://www.youtube.com/watch?v=dVCZs7vedWE&t=1094

    His talk is motivated by CAM (CNC machines) i.e. dynamical system, but a lot of that applies to animation in a virtual reality too. The PH curves that you hear in that talk is his research topic; he also has a book on this. You can mostly ignore those parts. He does explain the basics of quaternions well though, including why you can't have "three-dimensional numbers", how quaternions represent rotations etc. He uses vector algebra for quaternion ops.

    – the gods from engineering Mar 04 '15 at 19:16
  • Here's an hour long lecture about quaternions and rotations which doesn't get into group theory: https://www.youtube.com/watch?v=G19dXq1ToCM I've watched maybe 10 minutes of that by skipping around and except for the cool animation in the last 2 minutes of the talk, I can't say you get a moment of enlightenment out it... – the gods from engineering Mar 05 '15 at 17:30
  • By the way, for a serious but not too winded "why does it work this way" you basically have to read an intro book to Lie groups. Some such books are discussed in http://math.stackexchange.com/questions/194419/whats-a-good-place-to-learn-lie-groups Personally, I'd recommend [the first two chapters of] Stillwell's Naive Lie Theory. See my comments in that thread for a few more details. – the gods from engineering Apr 12 '15 at 13:48

3 Answers3

1

The key fact is that:

a rotation of an angle $2\theta$ in space, around an axis passing through the origin, is represented by a quaternion $e^{\mathbf{u}\theta}$, where $\mathbf{u}$ is the imaginary quaternion that correspond to the unit vector oriented along the axis of rotation. So we have the correspondence: $$ \vec{w}=R_{\mathbf{u},\theta} \; \vec{v} \quad \longleftrightarrow \quad \mathbf{w}= e^{\mathbf{u}\theta/2}\mathbf{v}e^{-\mathbf{u}\theta/2} $$

See my answer to :Quaternions vs Axis angle.

For the exponentiaton we have that if $ \mathbf{v} \in \mathbb{H}_P$ is an imaginary quaternion, putting $\theta=|\mathbf{v}|$ we have: $$ e^\mathbf{v}= \cos\theta + \mathbf{v}\;\dfrac{\sin \theta}{\theta} $$

See:Exponential Function of Quaternion - Derivation

Emilio Novati
  • 62,675
1

Why would we need a four dimensional set to represent rotations on a three dimensional space?

Short answer: We don't. A rotation quaternion only has three degrees of freedom, because it is limited to unit length: $$ |Q| = w^2 + x^2 + y^2 + z^2 = 1 $$

Longer answer: If you have any three of those, you can easily calculate the fourth whenever you need it. But if you do that, you lose the possibility of measuring and correcting accumulated errors caused by the limited precision used to represent those numbers in a computer.

After any manipulation of the quaternion, the limited precision may have caused the square sum to deviate somewhat from 1. This deviation gives an estimate of the precision you have lost.

To regain most of that precision, you can multiply each element by the inverse of the length: $$ Q_2 = Q_1 * \sqrt{\frac{1}{w^2 + x^2 + y^2 + z^2}} $$ Thus regaining the unity property by repairing your quaternion as best you can.

Tip for extra high precision: Calculate a re-normalized quaternion after every manipulation. Keep the new one only if its length is closer to 1 than the old one.

Also, don't use float but double.

Jan Heldal
  • 11
  • 1
0

"Why would we need a four dimensional set to represent rotations on a three dimensional space?"

"These articles are too technical for me to understand"

You can pick an axis and do a complex-number-type rotation around that axis. If you do that repeatedly, you may sometimes find that the axis you need to rotate around is the direction of the vector you want to rotate. "Gymbal lock".

Quaternions with four dimensions do two complex rotations at the same time, by the same number of degrees. One of the rotations is perpendicular to the direction of the vector part of the quaternion, and the scalar part of a unit quaternion is like the cosine of the angle you rotate through. That's the rotation you want. BUT at the same time, you get a second complex rotation between the axis dimension and the scalar dimension. Your result is not a vector but a quaternion.

It turns out that you can multiply on the other side to get the rotation you want, but the second rotation is in the opposite direction. So rotate half the angle you want, then on the other side rotate the other half and cancel out the part you don't want. No gymbal lock. You can get some estimate of your rounding error by checking how much the vector length has changed.

J Thomas
  • 301
  • 3
  • 8