# Quaternion

May 20, 2023

A quaternion is a mathematical concept, used in computer graphics, game development, robotics, and physics simulations. A quaternion represents a rotation in 3D space, consisting of a real number and three imaginary numbers. The real number is called the scalar component, while the imaginary numbers are called the vector components.

Quaternions were invented by William Rowan Hamilton in 1843 as an extension of complex numbers. He was looking for a way to represent rotations in 3D space, but complex numbers could only represent 2D rotations. Quaternions are a four-dimensional extension of complex numbers that can represent any 3D rotation.

## Purpose of Quaternions

Quaternions have several advantages over other methods of representing rotations in 3D space, such as Euler angles and rotation matrices. One advantage is that they do not suffer from gimbal lock, a problem that occurs with Euler angles when two of the rotation axes are aligned. Gimbal lock can cause unexpected behavior in animations and simulations. Quaternions also have a smaller memory footprint than rotation matrices, which makes them more efficient to use in computer graphics and game development.

Another advantage of quaternions is that they can be interpolated smoothly between two rotations. This is important for animations, where objects need to move smoothly from one position to another. Quaternions can also be used to represent orientation, as well as rotation, which makes them useful for robotics and physics simulations.

Quaternions are used extensively in computer graphics and game development to represent rotations and orientations of objects. They are used to animate 3D models, simulate physics, and control cameras. For example, a camera in a 3D game can be controlled using quaternions to smoothly move and rotate the camera. Quaternions are also used in robotics to control the orientation of robotic arms and end-effectors.

## Quaternion Components

A quaternion consists of four components: a scalar component and three vector components. The scalar component is usually denoted by `w`, while the vector components are denoted by `x`, `y`, and `z`. The quaternion is usually written in the form:

``````q = w + xi + yj + zk
``````

where `i`, `j`, and `k` are the imaginary unit vectors.

The scalar component represents the rotation angle, while the vector components represent the rotation axis. The rotation angle is measured in radians, and the rotation axis is a unit vector that points in the direction of the axis of rotation.

## Quaternion Operations

Quaternions can be added, subtracted, multiplied, and divided. The addition and subtraction of quaternions is done component-wise, just like complex numbers. The multiplication of quaternions is more complex and involves the use of the quaternion product.

### Quaternion Product

The quaternion product is defined as:

``````q1 * q2 = (w1w2 - x1x2 - y1y2 - z1z2) + (w1x2 + x1w2 + y1z2 - z1y2)i
+ (w1y2 - x1z2 + y1w2 + z1x2)j + (w1z2 + x1y2 - y1x2 + z1w2)k
``````

where `q1` and `q2` are two quaternions, and `w1`, `x1`, `y1`, and `z1`, and `w2`, `x2`, `y2`, and `z2` are their respective scalar and vector components.

The quaternion product is not commutative, which means that `q1 * q2` is not equal to `q2 * q1`. However, it is associative, which means that `(q1 * q2) * q3` is equal to `q1 * (q2 * q3)`.

### Quaternion Conjugate

The conjugate of a quaternion is obtained by negating the vector components. It is denoted by `q*`. The conjugate of a quaternion has the same scalar component but the vector components are negated. The conjugate of a quaternion is used in many quaternion operations, such as finding the inverse of a quaternion.

``````q* = w - xi - yj - zk
``````

### Quaternion Inverse

The inverse of a quaternion is obtained by dividing the conjugate of the quaternion by the squared magnitude of the quaternion. The inverse of a quaternion represents the opposite rotation. It can be used to undo a rotation or to find the rotation that brings an object from one orientation to another.

``````q^-1 = q* / |q|^2
``````

where `|q|^2 = w^2 + x^2 + y^2 + z^2` is the squared magnitude of the quaternion.

## Quaternion Interpolation

One of the most useful features of quaternions is their ability to interpolate smoothly between two rotations. Interpolation is the process of finding a value between two other values. In the case of quaternions, interpolation is used to find a quaternion that represents a rotation between two other rotations.

### Spherical Linear Interpolation (SLERP)

Spherical Linear Interpolation (SLERP) is a method of interpolating between two quaternions. It is called spherical because the interpolation is done along the surface of a sphere, which is the most direct path between two points on a sphere. SLERP is commonly used in computer graphics and game development to animate 3D models.

The formula for SLERP is:

``````q(t) = (q1 * q2^-1)^t * q1
``````

where `q1` and `q2` are the two quaternions being interpolated, `t` is a value between 0 and 1 that represents the interpolation parameter, and `q(t)` is the interpolated quaternion.

SLERP works by finding a quaternion that represents the shortest path between `q1` and `q2` along the surface of a sphere, and then interpolating along that path. The `q1 * q2^-1` part of the formula is the quaternion that represents the rotation from `q1` to `q2`. The `t` value is used to interpolate between `q1` and `q2`. Finally, the `q1` part of the formula is used to ensure that the interpolation starts and ends at `q1`.