Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

math - Quaternion rotation without Euler angles

In this comment it was strongly suggested that we should never use Euler angles. I understand that there are some limitation to Euler angles, most notably gimbal lock, but I'd like to know the best technique, or the set of techniques, that one typically uses in the absence of Euler angles?

Most dicussions on this topic involve converting from an Euler angle to a quaternion and that is a simple thing to do. But the only way I have ever read about doing rotation without Euler angles at all is to create a quaternion from two vectors as described by the article "The Shortest Arc Quaternion" by Stan Melax in "Game Programming Gems", using this technique:

template <typename T>
inline QuaternionT<T> QuaternionT<T>::CreateFromVectors(const Vector3<T>& v0, const Vector3<T>& v1)
{

    Vector3<T> c = v0.Cross(v1);
    T d = v0.Dot(v1);
    T s = std::sqrt((1 + d) * 2);

    QuaternionT<T> q;
    q.x = c.x / s;
    q.y = c.y / s;
    q.z = c.z / s;
    q.w = s / 2.0f;
    return q;
}

Is this the method referred to in the linked comment?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Orientation

An orientation is the way that a transform will orient an object within a coordinate system. An orientation is an absolute quantity, like a position or a scalar. An orientation is conceptually a value. And there are operations one can apply to orientations, depending on their representation.

Unlike vectors and scalars, orientations can be represented in a wide variety of ways.

What does it mean to "Use Euler Angles"?

Euler angles are a series of 3 rotations about 3 fixed, orthogonal axes. The order in which these are applied is important and is generally established by convention.

To "use Euler angles" means that Euler angles are how your code stores and manipulates the orientation of an object. How you eventually compose these angles to generate a matrix is of no consequence. What matters is that your code will treat the orientation as 3 angles. For example, when you apply a rotational offset to the orientation, it will be provided as offsets to rotation angles, and these offsets will be applied to the stored Euler angles directly.

What does it mean to "Use Matrices"?

I know nobody said that, but I have a point to make here.

To "use matrices" means that a rotation matrix is how your code stores and manipulates the orientation of an object. If some piece of code wants to rotate the object, they will apply a matrix to it, either on the left side or the right. Even if that matrix is computed via some axial rotation, the code is still performing the basic operation on a matrix, not an angle.

What does it mean to "Use Quaternions"?

For the purposes of this discussion, a "quaternion" is a 4-element unit vector which is used to encode an orientation. Quaternions can have matrix-like operations done on them, such as composition and inversion. Quaternions must remain normalized in order to properly encode an orientation.

To "use quaternions" means that you are storing the orientation and manipulating of the object as a quaternion. All of your operations on orientations, at their most fundamental level, are dealing with quaternion math.

How to adjust the orientation

Euler angles are often used because they are (theoretically) intuitive to adjust: you just increment or decrement an angle. If you want to turn the object -10 degrees in the X, you just subtract 10 from the X axial rotation. But we don't want to use them, because they're terrible, so let's look at the other orientation representations.

To adjust an orientation as a matrix, you have to do two things. You must multiply the current orientation with an offset rotation matrix (if you want to rotate by -10 degrees in the X axis, you create an angle/axis matrix for that and right-multiply it). And then, because computers have finite precision, you must re-orthonormalize the matrix. If you don't do the second step, your matrix will eventually stop being orthonormal and thus stop being an orientation.

Orthonormalizing a matrix is hard. That's (part of) the reason why we use quaternions instead. Normalizing a quaternion is easy; it's just 4-element vector normalization. And since quaternions and matrices have analogous operations, the same math will work with both. So they look pretty much identical.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...