Main Menu

search

You are here

Quaternions

[last updated: 2024-11-14
vector mathematics
vectors: alternative definitions
geometric algebra and calculus
quaternions and rotations
-----

  • Quaternions are an extension of the Complex number system.
    In the Complex number system, quantities are of the form:
      c = a + bi
      (Notice this is the form for an expression defining electrical impedance.)

    Complex numbers are plotted on a 2-dimensional graph, with the scalar portion "a" along the x-axis,
    and the complex/imaginary portion "b" along the y-axis.


    Quaternions were invented to extend that system into 3 dimensions. As such, quaternions are of the form:

      q = a + bi + cj + dk

    where:

      a, b, c, and d are Real scalars, and
      i, j, and k are unit vectors that obey the rules:
        ı2 = j2 = k2 = −1
        and:
        ı j = +k
        j k = +i
        k i = +j

        These latter rules define the unit vectors as mutually perpendicular (orthonormal).
  • Properties of quaternions:
    Theorems and proofs are available to show:
      i j k = -1
      and:
      ij = −ji,
      jk = −kj,
      ki = −ik
  • Note that if a = 0, then the expression for a quaternion is equivalent to a "standard" vector in 3 dimensions.
      v = bi + cj + dk

    This is called a "pure quaternion."

  • Un-sorted notes from reddit post...
      The main thing quaternions are doing is that any rotation in3D can be represented by a composition of 2 reflections. The algebra of a reflection is fairly simple to understand visually so that is the easiest way to understand quaternions, to understand how the manipulation of the coordinates is equivalent to a composition of reflections.
      -----

      We can represent rotations by many different forms:
      matrix
      axis-angle
      etc
      Unit quaternions are mathematically equivalent to axis+half angle rotation form. That is they use a 720° system. An analogy is a möbius strip but for rotations.
      -----

      It might be sufficient to understand that quaternions are rotation axes (as vectors) plus an implied angle between 0 and 180°, arccos(w). The quaternion multiplication is just the sequential rotation of both while simple vectors can also be represented as 90° rotations around an axis (the common quaternion rotation p·q·p⁻¹ even works out as a 4D rotation).

      The multiplication of unit quaternions works like each quaternion's vector is the center of a scalable cylinder whose surface contains the other quaternion's vector and the surface is rotated by the angle of the quaternion (the other quaternion is located at 0° cylindrical rotation). The left quaternion rotates its cylinder in positve direction, the right quaternion rotates it's cylinder in left direction. Now, draw one parallel line on the surface of each cylinder, i.e. show

      where:all points which are at the quaternion's angle inside the cylinder surface. Finally, the intersection of these two lines is the vector of the new quaternion. And you get the w component, if you imagine each quaternion vector to be the center point of a circle that is contained in the surface of a sphere (which uses the quaternion's length as its sphere radius).

      The length of each unit quaternion vector is between 0 and 1 and scaling the vector in the unit quaternion will change its angle and scale the radius of the other cylinder.

      There are details in this visualization. I first wrote a Reddit post which was too long and then decided to write a more comprehensive Medium blog post about the visualization. The main soar spot is, I wasted over half a week of thinking, while I actually have no time for it and therefore did not add actual visualization in figures or images.
      https://medium.com/@dorla.hutch/intuitive-visualization-of-quaternions-0...
      -----

      From a very high perspective, you can treat quaternions as a blackbox to rotate vectors without any problems/singularities. Looking a bit closer, we can start with understanding what complex numbers do: When you take an arbitrary complex number, you can interpret this as a vector in 2D. Multiplying it by another complex number rotates (and scales) this vector. Multiplying with a complex number of length 1 just rotates it. For example multiplying this vector with i gives a 90° rotation, multiplying it two times by i gives i^2=-1 and therefore 180°. Quaternions are now the attempt to bring this idea to 3D and it turns out instead of having just one imaginary number i, you need three, i,j,k. Why? Since adding one imaginary variables j for 3D boils the question what is i*j, the trick is, we simply say i*j=k and close it circularly. And it turns out that we found a nice tool that way to rotate objects in 3D by just having four numbers and we even don't need slow trig functions, but just addition and multiplication of some variables. If you want to know more, I wrote an article here: https://raw.org/book/algebra/quaternions/
      -----

      So quaternions are three parts imaginary right?

      The simplest way I have found to explain what this means is with the complex number circle (the unit circle but instead of the y axis, it's the imaginary axis). This lets "i" be one 90 degree rotation around the circle. 1 (1,0) is 0 degrees on the circle, times i is i (0,1) or 90 degrees, times i is -1 (-1,0) or 180 degrees, times i is -i (0,-1) or 270 degrees, time i is 1. We can think of of this as a rotation around one axis that natively circles around using multiplication, note that we didn't have to check for any edge cases (e.g. angle >= 360 then set to a valid angle less than 360). Stop here and make sure you understand the relationship between complex numbers and single-axis rotations. It may be useful to look into how other partially imaginary values with an absolute value of 1 multiply into the circle.

      Quaternions are then three parts imaginary. If we expand our circle model, two parts would be a unit sphere where we can rotate it around by multiplying. And three parts would be... some sort of very difficult to imagine hypersphere. And so I fall back on the unit sphere model. But the basic idea is that each imaginary part is an axis of rotation. So it's like walking on a globe, but a globe from a point on it feels like it's 2d, for that extra freedom of rotation, for walking on the sphere to feel like moving around in 3d, it would have to be a fourth dimensional unit sphere that is three parts imaginary. And so when we multiply two positions on this hyper-sphere together (following the rules of quaternion multiplication) it's like multiplying the locations on the unit circles to get a new unit circle position, rotating the two together, which when done on a hyper-sphere is doing a rotation about three axis.
      -----
      Short answer would be that a constraint of rotation quaternions is that it must be of unit length. A unit vector is a vector with length of one, so a quaternion rotation is a point in 4D space of length one. When you follow process listed in this picture, you will always end up with a quaternion of unit length.
      -----
      the book Game Engine Architecture and got to the section on quaternion rotations. It was explained so plainly
      -----
      You don't need to convert it back to axis/angle for interpolating between two quaternion rotations. Instead you would do a LERP or SLERP operation to get the weighted average between two quaternions. SLERP is recommended for accuracy, but LERPing the two quaternions usually results in a good enough transition and is generally faster to compute. I won't go into the details of how to compute SLERP or LERP as you can find them online, but I'll give you an idea of what they are doing.

      Another way to visualize a quaternion is a point on a hypersphere (4D sphere). For simplicity's sake, you can think of it like a point on a regular sphere. If you want to move between two points on the sphere, you generally have two options. You can draw a line between the two which would be inside of the sphere and traverse the line which is a representation of LERP. You could alternatively draw a curve between the two points that lies on the sphere and traverse that. The second option would be an example of SLERP. For less dramatic changes in rotations, the loss of accuracy in LERPing wouldn't be noticeable. When you are getting closer to a 180 degree change in rotation, it might be better to use the SLERP algorithm since LERP may start looking incorrect.

      Here is a video that shows this in action. Hope this helps!
      https://www.youtube.com/watch?v=uNHIPVOnt-Y
      -----
      a quaternion "encodes" a rotation.
      -----

      Indeed! You just divide the angle in two. So for the W component it would look something like:

      quaternion.w = Math.Cos(angle / 2);

      -----

-------------------------------------------

  • Links:


    .


    .

    eof