Quaternions in Unity3d

Asked

Viewed 551 times

-1

Well... I wish I knew more about Quaternions in Unity... I didn’t understand very well what they do... and why they use the Quaternions and not the Angles of Euler... I didn’t find good material about it, I even found out about the Quaternions themselves, but not about the Quaternions inserted in Unity... I do not have an extremely advanced knowledge in mathematics, so try to explain in a simplified way... thank you...

1 answer

1


I don’t understand about it, but I find it interesting, so I did some research and I’ll put what I found here, with the sources.

From the Unity3d documentation:

Unity - Scripting API: Quaternion

Quaternions are used to represent rotations.

They are compact, do not suffer from the gimbal lock (gimbal lock) and can be easily interpolated. Unity internally uses Quaternions to represent all rotations.

They are based on complex numbers and are not easy to understand intuitively. You almost never access or modify individual Quaternion components (x, y, z, w); most of the time you would simply take existing rotations (for example, from the class Transform) and would use them to construct new rotations (for example, to interpolate smoothly between two rotations). The functions of the Quaternion structure that you use 99% of the time are: Quaternion.Lookrotation, Quaternion.Angle, Quaternion.Euler, Quaternion.Slerp, Quaternion.Fromtorotation and Quaternion.Dentity. (Other functions are for exotic uses only).

Do Stack Overflow (English):

Quaternions vs. Euler Angles

Euler angles are more understandable for humans and also good for breaking down rotations into individual degrees of freedom (for kinematic and similar joints), but have disadvantages such as ambiguity and gimbal locking. In practice I would prefer Quaternions, since they are easier to compute (for the computer, not for humans) and more efficient. You have to do three rotations and multiply them together by rotating the Euler angles, while with a Quaternion only one rotation is required, and as it already encodes the sine and cosine, the conversion of Quaternion to matrix is quite efficient.

From the Unity Questions and Answers site:

When to use Quartenion vs Euler Angles? (When to use Quaternion vs Euler Angles?)

Question:
This is a matter of "best practices" as well as a question of clarification. I understand that Euler angles are a way to read Quaternions as a vector3 value, but when would you want to use Euler angles? Why not wear Quaternions all the time?

Answer:
Quaternions have some advantages when it comes to the gimbal lock and smooth interpolation. Their main disadvantage is that they rely on advanced mathematics -- mathematics that even experienced developers often find difficult and confusing.

People rarely interact with the Quaternions directly. As it turns out, it’s almost always easier to manipulate them using other representations:

  • The angle-axis representation specifies a unit vector and a rotation on that vector (see the Toangleaxis and Angleaxis pages).

  • The Euler angle representation specifies the rotation around the Z, X and Y axes in that order (see eulerAngles and Euler pages).

The script manual suggests some of the most common tricks for manipulating or generating Quaternions.

There are some academic articles on the Internet if you want to understand more about the mathematics behind the Quaternions. If you plan to use them too much, I highly recommend that you at least pass lightly by some.

I recommend using Quaternion variables to represent two things: the rotation of an object and/or a rotation that you would like to apply to an object. Even when used for this purpose, it is almost always easier to generate them using the methods described above, or by taking an existing Quaternion and rotating it for some value that you have just generated.

It is very rare that you need to actively examine or manipulate Quaternions that were not created by yourself. Generally, it is easier to use vector mathematics to solve problems, including the intelligent use of scalar and vector products.

From the comments to the above reply:

Use a Quaternion to copy a spin:

Quaterion myQuaterion = transform.rotation;
otherTransform.rotation = myQuaterion;

Use Euler angles to change or configure a new rotation:

rotation.eulerAngles = new Vector3(0, 30, 0);

By the time you have a Phd in mathematics to use Quaternions directly, you will no longer be interested in writing games.

Very difficult to define a specific angle with a Quaternion.
Both have different applications. I usually use eulerAngles much more often than Quaternions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.