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.
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityI 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.
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.
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
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