Skip to main content
Adding note on FFT
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

One place that imaginary numbers get a lot of use in video games is in the use of quaternions to represent orientations and rotations of 3D objects.

Like complex numbers \$z = a + b \cdot i\$, a quaternion consists of both a real part and an imaginary part. But instead of just one imaginary axis, quaternions have three!

\$q = w + x\cdot i + y \cdot j + z \cdot k\$

Each of these imaginary units \$i, j, k\$ has the property \$i^2 = j^2 = k^2 = -1\$, just like the \$i\$ you might be used to in complex numbers. But they also have special rules for how they multiply with each other:

$$\begin{matrix} \bf \times & \bf 1 & \bf i & \bf j & \bf k \\ \bf 1 & 1 & i & j & k\\ \bf i & i & -1 & k & -j\\ \bf j & j & -k & -1 & i\\ \bf k & k & j & -i & -1 \end{matrix}$$

Why would we work with such a torturous thing? It turns out this construction has a very useful isomorphism. Similar to how the multiplication of unit complex numbers is equivalent to 2D rotations in geometry, multiplication of unit quaternions is equivalent to 3D rotations!

Specifically, we can express a rotation around the unit vector \$(x, y, z)\$ by an angle \$\theta\$ as the unit quaternion:

$$q = \cos \frac \theta 2 + \sin \frac \theta 2 \cdot( xi + yj + zk)$$

This is huge. It turns out that manipulating rotations in this form has some major advantages over other ways we might try to represent them in 3D:

$$\begin{matrix} & \textbf{quaternion} & \textbf{rotation matrix} & \textbf{angle triplets}\\ \textbf{storage} & \text{4 floats} & \text{9 floats} & \text{3 floats}\\ \\ \textbf{interpolating} & \text{rotates cleanly} & \text{distorts scale} & \text{tumbles wildly}\\ \\ \textbf{composing} & \begin{array} .\text{16 multiplies} \\ \text{+ 12 adds}\end{array} & \begin{array} .\text{27 multiplies} \\ \text{+ 18 adds}\end{array} & \text{gimbal lock}\\ \\ \textbf{reversing} & \text {3 multiplies} & \text {6 swaps} & \text {trig nightmare} \end{matrix}$$

(For more info on the hazards of using the wrong rotation representation, see this case of compounding error, this example of interpolation, this example of orientation ranges, this issue with wraparounds, and gimbal lock)

So most often, 3D game software will use quaternions, with all their imaginary number guts, as the way to store and track rotations. Especially so in places where rotations need to be stacked on top of one another or blended - like in animating the orientation of all the bones in the hierarchical rig of an animated character.

We'll still usually convert these to matrices when it comes time to render our meshes, since those let us fold in scale, mirroring, skew, and translation into a single representation, but working with quaternions as the intermediate saves us a lot of headaches.

There are of course places where the simpler two-component complex numbers come up too, though they're often more niche. One example is when we need to solve a high-order polynomial equation, like in this answer about planning parabolic trajectories to hit an accelerating target - something we'd need for an AI character to be able to accurately lob grenades in the path of a player. A quirky thing about cubic and higher-order polynomials is sometimes the fastest way to find the real-number solutions is to go via imaginary numbers along the way!

Imaginary numbers are also used in Fast Fourier Transforms for manipulating audio signals - like applying DSP effects to sounds in the game, or speech recognition for voice control, or beat detection for music games, etc.

One place that imaginary numbers get a lot of use in video games is in the use of quaternions to represent orientations and rotations of 3D objects.

Like complex numbers \$z = a + b \cdot i\$, a quaternion consists of both a real part and an imaginary part. But instead of just one imaginary axis, quaternions have three!

\$q = w + x\cdot i + y \cdot j + z \cdot k\$

Each of these imaginary units \$i, j, k\$ has the property \$i^2 = j^2 = k^2 = -1\$, just like the \$i\$ you might be used to in complex numbers. But they also have special rules for how they multiply with each other:

$$\begin{matrix} \bf \times & \bf 1 & \bf i & \bf j & \bf k \\ \bf 1 & 1 & i & j & k\\ \bf i & i & -1 & k & -j\\ \bf j & j & -k & -1 & i\\ \bf k & k & j & -i & -1 \end{matrix}$$

Why would we work with such a torturous thing? It turns out this construction has a very useful isomorphism. Similar to how the multiplication of unit complex numbers is equivalent to 2D rotations in geometry, multiplication of unit quaternions is equivalent to 3D rotations!

Specifically, we can express a rotation around the unit vector \$(x, y, z)\$ by an angle \$\theta\$ as the unit quaternion:

$$q = \cos \frac \theta 2 + \sin \frac \theta 2 \cdot( xi + yj + zk)$$

This is huge. It turns out that manipulating rotations in this form has some major advantages over other ways we might try to represent them in 3D:

$$\begin{matrix} & \textbf{quaternion} & \textbf{rotation matrix} & \textbf{angle triplets}\\ \textbf{storage} & \text{4 floats} & \text{9 floats} & \text{3 floats}\\ \\ \textbf{interpolating} & \text{rotates cleanly} & \text{distorts scale} & \text{tumbles wildly}\\ \\ \textbf{composing} & \begin{array} .\text{16 multiplies} \\ \text{+ 12 adds}\end{array} & \begin{array} .\text{27 multiplies} \\ \text{+ 18 adds}\end{array} & \text{gimbal lock}\\ \\ \textbf{reversing} & \text {3 multiplies} & \text {6 swaps} & \text {trig nightmare} \end{matrix}$$

(For more info on the hazards of using the wrong rotation representation, see this case of compounding error, this example of interpolation, this example of orientation ranges, this issue with wraparounds, and gimbal lock)

So most often, 3D game software will use quaternions, with all their imaginary number guts, as the way to store and track rotations. Especially so in places where rotations need to be stacked on top of one another or blended - like in animating the orientation of all the bones in the hierarchical rig of an animated character.

We'll still usually convert these to matrices when it comes time to render our meshes, since those let us fold in scale, mirroring, skew, and translation into a single representation, but working with quaternions as the intermediate saves us a lot of headaches.

There are of course places where the simpler two-component complex numbers come up too, though they're often more niche. One example is when we need to solve a high-order polynomial equation, like in this answer about planning parabolic trajectories to hit an accelerating target - something we'd need for an AI character to be able to accurately lob grenades in the path of a player. A quirky thing about cubic and higher-order polynomials is sometimes the fastest way to find the real-number solutions is to go via imaginary numbers along the way!

One place that imaginary numbers get a lot of use in video games is in the use of quaternions to represent orientations and rotations of 3D objects.

Like complex numbers \$z = a + b \cdot i\$, a quaternion consists of both a real part and an imaginary part. But instead of just one imaginary axis, quaternions have three!

\$q = w + x\cdot i + y \cdot j + z \cdot k\$

Each of these imaginary units \$i, j, k\$ has the property \$i^2 = j^2 = k^2 = -1\$, just like the \$i\$ you might be used to in complex numbers. But they also have special rules for how they multiply with each other:

$$\begin{matrix} \bf \times & \bf 1 & \bf i & \bf j & \bf k \\ \bf 1 & 1 & i & j & k\\ \bf i & i & -1 & k & -j\\ \bf j & j & -k & -1 & i\\ \bf k & k & j & -i & -1 \end{matrix}$$

Why would we work with such a torturous thing? It turns out this construction has a very useful isomorphism. Similar to how the multiplication of unit complex numbers is equivalent to 2D rotations in geometry, multiplication of unit quaternions is equivalent to 3D rotations!

Specifically, we can express a rotation around the unit vector \$(x, y, z)\$ by an angle \$\theta\$ as the unit quaternion:

$$q = \cos \frac \theta 2 + \sin \frac \theta 2 \cdot( xi + yj + zk)$$

This is huge. It turns out that manipulating rotations in this form has some major advantages over other ways we might try to represent them in 3D:

$$\begin{matrix} & \textbf{quaternion} & \textbf{rotation matrix} & \textbf{angle triplets}\\ \textbf{storage} & \text{4 floats} & \text{9 floats} & \text{3 floats}\\ \\ \textbf{interpolating} & \text{rotates cleanly} & \text{distorts scale} & \text{tumbles wildly}\\ \\ \textbf{composing} & \begin{array} .\text{16 multiplies} \\ \text{+ 12 adds}\end{array} & \begin{array} .\text{27 multiplies} \\ \text{+ 18 adds}\end{array} & \text{gimbal lock}\\ \\ \textbf{reversing} & \text {3 multiplies} & \text {6 swaps} & \text {trig nightmare} \end{matrix}$$

(For more info on the hazards of using the wrong rotation representation, see this case of compounding error, this example of interpolation, this example of orientation ranges, this issue with wraparounds, and gimbal lock)

So most often, 3D game software will use quaternions, with all their imaginary number guts, as the way to store and track rotations. Especially so in places where rotations need to be stacked on top of one another or blended - like in animating the orientation of all the bones in the hierarchical rig of an animated character.

We'll still usually convert these to matrices when it comes time to render our meshes, since those let us fold in scale, mirroring, skew, and translation into a single representation, but working with quaternions as the intermediate saves us a lot of headaches.

There are of course places where the simpler two-component complex numbers come up too, though they're often more niche. One example is when we need to solve a high-order polynomial equation, like in this answer about planning parabolic trajectories to hit an accelerating target - something we'd need for an AI character to be able to accurately lob grenades in the path of a player. A quirky thing about cubic and higher-order polynomials is sometimes the fastest way to find the real-number solutions is to go via imaginary numbers along the way!

Imaginary numbers are also used in Fast Fourier Transforms for manipulating audio signals - like applying DSP effects to sounds in the game, or speech recognition for voice control, or beat detection for music games, etc.

added 77 characters in body
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

One place that imaginary numbers get a lot of use in video games is in the use of quaternions to represent orientations and rotations of 3D objects.

Like complex numbers \$z = a + b \cdot i\$, a quaternion consists of both a real part and an imaginary part. But instead of just one imaginary axis, quaternions have three!

\$q = w + x\cdot i + y \cdot j + z \cdot k\$

Each of these imaginary units \$i, j, k\$ has the property \$i^2 = j^2 = k^2 = -1\$, just like the \$i\$ you might be used to in complex numbers. But they also have special rules for how they multiply with each other:

$$\begin{matrix} \bf \times & \bf 1 & \bf i & \bf j & \bf k \\ \bf 1 & 1 & i & j & k\\ \bf i & i & -1 & k & -j\\ \bf j & j & -k & -1 & i\\ \bf k & k & j & -i & -1 \end{matrix}$$

Why would we work with such a torturous thing? It turns out this construction has a very useful isomorphism. Similar to how the multiplication of unit complex numbers is equivalent to 2D rotations in geometry, multiplication of unit quaternions is equivalent to 3D rotations!

Specifically, we can express a rotation around the unit vector \$(x, y, z)\$ by an angle \$\theta\$ as the unit quaternion:

$$q = \cos \frac \theta 2 + \sin \frac \theta 2 \cdot( xi + yj + zk)$$

This is huge. It turns out that manipulating rotations in this form has some major advantages over other ways we might try to represent them in 3D:

$$\begin{matrix} & \textbf{quaternion} & \textbf{rotation matrix} & \textbf{angle triplets}\\ \textbf{storage} & \text{4 floats} & \text{9 floats} & \text{3 floats}\\ \textbf{interpolating rotations} & \text{rotates cleanly} & \text{distorts scale} & \text{tumbles wildly}\\ \textbf{composing rotations} & \begin{array} .\text{16 multiplies} \\ \text{+ 12 adds}\end{array} & \begin{array} .\text{27 multiplies} \\ \text{+ 18 adds}\end{array} & \text{gimbal lock} \end{matrix}$$$$\begin{matrix} & \textbf{quaternion} & \textbf{rotation matrix} & \textbf{angle triplets}\\ \textbf{storage} & \text{4 floats} & \text{9 floats} & \text{3 floats}\\ \\ \textbf{interpolating} & \text{rotates cleanly} & \text{distorts scale} & \text{tumbles wildly}\\ \\ \textbf{composing} & \begin{array} .\text{16 multiplies} \\ \text{+ 12 adds}\end{array} & \begin{array} .\text{27 multiplies} \\ \text{+ 18 adds}\end{array} & \text{gimbal lock}\\ \\ \textbf{reversing} & \text {3 multiplies} & \text {6 swaps} & \text {trig nightmare} \end{matrix}$$

(For more info on the hazards of using the wrong rotation representation, see this case of compounding error, this example of interpolation, this example of orientation ranges, this issue with wraparounds, and gimbal lock)

So most often, 3D game software will use quaternions, with all their imaginary number guts, as the way to store and track rotations. Especially so in places where rotations need to be stacked on top of one another or blended - like in animating the orientation of all the bones in the hierarchical rig of an animated character.

We'll still usually convert these to matrices when it comes time to render our meshes, since those let us fold in scale, mirroring, skew, and translation into a single representation, but working with quaternions as the intermediate saves us a lot of headaches.

There are of course places where the simpler two-component complex numbers come up too, though they're often more niche. One example is when we need to solve a high-order polynomial equation, like in this answer about planning parabolic trajectories to hit an accelerating target - something we'd need for an AI character to be able to accurately lob grenades in the path of a player. A quirky thing about cubic and higher-order polynomials is sometimes the fastest way to find the real-number solutions is to go via imaginary numbers along the way!

One place that imaginary numbers get a lot of use in video games is in the use of quaternions to represent orientations and rotations of 3D objects.

Like complex numbers \$z = a + b \cdot i\$, a quaternion consists of both a real part and an imaginary part. But instead of just one imaginary axis, quaternions have three!

\$q = w + x\cdot i + y \cdot j + z \cdot k\$

Each of these imaginary units \$i, j, k\$ has the property \$i^2 = j^2 = k^2 = -1\$, just like the \$i\$ you might be used to in complex numbers. But they also have special rules for how they multiply with each other:

$$\begin{matrix} \bf \times & \bf 1 & \bf i & \bf j & \bf k \\ \bf 1 & 1 & i & j & k\\ \bf i & i & -1 & k & -j\\ \bf j & j & -k & -1 & i\\ \bf k & k & j & -i & -1 \end{matrix}$$

Why would we work with such a torturous thing? It turns out this construction has a very useful isomorphism. Similar to how the multiplication of unit complex numbers is equivalent to 2D rotations in geometry, multiplication of unit quaternions is equivalent to 3D rotations!

Specifically, we can express a rotation around the unit vector \$(x, y, z)\$ by an angle \$\theta\$ as the unit quaternion:

$$q = \cos \frac \theta 2 + \sin \frac \theta 2 \cdot( xi + yj + zk)$$

This is huge. It turns out that manipulating rotations in this form has some major advantages over other ways we might try to represent them in 3D:

$$\begin{matrix} & \textbf{quaternion} & \textbf{rotation matrix} & \textbf{angle triplets}\\ \textbf{storage} & \text{4 floats} & \text{9 floats} & \text{3 floats}\\ \textbf{interpolating rotations} & \text{rotates cleanly} & \text{distorts scale} & \text{tumbles wildly}\\ \textbf{composing rotations} & \begin{array} .\text{16 multiplies} \\ \text{+ 12 adds}\end{array} & \begin{array} .\text{27 multiplies} \\ \text{+ 18 adds}\end{array} & \text{gimbal lock} \end{matrix}$$

(For more info on the hazards of using the wrong rotation representation, see this case of compounding error, this example of interpolation, this example of orientation ranges, this issue with wraparounds, and gimbal lock)

So most often, 3D game software will use quaternions, with all their imaginary number guts, as the way to store and track rotations. Especially so in places where rotations need to be stacked on top of one another or blended - like in animating the orientation of all the bones in the hierarchical rig of an animated character.

We'll still usually convert these to matrices when it comes time to render our meshes, since those let us fold in scale, mirroring, skew, and translation into a single representation, but working with quaternions as the intermediate saves us a lot of headaches.

There are of course places where the simpler two-component complex numbers come up too, though they're often more niche. One example is when we need to solve a high-order polynomial equation, like in this answer about planning parabolic trajectories to hit an accelerating target - something we'd need for an AI character to be able to accurately lob grenades in the path of a player. A quirky thing about cubic and higher-order polynomials is sometimes the fastest way to find the real-number solutions is to go via imaginary numbers along the way!

One place that imaginary numbers get a lot of use in video games is in the use of quaternions to represent orientations and rotations of 3D objects.

Like complex numbers \$z = a + b \cdot i\$, a quaternion consists of both a real part and an imaginary part. But instead of just one imaginary axis, quaternions have three!

\$q = w + x\cdot i + y \cdot j + z \cdot k\$

Each of these imaginary units \$i, j, k\$ has the property \$i^2 = j^2 = k^2 = -1\$, just like the \$i\$ you might be used to in complex numbers. But they also have special rules for how they multiply with each other:

$$\begin{matrix} \bf \times & \bf 1 & \bf i & \bf j & \bf k \\ \bf 1 & 1 & i & j & k\\ \bf i & i & -1 & k & -j\\ \bf j & j & -k & -1 & i\\ \bf k & k & j & -i & -1 \end{matrix}$$

Why would we work with such a torturous thing? It turns out this construction has a very useful isomorphism. Similar to how the multiplication of unit complex numbers is equivalent to 2D rotations in geometry, multiplication of unit quaternions is equivalent to 3D rotations!

Specifically, we can express a rotation around the unit vector \$(x, y, z)\$ by an angle \$\theta\$ as the unit quaternion:

$$q = \cos \frac \theta 2 + \sin \frac \theta 2 \cdot( xi + yj + zk)$$

This is huge. It turns out that manipulating rotations in this form has some major advantages over other ways we might try to represent them in 3D:

$$\begin{matrix} & \textbf{quaternion} & \textbf{rotation matrix} & \textbf{angle triplets}\\ \textbf{storage} & \text{4 floats} & \text{9 floats} & \text{3 floats}\\ \\ \textbf{interpolating} & \text{rotates cleanly} & \text{distorts scale} & \text{tumbles wildly}\\ \\ \textbf{composing} & \begin{array} .\text{16 multiplies} \\ \text{+ 12 adds}\end{array} & \begin{array} .\text{27 multiplies} \\ \text{+ 18 adds}\end{array} & \text{gimbal lock}\\ \\ \textbf{reversing} & \text {3 multiplies} & \text {6 swaps} & \text {trig nightmare} \end{matrix}$$

(For more info on the hazards of using the wrong rotation representation, see this case of compounding error, this example of interpolation, this example of orientation ranges, this issue with wraparounds, and gimbal lock)

So most often, 3D game software will use quaternions, with all their imaginary number guts, as the way to store and track rotations. Especially so in places where rotations need to be stacked on top of one another or blended - like in animating the orientation of all the bones in the hierarchical rig of an animated character.

We'll still usually convert these to matrices when it comes time to render our meshes, since those let us fold in scale, mirroring, skew, and translation into a single representation, but working with quaternions as the intermediate saves us a lot of headaches.

There are of course places where the simpler two-component complex numbers come up too, though they're often more niche. One example is when we need to solve a high-order polynomial equation, like in this answer about planning parabolic trajectories to hit an accelerating target - something we'd need for an AI character to be able to accurately lob grenades in the path of a player. A quirky thing about cubic and higher-order polynomials is sometimes the fastest way to find the real-number solutions is to go via imaginary numbers along the way!

Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

One place that imaginary numbers get a lot of use in video games is in the use of quaternions to represent orientations and rotations of 3D objects.

Like complex numbers \$z = a + b \cdot i\$, a quaternion consists of both a real part and an imaginary part. But instead of just one imaginary axis, quaternions have three!

\$q = w + x\cdot i + y \cdot j + z \cdot k\$

Each of these imaginary units \$i, j, k\$ has the property \$i^2 = j^2 = k^2 = -1\$, just like the \$i\$ you might be used to in complex numbers. But they also have special rules for how they multiply with each other:

$$\begin{matrix} \bf \times & \bf 1 & \bf i & \bf j & \bf k \\ \bf 1 & 1 & i & j & k\\ \bf i & i & -1 & k & -j\\ \bf j & j & -k & -1 & i\\ \bf k & k & j & -i & -1 \end{matrix}$$

Why would we work with such a torturous thing? It turns out this construction has a very useful isomorphism. Similar to how the multiplication of unit complex numbers is equivalent to 2D rotations in geometry, multiplication of unit quaternions is equivalent to 3D rotations!

Specifically, we can express a rotation around the unit vector \$(x, y, z)\$ by an angle \$\theta\$ as the unit quaternion:

$$q = \cos \frac \theta 2 + \sin \frac \theta 2 \cdot( xi + yj + zk)$$

This is huge. It turns out that manipulating rotations in this form has some major advantages over other ways we might try to represent them in 3D:

$$\begin{matrix} & \textbf{quaternion} & \textbf{rotation matrix} & \textbf{angle triplets}\\ \textbf{storage} & \text{4 floats} & \text{9 floats} & \text{3 floats}\\ \textbf{interpolating rotations} & \text{rotates cleanly} & \text{distorts scale} & \text{tumbles wildly}\\ \textbf{composing rotations} & \begin{array} .\text{16 multiplies} \\ \text{+ 12 adds}\end{array} & \begin{array} .\text{27 multiplies} \\ \text{+ 18 adds}\end{array} & \text{gimbal lock} \end{matrix}$$

(For more info on the hazards of using the wrong rotation representation, see this case of compounding error, this example of interpolation, this example of orientation ranges, this issue with wraparounds, and gimbal lock)

So most often, 3D game software will use quaternions, with all their imaginary number guts, as the way to store and track rotations. Especially so in places where rotations need to be stacked on top of one another or blended - like in animating the orientation of all the bones in the hierarchical rig of an animated character.

We'll still usually convert these to matrices when it comes time to render our meshes, since those let us fold in scale, mirroring, skew, and translation into a single representation, but working with quaternions as the intermediate saves us a lot of headaches.

There are of course places where the simpler two-component complex numbers come up too, though they're often more niche. One example is when we need to solve a high-order polynomial equation, like in this answer about planning parabolic trajectories to hit an accelerating target - something we'd need for an AI character to be able to accurately lob grenades in the path of a player. A quirky thing about cubic and higher-order polynomials is sometimes the fastest way to find the real-number solutions is to go via imaginary numbers along the way!