Why math matters in computer science (Part 1)

Michael DiBernardo
6 December 2013

Above:Screenshot from the SuperTuxKart video game (Supertuxkart development team)

A question I often hear from students who want to pursue computer science as a career is, "Why do I need to know so much math?"

To answer that question, let's look at what it takes to program a very simple car-racing video game. The game will takes place in a two-dimensional landscape, which we'll call the map. The goal is to complete laps in the fastest possible time and the car is very squat and square, like a VW Beetle.

As a game designer, you need to create a simple model of the game world so the computer can properly respond to user commands and make things move around on the screen.

The simplest model is a grid, where (0,0) represents the top-left corner (yes, just like in math class!). The car needs a position on the grid, so let's put it at (1,1):

If all we had to do was place cars on a map, we'd be done. But we also need the cars to move!

Let's pretend that each box on the grid represents one square metre of space. When the user pushes the up key, the car will accelerate at a rate of one metre per second. The car will have a maximum speed of two metres (or boxes) per second.

Fast fact: One of the first car racing video games was released in 1974. "Speed Race" was created by Tomohiro Nishikado and was the first arcade game to feature scrolling graphics. Here, we run into a problem. Computers don't update the screen just once every second. Imagine how weird and choppy that would look! Instead, a computer refreshes the entire screen (or frame) at a frequency called the frame rate.

In most video games, the image on the screen is updated between 30 and 60 times per second, which means the game has a frame rate of 30 or 60 frames per second (fps). Of course, physicists already have a unit they use to measure frequency, called Hertz (Hz). But computer scientists like to come up with their own names for things…

Let's get back to the program… The model of the game world needs to change with every frame so the computer has something new to display on the screen. That means we need to figure out much to accelerate the car every frame, not just every second.

Let's assume the game runs at 30 fps. If you want acceleration to be 1 m per second, what should the acceleration per frame be? Let's use dimensional analysis to find out:

1 (m/s) / second * 1 second / 30 frames = 1/30 (m/s) / frame = 0.033 (m/s) / frame

So you need to increase the car's velocity by 0.033 m/s every frame.

So far, we've looked at how the game world will be modelled and how to make the cars accelerate. And we've already had to use some basic math to get this far. However, we still haven’t dealt with how to make the cars turn! Let’s leave that problem for another article.

Michael DiBernardo

Michael DiBernardo has his B.Math in Computer Science/Bioinformatics from the University of Waterloo (2005) and M.Sc in Computer Science from the University of British Columbia (2007). He has been a practicing software engineer for 10 years. In that time he has taught computer science at the highschool and university level, worked in industry at companies such as Google and Novell, and has been involved in several startups, most recently shopable.tv -- a software platform for purchasing products through your television. He is currently the chair of PyCon Canada 2013 (http://pycon.ca), as well as the Toronto organizer of the Global Day of Coderetreat (http://globalday.coderetreat.org/).

Comments are closed.