# Euler, Quaternions, Radians, Degrees... huh?

Discussion in 'Scripting' started by MauMau, Jul 4, 2010.

1. ### MauMau

Joined:
Jun 26, 2010
Posts:
41
Could someone please explain me (as a dumb non-mathematician) the general difference between euler, quaternions, radians and degrees in a few words (or point me to a clear, easy-to-understand tutorial)? What's the general difference between them and which one should be used in what situation?

Before I switched to Unity, I did not have to think about it too much since the language of my choice could be used like "RotateObject MyObject, 0...360, 0...360, 0...360". I heard from eulers and quaternions before but (thanksfully) never had to mess with them.

I searched for clear tutorials on this subject but most of them are written from mathematicians for mathematicians. How could this be explained in simple words to the average Joe?

2. ### tomvds

Joined:
Oct 10, 2008
Posts:
1,028
They are all different ways of measuring the same thing.

Euler angles are most likely what you are used to (the 3 numbers in your RotateObject are an Euler angle). An Euler angle expresses a 3d angle as 3 numbers, the rotation around the x, y and z axis. These numbers are in degrees (a number between 0-360). In the Unity inspector the angles you can fill in are Euler angles.

Radians are the same thing as degrees, except that they run from 0-6.28 (2*pi) instead of 0-360. It's like the difference between yards and meters. Radians are only important when using with Sin and Cos functions as their input is in radians instead of degrees.

Finally, quaternions are similar to Euler angles in that they express 3d angles. Unlike Euler angles, the mechanics of Quaternions quite difficult to grasp for most people (they are closely related to complex numbers). The reason that they are important is they do not suffer from a certain mathematical issue with Euler angles, called Gimbal Lock (http://en.wikipedia.org/wiki/Gimbal_lock).

Since Gimbal Lock is a big problem in computer graphics, Unity and most other 3D engines use quaternions to represent angles internally. The good news is that you don't have to understand them. While Unity wants quaternions from you when setting angles, you can simply create the quaternion from Euler angles and forget what the quaternion is. Additionally, when Unity gives you an angle (as a quaternion), you can convert it back to an euler angle to get a more readable rotation.

3. ### MauMau

Joined:
Jun 26, 2010
Posts:
41
Thanks for the detailed answer, tomvds. So I can focus on Euler only and don't have to mess with the internals of Quaternions at all?

So whenever dealing with rotations in Unity, I can do everything with Eulers (as I am used to) and just convert a Euler rotation to a Quaternion when telling Unity what to do?

Code (csharp):
1.
2. var pitch:float = transform.eulerAngles.x;
3. var yaw:float   = transform.eulerAngles.y;
4. var roll:float  = transform.eulerAngles.z;
5.
6. pitch -= 15;
7. yaw   += 90;
8. roll   = 45;
9.
10. transform.rotation = Quaternion.Euler(y,x,z);
11.

4. ### tomvds

Joined:
Oct 10, 2008
Posts:
1,028
Yes, that's the basic idea. There are some other useful quaternion functions, though. Especially Quaternion.Lerp and Quaternion.LookRotation are useful functions that don't require any knowledge of quaternion internals.

### Volunteer ModeratorModerator

Joined:
Jul 19, 2006
Posts:
32,318
It's not quite that simple unfortunately. In particular, reading euler angles from a transform can result in unexpected results after the quaternion conversion. (Not incorrect results, just not necessarily what you would expect given that a rotation can be expressed in more than one way.)

With your pitch/yaw/roll code, for example, it would be better to track the rotations yourself and assign the euler angles from that, rather than relying on reading from the transform. (There's a lengthy topic about exactly this somewhere around here.) And when assigning euler angles, you should always do all 3 elements at once rather than just transform.eulerAngles.x (or .y or .z). There are a few exceptions to this...if you're only ever rotating on one axis, for example, then you can read from that reliably.

--Eric

6. ### qvision

Joined:
Jun 5, 2013
Posts:
1
I know this is an old thread but it's Unity so it's pertinent.

I am writing a main camera controller to simulate a first-person view from a spaceship cockpit.

The control system is the same as that of X3, the distance of the mouse from screen centre tells the ship how fast to turn toward the mouses position, nice and simple (or so i thought !).

I am new to Unity and a bit of a dinosaur programmer (6502/6800 assembly plus various higher level languages), i still think somewhat procedurally but i'm trying to kick the habit ;+}

I have read that Unity converts internally to quats so Gimbal lock is avoided. but i am experiencing erroneus behaviour.

When my pitch exceeds 90 degrees my yaw control suffers from inversion.

I've searched quite a lot and can't find a solution, most people writing space-games use a camera-follow script but i want to transform the main camera.

I have tried many variations on transform commands and Euler/Quat conversions but all end up being problematic.

Does anyone have a solution for this before i pull out the last of my hair ?

Cheers,

qvision.

Last edited: Jun 8, 2013
7. ### samizzo

Joined:
Sep 7, 2011
Posts:
485
Quaternions aren't just a magic panacea to avoiding gimbal lock. Gimbal lock occurs when you represent a rotation as multiple rotations around component axes. For example if you rotate around X axis, then Y axis, then Z axis it is possible to rotate one axis onto another, and thus you lose an axis of freedom which results in gimbal lock. It doesn't matter how you combine those rotations - as matrices, as quaternions, or as Euler angles - you will still get gimbal lock. Here's an explanation.

8. ### LightStriker

Joined:
Aug 3, 2013
Posts:
2,677
However, quaternion, when properly used, are only 1 rotation. In mathematic, radian (and other) define an orientation, while a quaternion defines a rotation. The notion is subtil, but crucial. When properly used, a quaternion should allow you to avoid Gimbal locks.

If you transfer your quaternion into Euler or back and forth between the two, you're an idiot that only ask for Gimbal locks to occur.

unityunity