0
$\begingroup$

My question is : When I rotate around x and y axis, yaw and pitch, how can I determine the sometimes resulting z axis rotation, the roll, caused by the Gimbal Lock ? Could you please give me any formula ? Another question would be : If I would enable x-axis rotation to go from -90 to 90 degrees, wont I have the Gimbal Lock then ?

  • 0
    Perhaps I misunderstand the concept of 'gimbal lock' but I thought that if the normal vectors of two gimbal planes were parallel then there could be no rotation about the third axis (roll in your example) because of the gimbal lock.2017-01-28
  • 0
    I think gimbal lock is a resulting z axis rotation, and of course, you wont rotate right then, but roll.2017-01-28
  • 0
    OK, outside my area of expertise.2017-01-28
  • 0
    I think John is right... is the question how to overcome the gimbal lock problem?2017-01-29
  • 0
    Yes, basically. Its that I thought of overcoming it with evaluating the resulting roll, and then rotating back this.2017-01-29
  • 0
    Are you familiar with https://en.wikipedia.org/wiki/Gimbal_lock? Maybe this helps to obtain the answer...2017-01-29
  • 0
    Yes, but I found it on the german wikipedia.2017-01-30
  • 0
    So, as I edited : Would x-axis rotation from -90 to 90 degrees be a solution ?2017-01-30
  • 1
    If you are using three parameters to represent rotations, you will always have at least two configurations which correspond to gimbal lock. This is because $SO(3) \sim S_1 \times S_2$. In other words, 3D rotations can be thought of as the combination of points on a circle and a sphere. Since a sphere cannot be parameterized with two parameters without a singularity (i.e. problem with lat/long at North and South poles), neither can 3D rotations.2017-01-31
  • 0
    This is why people use unit quaternions to represent rotations. They do not suffer from gimbal lock.2017-01-31
  • 0
    @user7185318: [Versors](https://en.wikipedia.org/wiki/Versor) are easy to implement. Quaternions $q=(w,x,y,z)$ are versors when used to represent a rotation, and have norm is one, $\lVert q \rVert = \sqrt(w^2 + x^2 + y^2 + z^2) = 1$ i.e. $w^2 + x^2 + y^2 + z^2 = 1$. You can convert between Euler angles and versors, and the [matrix corresponding to the rotation](https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Quaternion-derived_rotation_matrix) is easy. [...]2017-02-02
  • 0
    .. Instead of adding to or subtracting from some Euler angle(s), you multiply ([Hamilton product](https://en.wikipedia.org/wiki/Quaternion#Hamilton_product)) the current orientation quaternion with a quaternion describing the operation (like "pull up", "roll left", and so on, usually scaled by the amount ("degrees")), often followed by renormalization for numerical stability ($n = \sqrt{w^2 + x^2 + y^2 + z^2}$, $(w, x, y, z) \leftarrow (w / n, x / n, y / n, z / n)$). No gimbal lock, and at least as easy to implement than Euler angles. You need half a dozen helper functions, but that's it.2017-02-02
  • 0
    @NominalAnimal : Thanks, ill probably use it. But one question : Would gimbal lock in a flight simulation be realistic ?2017-02-02
  • 0
    @user7185318: Let's say you are flying a plane, with a really powerful engine. You pull up its nose towards the zenith, so the plane is essentially vertical. Using the typical Euler angle conventions, at this point your yaw acts exactly like your roll. Is that realistic? No, it's not. It does not occur in real flight. Nor does it occur if you use versors (the easy way; i.e. not converting to Euler angles and back). Euler angles are bad, mm'kay? They only *look* simple, but they're really not. (But, if your planes **never** fly straight up like that, then no need to worry about gimbal lock.)2017-02-02
  • 0
    @Nominal Animal : Thanks, Ill try to get quaternions working2017-02-04
  • 0
    It's unclear how you mean to "solve" gimbal lock by an $x$ rotation from -90 to 90. "Gimbal lock" is so named because it happens with actual physical gimbals of an actual rotating object. When the object reaches a certain orientation from a certain direction, the only way to continue rotating it in a similar direction is first to instantaneously flip one of the gimbals 180 degrees. Physical gimbals can't do that, so instead they get stuck. Of course _mathematically_ you can flip a gimbal instantaneously and continue the rotation, but that's just ignoring the problem, not solving it.2017-02-04
  • 0
    If what you meant was you are going to limit the $x$ rotation to less than its usual range, you might avoid gimbal lock by preventing the rotation from getting near the gimbal-lock orientation. But that would just be avoiding one limitation on rotations by imposing a much more severe limitation on rotations.2017-02-04
  • 0
    @DavidK : I know, its not actually solving it, but I thought that this would be realistic in a minecraft-like 3d game I am trying to make; You can only look upwards until 90 degrees, and downwards to, or can you move your head that way. I think its realistic.2017-02-04
  • 0
    Head are mounted on very restrictive "gimbals." When modeling something like that, you really don't have to worry about "gimbal lock" because the realistic limitations on rotation of the head are so much more severe. So then the answer to your question may be yes, the kind of freedom of rotation you are trying to model has no gimbal lock.2017-02-04

1 Answers 1

1

This is not an answer, only some advice on how to utilize versors; unit quaternions that represent rotations in three dimensions.


  1. In computer graphics, both 3×3 and 4×4 matrices are used to represent 3D transforms.

    Point $\vec{p} = \left [ \matrix { x \\ y \\ z } \right ]$ is transformed by matrix $\mathbf{M} = \left [ \matrix { x_x & y_x & z_x \\ x_y & y_y & z_y \\ x_z & y_z & z_z } \right ] $ via multiplication:

    $$\vec{p}' = \mathbf{M} \vec{p}$$

    which means that

    $$\begin{cases} x' = x \; x_x + y \; y_x + z \; z_x \\ y' = x \; x_y + y \; y_y + z \; z_y \\ z' = x \; x_z + y \; y_z + z \; z_z \end{cases}$$

    Note that the three vectors, $\hat{e}_x = \left [ \matrix { x_x \\ x_y \\ x_z } \right ]$, $\hat{e}_y = \left [ \matrix { y_x \\ y_y \\ y_z } \right ]$, and $\hat{e}_z = \left [ \matrix { z_x \\ z_y \\ z_z } \right ]$, are also the unit vectors in the rotated coordinate system.

    Because pure rotation matrixes are orthogonal, the inverse of $\mathbf{M}$ is its transpose, i.e. $\mathbf{M}^{-1} = \mathbf{M}^{T} = \left [ \matrix { x_x & x_y & x_z \\ y_x & y_y & y_z \\ z_x & z_y & z_x } \right ]$.

    If 4×4 matrices are used, then points are implicitly $\vec{p} = \left [ \matrix { x \\ y \\ z \\ 1 } \right ]$, and the transformation matrix is $\mathbf{M} = \left [ \matrix { x_x & y_x & z_x & t_x \\ x_y & y_y & z_y & t_y \\ x_z & y_z & z_z & t_z \\ 0 & 0 & 1} \right ] $, where the vector $\vec{T} = \left [ \matrix { t_x \\ t_y \\ t_z } \right ]$ is translation after rotation.

    I've shown here how to use the translation part $\vec{T}$ to define the matrix as a rotation around some other point than origin, and how to combine translation before and after the rotation into $\vec{T}$.

    In the 4×4 matrix case, very little changes programming-wise:

    $$\begin{cases} x' = x \; x_x + y \; y_x + z \; z_x + t_x \\ y' = x \; x_y + y \; y_y + z \; z_y + t_y \\ z' = x \; x_z + y \; y_z + z \; z_z + t_z \end{cases}$$

    Note that the order in which the matrix elements are stored, varies. Multidimensional arrays in Fortran use column-major order, whereas in C they use row-major order. If you use a 3D toolkit, check its documentation to verify.


  1. Quaternion $q = (w, x, y, z)$ is an unit quaternion or a versor, if $w^2 + x^2 + y^2 + z^2 = 1$.

    The versor $q$ corresponds to a rotation matrix $\mathbf{M}$, $$\mathbf{M} = \left[\begin{matrix} 1 - 2 (y^2 + z^2) & 2 (x y - z w) & 2 (x z + y w) \\ 2 (x y + z w) & 1 - 2 (x^2 + z^2) & 2 (y z - x w) \\ 2 (x z - y w) & 2 (y z + x w) & 1 - 2 (x^2 + y^2) \end{matrix}\right]$$


  1. Versor $q$ also corresponds to a rotation around an axis.

    Let $\hat{a} = (a_x, a_y, a_z)$ be the axis, and its norm 1, i.e. $a_x^2 + a_y^2 + a_z^2 = 1$. Let $\theta$ be the rotation around that axis. Then,

    $$\begin{cases} w = \cos\frac{\theta}{2} \\ x = a_x \sin\frac{\theta}{2} \\ y = a_y \sin\frac{\theta}{2} \\ z = a_z \sin\frac{\theta}{2} \end{cases}$$


  1. To avoid numerical errors, you can always normalize the quaternion.

    This is a safe operation (does not introduce any bias in any direction), and is very useful when multiplying quaternions many times using e.g. floating-point representation.

    First, calculate $n = \sqrt{w^2 + x^2 + y^2 + z^2}$. Then, $$\begin{cases} w' = \frac{w}{n} \\ x' = \frac{x}{n} \\ y' = \frac{y}{n} \\ z' = \frac{z}{n} \end{cases}$$ where $q' = (w', x', y', z')$ is the normalized versor.

    Basically, if you write a program that uses versors, you do this every set of operations, to avoid numerical errors from creeping in.


  1. You can multiply two versors to combine the rotations. If $q_0 = (w_0, x_0, y_0, z_0)$ and $q_1 = (w_1, x_1, y_1, z_1)$, then rotating by $q_0$ and then by $q_1$ is described by $$q = q_0 q_1 = \begin{cases} w = w_0 w_1 - x_0 x_1 - y_0 y_1 - z_0 z_1 \\ x = w_0 x_1 + x_0 w_1 + y_0 z_1 - z_0 y_1 \\ y = w_0 y_1 - x_0 z_1 + y_0 w_1 + z_0 x_1 \\ z = w_0 z_1 + x_0 y_1 - y_0 x_1 + z_0 w_1 \end{cases}$$

  1. You can interpolate between two versors, by scaling and adding them together, and normalizing the result (as in step 4. above).

    For example, if you have $0 \le p \le 1$ describing "phase", or state between rotations $q_0$ and $q_1$, then

    $$\begin{cases} w' = (1-p) w_0 + p w_1 \\ x' = (1-p) x_0 + p x_1 \\ y' = (1-p) y_0 + p y_1 \\ z' = (1-p) z_0 + p z_1\end{cases}$$

    and with $n' = \sqrt{w'^2 + x'^2 + y'^2 + z'^2}$, you can compute the desired rotation with $w = w'/n'$, $x = x'/n'$, $y = y'/n'$, and $z = z'/n'$.


  1. Rudders, ailerons, and attitude thrusters, can be represented by the axis and angle they try to rotate the vessel around.

    The axis does not change (unless the attitude control element suffers damage), but the angle is dependent on both the force and the duration.

    For example, consider a fixed wing aircraft with elevators (controlling pitch), ailerons (controlling roll) and a rudder (controlling yaw).

    To simulate their effect on the current orientation of the plane, a programmer would have one quaternion ($q$) representing the orientation of the plane compared to some fixed reference (world coordinates), as well as three variables describing the state of the three control surfaces ($p$, $r$, $a$, say all within -1 and +1).

    To update the orientation, we need to know how long the state has been applied; i.e. the length of the time step, $\Delta t$. We also need to know how large a rotation (in radians), in one time unit, each control surface can affect (say, $C_p$, $C_r$, $C_a$). Then, we can calculate $\theta_p = p \; C_p \; \Delta t$, $\theta_r = r \; C_r \; \Delta t$, and $\theta_a = a \; C_a \; \Delta t$.

    With these, the programmer can use point 3. above to calculate the versor representing each of the three effects (the axis being the rotation axis the control surface rotates the aircraft around).

    The new orientation is obtained by multiplying the current orientation versor by the three control surface versors, and normalizing the result (step 4. above).

    Note that here, we successively apply the rotation caused by each control surface. If the time steps (and thus changes in orientation) are small -- and you do need them to be small, in any kind of simulation! --, this works just fine. Mathematically speaking, the order in which we multiply the quaternions matters: the first one is applied to the old orientation, but the second one is applied to the resulting orientation -- as if the first control surface affected the second (and both affect the third). This has nothing to do with quaternions per se, but only on how we model the effect of the control surfaces to the current orientation.

  • 0
    Thanks for the much effort you spend in this very detailed advice. But I think its an answer : Quaternions are used to overcome the Gimbal Lock. So why shouldnt this be an answer ? Id like to accept it.2017-02-04
  • 0
    @user7185318: Well, it is your decision. I don't consider it an answer, because quaternions don't *overcome* gimbal lock; they avoid it altogether. With versors, there is no gimbal lock.2017-02-04
  • 0
    All what matters is that the Gimbal Lock does not occur.2017-02-04
  • 0
    @user7185318 Usually one accepts an answer to the question originally asked, but sometimes what a question literally says is not what it really is about. If your question was really "how to avoid the problem of gimbal lock," as it now seems to be, this seems to be a perfectly good answer and you may accept it. You may just want to wait a couple of hours to see if anyone else has something to add.2017-02-04
  • 0
    @DavidK : Good suggestion. Ill do.2017-02-04