Mathematics in Computer Games

Mathematics in Computer Games


Video games are played by approximately 3.1 billion individuals [1], a figure that has risen by over one billion in the last seven years. There was a significant surge in the number of players during the Covid-19 lockdowns. In the wealthiest countries about two-thirds of individuals play and nearly half of them are women.

The biggest games, so called AAA (‘triple A’) titles can be developed by teams of over 500 people and can take around five years to make, costing several hundred million pounds. There is also a thriving industry of smaller ‘indie’ games, created by small teams (or even individuals) generally without the backing of a large publisher. I am based at the University of Warwick and there are over 50 companies involved in making games in the town of Leamington Spa, just a few miles down the road. Andrew Oliver, who is interviewed in this issue, and his brother Philip, played a significant role in creating this environment.

Platforms for playing games include consoles, PCs, tablets and smart phones. The spread of smartphones means that huge numbers of people are carrying around a powerful gaming device and this has led to a much more diverse audience for games. Although many games are still action based, there are an increasing number of games which address education and ethics. Unlike in books and films, in a game the player has choices and responsibility. In the context of play, this can be used in a very powerful way.

At its most fundamental level a game involves characters and environments that interact according to a set of rules to produce a stimulating experience. Games involve responding to player inputs, checking for interactions between objects, assigning attributes to objects (often involving probability distributions), calculating how to display scenes and updating everything according to the events and rules in the game. All of these are implemented in code using mathematics. We can think of a game as a mathematical system which continually cycles between taking input from the player, mathematical processing and sending output back in an appropriate way (usually involving a graphical form) so that the player can react again.

A decade or so after the first electronic computers were seen, commonly in research and financial institutions, the first video games were made. Tennis for Two, created in 1958 is widely acknowledged to be the first video game. This was followed by Spacewar! in 1962 (see Figure 1). Both games simulated the movement of objects on a screen using mathematics. In Spacewar! a couple of spacecraft move subject to the gravity due to the presence of a star field and the thrust from their engines (which can be applied by the player).

Figure 1: Spacewar! on PDP1 computer
Figure 1: Spacewar! on PDP1 computer

We can imagine using Newton’s equations to calculate the paths of the spacecraft. The great advantage that a mathematician working with Newton’s equations has over the games developer is that for them time is continuous and infinitely divisible. As is required by calculus, it can be chopped up as much as is necessary and can even be considered in the limit as a change in time tends to zero. For the games developer, time comes in discrete positive chunks with a definite positive minimum size, determined by the tick of the processing unit, and even the size of those chunks can vary if the processing unit is given too much to do. Thus, not only do games developers use ideas from mathematics for their purposes, they often have to adapt them too. Some examples of innovative mathematics in computer games are described below.

Trigonometry
In this issue, Andrew Oliver discusses how he used a ‘256 degree circle’ for trigonometry which was much more efficient in 8-bit computing.

Dijkstra’s algorithm
The algorithm that determines the path the ghost uses to chase Pac-Man, and for pursuit in many of the latest games, is known to game developers as the A* algorithm. It’s a modified version of Dijkstra’s algorithm which was conceived in 1956.

Fibonacci sequence
In their seminal space trading game, Elite, released in 1984, David Braben and Ian Bell used a Fibonacci like sequence to store a detailed world of 8 galaxies, each with 256 planets. A single seed number is run through a fixed algorithm the appropriate number of times and creates a sequence of numbers determining each planet’s complete composition (position in the galaxy, prices of commodities, name and local details).

Reciprocal square root algorithm
The reciprocal square root algorithm featured in this issue is an ingenious use of logarithms and the way computers store real numbers.

Hamilton’s quaternions
Who would have thought that Hamilton’s quaternions, also featured in this issue, would have a place in computer graphics?

To say this list of examples barely scratches the surface would be a huge understatement.

In the Desmos, GeoGebra and Scratch CPD Workshop article there is a section about Year 8 teachers engaging students with game development using the Scratch programming the pupils met at primary school. Any novice attempting to make a game is on a direct path to mathematics as they encounter vectors, trigonometry and probability distributions, to name a few.

Imagine a budding games developer who is programming a ‘space invaders’ type game. They want the enemy spacecraft to move in an interesting way, perhaps they should move along the screen from left to right and also swoop around at the same time. Maybe this will lead them to the following equation

(t+ \cos (t), \sin (t)),

which they are going to use for the position of their spacecraft with t increasing in steps of 1/50 of a second, the frame rate for their game. They implement this and they see their spacecraft swooping across the screen (see Figure 2).

Figure 2: Spacecraft moving along a parametrised curve
Figure 2: Spacecraft moving along a parametrised curve

They feel great, they’ve solved their problem. Then they start to think about how they can make the second and third swoops faster! They need to really understand how the equations are working for this. What a wonderful way to meet trigonometry and parametric equations. What a great motivation to learn more to find out what is possible!

There are also great opportunities to make a career in game development. The number of UK jobs in the computer games industry has risen by more than 30 000 from 2011 to 2022, see Figure 3 [2].

UK games industry jobs
Figure 3: The number of jobs in the UK games industry, data from [2]

Hopefully the articles about gaming in this edition show how sophisticated the mathematics used in video games can be. It’s important, particularly for young people, that those considering a career in game development are aware of the value of developing their maths knowledge and skills. Many youngsters struggle to appreciate how mathematics can be a creative pursuit and how mathematics can be applied. Video game development addresses both of these powerfully and directly, it is a perfect vehicle to increase participation in, and appreciation of, our amazing subject.

Richard Lissaman
Guest Editor

References

  1. Bankhurst, A. (2020) Three Billion People Worldwide Now Play Video Games, New Report Shows, IGN.
  2. Department for Digital, Culture, Media & Sport et al. (2023) Economic Estimates: Employment in the DCMS Sectors, January to December, 2011–2022, published under Open Government Licence v3.0.

Reproduced from Mathematics Today, October 2023

Download the article, Mathematics in Computer Games (pdf)

Image credit: Spacewar!,  Kenneth Lu | Flickr, CCBY2.0
Image credit: Spacecraft, Ron Weickart | Network-Graphics
Published