Quaternion Basics in Unity3d

by SlideFactory

No matter what kind of 3D development you do, you’re going to run up against the concept of Quaternions at one point or another. If you’re a unity user, an important part of how it translates 3D input is through its use of Quaternion rotations and to effectively position items in 3D, there’s probably no way you’ll get around using them, at one point or another. In this article, we’re going to explore the Quaternion basics in Unity3d. We’ll look at the basics of what a Quaternion is, how to translate Euler rotations to quaternion rotations in Unity, as well as the benefits of using quaternions over Euler rotations. Additionally, we’ll look at the basic structure of a quaternion and how to translate quaternions into different quadrants and shift their rotations.

Keep in mind, this article is just going to be a basic primer. Quaternions are crazy and if you’re really looking for a deep dive into the math behind them, there are a lot better sources with much deeper information.

Euler rotations use a set of three angles to represent a rotation around the X, Y, and Z axes, respectively. Quaternion rotations, on the other hand, use a four-dimensional vector to represent a rotation in 3D space. In Unity, we can use the Quaternion.Euler() function to translate Euler rotations to quaternion rotations. This function takes a Vector3 object that represents the Euler angles (in degrees) around the X, Y, and Z axes. For example, the following code rotates an object by 45 degrees around the X axis, 30 degrees around the Y axis, and 60 degrees around the Z axis:

Vector3 eulerAngles = new Vector3(45, 30, 60); Quaternion rotation = Quaternion.Euler(eulerAngles); transform.rotation = rotation;

Quaternions have several advantages over Euler rotations when it comes to rotations in 3D space. One of the main advantages is that quaternions are less prone to gimbal lock. Gimbal lock is a phenomenon that occurs when two of the three axes of a Euler rotation align, causing the rotation to become ambiguous. 

Quaternions, on the other hand, do not suffer from gimbal lock. Additionally, Quaternions also offer the ability to smoothly interpolate between two rotations which is not easily possible with Euler angles, specially when objects are rotating with large angles or when you need a smooth object rotation.

A quaternion is a 4-dimensional vector that is represented by a scalar (real) part and a 3-dimensional vector (imaginary) part. The scalar part represents the amount of rotation, while the vector part represents the axis of rotation. It can also be written in the form w + xi + yj + zk, where w, x, y, and z are real numbers and i, j, and k are imaginary units.

In Unity, a quaternion can be represented using the Quaternion class. This class contains several properties that can be used to access the individual parts of a quaternion. For example, the following code demonstrates how to access the x, y, z, and w components of a quaternion:

Quaternion rotation = transform.rotation; 
float x = rotation.x; 
float y = rotation.y; 
float z = rotation.z; 
float w = rotation.w;

As mentioned, the x, y, z, and w components of a quaternion represent the imaginary and real parts of the quaternion. The x, y, and z components represent the imaginary vector (i, j, k) part of the quaternion and the w component represents the real scalar part of the quaternion. They can be used to create a new quaternion, for example, the following code creates a new quaternion with x = 0.1, y = 0.2, z = 0.3 and w = 0.4:

Quaternion myQuaternion = new Quaternion(0.1f, 0.2f, 0.3f, 0.4f);

It is also possible to create a quaternion using other functions, like Quaternion.Euler(Vector3 eulerAngles) that we previously used, or Quaternion.LookRotation(Vector3 direction) to create a rotation that point in specific direction.

It is important to note that the values of the x, y, z, and w components of a quaternion should not exceed the range of [-1,1], otherwise the quaternion will not represent a valid rotation.

It is also worth noting that the Quaternion class in Unity provides a lot of useful methods that can be used to perform different types of operations on quaternions. These operations include, but are not limited to, interpolation, inversion, and normalization.

In addition, Quaternions also offers the ability to smoothly interpolate between two rotations which is not easily possible with Euler angles, specially when objects are rotating with large angles or when you need a smooth object rotation.

One of the key properties of quaternions is that they can be rotated by multiplying them with another quaternion. The result of this multiplication is a new quaternion that represents the original rotation followed by the rotation of the second quaternion. For example, the following code demonstrates how to rotate a quaternion by 45 degrees around the X axis:

Quaternion originalRotation = Quaternion.Euler(new Vector3(0, 0, 0)); 
Quaternion xRotation = Quaternion.Euler(new Vector3(45, 0, 0)); 
Quaternion newRotation = originalRotation * xRotation;

This multiplication can be used to orient a quaternion towards a specific direction or target. By multiplying a quaternion with the rotation between the current direction and the target direction, the quaternion will be oriented towards the target. For example, the following code demonstrates how to orient a quaternion towards a target position:

Quaternion originalRotation = transform.rotation; 
Vector3 targetDirection = target.position - transform.position; 
Quaternion targetRotation = Quaternion.LookRotation(targetDirection); 
Quaternion newRotation = Quaternion.Lerp(originalRotation, targetRotation, Time.deltaTime); transform.rotation = newRotation;

In conclusion, Quaternions provide a powerful and efficient way to represent rotations in 3D space. They can be rotated by multiplying them with another quaternion and this allows us to orient them in the direction we need. Understanding the structure and mathematical operations of quaternions can give you more control and precision in your rotations. Unity provides the Quaternion class with a set of properties and methods that makes it easy to use quaternions inside Unity. Using them can lead to more stable and realistic simulations, animations and games.

Share this article:

You might also like: