# How are imaginary numbers useful in video game creation?

I learned about imaginary numbers in school and they seemed so impractical, so I asked my teacher what they were used for and he said "video game creation".

I just want to know if this is true and if so how is it used during the creation process.

• Your teacher seems to have been extremely vague, I have no idea what they meant. The only thing I could imagine that could have been meant is some specific geometric calculations, but you could just as well use two regular numbers for that. The real point of imaginary numbers is that it's a tool that is often convenient for intermediary steps of calculations. Here is a good video by Numberphile and 3blue1brown about a very similar question: What higher dimensions are used for in Maths. youtube.com/watch?v=6_yU9eJ0NxA&t=2s – Fabian Röling Jan 31 at 4:21
• I believe your teacher's answer was not very precise. You might need complex numbers in gamedev, but it's pretty hard to come up with an example. However, there's one field where they are indispensable: DSP (Digital Signal Processing). Everything that has to do with sound processing, probably with video. Your media player, MP3 decoder, cell phone, HDMI video transfer: dig deep enough and you'll hit a Fourier transform somewhere, and a bunch of other complex number manipulations. – IMil Jan 31 at 14:37
• One for Fourier transforms, see Fast Fourier Trasform, I've seen FFT's used for Computational Fluid Dynamics also – Felipe Gutierrez Jan 31 at 20:35
• I'd love to see an answer that goes deeper into FFT and DSP applications. I don't have much hands-on experience with these, so I gave them rather short shrift in my post. It's a rich area worth showing off for a curious audience here! – DMGregory Jan 31 at 20:59
• Imaginary numbers are used in a heck of a lot more than video games. Phase angles comes to mind, but first time I had a practical use for them was with demodulating radar signals. – Mast Jan 31 at 21:51

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.

• What academic class would teach this?(I'm in high school, is this more of a college level or could I learn this now) – Daosof Jan 31 at 5:40
• @Daosof I'll confess I taught myself this, mostly from Wikipedia and game engine documentation. Quaternions are super practical for gamedev and robotics, but most of us can let our math library do the heavy lifting and never think about the exact computations under the hood. I don't think they're taught much until third year university or later, when discussing division algebras. (Fun fact: there are only four number systems where we can define division over everything but zero: real numbers, complex numbers, quaternions, and octonions - there are no others possible!) – DMGregory Jan 31 at 5:47
• @Daosof that would be Linear Algebra. The most valuable branch of math for 3D graphics and modeling (IMHO). – IMil Jan 31 at 14:18
• @Daosof If you're going into robotics engineering, you will probably run into complex numbers frequently in your electrical engineering classes, so I'm afraid you're not safe from them yet. – user3067860 Jan 31 at 20:59
• The best place I've found to understand how quaternions relate to rotation is this page, created by 3Blue1Brown, who makes some of the best mathematics videos in existence – BlueRaja - Danny Pflughoeft Jan 31 at 21:58

DMGregory already explained how Quaternions are often used for rotation in 3d space. But Quaternions are already 2 levels of understand above imaginary numbers.

When you want to go one level simpler, then you might find it interesting that you can use Complex numbers for rotation in 2d space.

When you want to rotate a set of 2d points by n degrees, then you need to use this algorithm:

xnew = xold * cos(angle) - yold * sin(angle)
ynew = yold * cos(angle) + xold * sin(angle)


But a rotation by n degrees can actually be encoded as a complex number. When you treat the coordinates as complex numbers too, then you can do a rotation by simply multiplying the point by the rotation.

rotation.real = cos(angle)
rotation.imaginary = sin(angle)
newPoint = oldPoint * rotation

• I mention this isomorphism between complex multiplication and 2D rotation in my answer, but I've never yet encountered a game that performs its 2D rotations this way. They seem to almost universally use 2x2 or 2x3 matrices instead. The result is equivalent, but it means we're manually storing the 4 coefficients of FOIL instead of using the fact that i*i=-1. And when we just multiply two vectors, we usually do it component-wise, not treating the y coordinate as imaginary. Maybe I'm overlooking a place where this property of complex numbers is used in games? – DMGregory Jan 31 at 12:39
• @DMGregory Matrices give you extra degrees of freedom. If you only need rotation, imaginary numbers work great - I've used them for rotation in the past (in fact, even in one of my first games, a Tetris clone in Pascal :D). But a 3x3 matrix gives you a simple way to rotate, scale, translate etc.; it's more expensive and somewhat annoying to work with, but more general. – Luaan Feb 3 at 8:48

Complex numbers have an enormous range of applications, but here we'll stick with those in game development.

Others have already noted how the "vanilla" 2-dimensional complex numbers $$\\Bbb C\$$ describe rotations in 2 dimensions, and the quaternions $$\\Bbb H\$$, a 4-dimensional "hypercomplex" system, describe rotations in 3 dimensions.

Another, less famous application of $$\\Bbb C\$$ is in representing the projections of 3-dimensional objects into a 2-dimensional plane identified with $$\\Bbb C\$$. (For example, Eq. (2) in the above paper notes that complex numbers $$\\alpha,\,\beta,\,\gamma\$$ are the projections into the plane of the vertices neighbouring 0, for some cube with a vertex projected to 0, iff $$\\alpha^2+\beta^2+\gamma^2=0\$$.) While such projections are obviously important in 3-dimensional games, which model a 3-dimensional world but ultimately have to display it in 2 dimensions on our devices' screens, I'm not sure whether games make use of this complex-number insight in practice.

Other authors have already discussed how important complex numbers can be for object rotation. Here I am adding a couple of other examples where we can see the use of complex/ imaginary numbers

• A really cool application of complex numbers is Fractals which is used in procedural generations in game development. Some of the popular fractals such as Mandlebrot set, Julia Set are basically built of iterations with complex numbers.
• Imaginary numbers are also used in procedural wave generation(using Fast Fourier Transforms). There are other methods of wave generation as well.