# Do you remember the days – Maths in games

Do you remember the days being back in school sitting at the back of a maths class, when you looked down at your book and notes and it resembled a bad scrabble rack more than what you always thought was a “numbers” subject?

Many years later I found myself creating a game where I had to use the old graph with x’s and y’s and replacing numbers with letters to move things around the screen. A lot of games (probably most of them) with movement use this graph set up.

*Disclaimer! This is not a coding tutorial, or programming lesson, the example used is not specific to any particular code or compiler – it is a general description only. The actual calculations are a bit more complicated than below, but this isn’t the time or place to get into that.*

Imaging your screen is a graph.

- The bottom left is 0 on the X axis and 0 on the Y axis.
- The top left is 0 on the X axis and 100 on the Y axis
- The top right is 100 on the X axis and 100 on the Y axis.
- The bottom right is 100 on the X axis and 0 on the Y axis

To position our character on screen, we create two variables x_position and y_position. These variables will have a numeric value which will position our character on our graph/screen. If the character starts in position 20 on both the X and Y axis we would declare our variables with

- x_position = 20
- y_position = 20

If we press the left key, we want the character to move left 1 space on the X axis. The code for pressing the left key is

- x_position = x_position – 1

This takes our original value of 20 and reduces it by 1. Our new position on the X axis is 19. If we press the right key, our x_position increases, the up and down keys effect the position along the Y axis

- LEFT-KEY : x_position = x_position – 1
- RIGHT-KEY : x_position = x_position + 1
- UP-KEY : y_position = y_position + 1
- DOWN-KEY : y_position = y_position – 1

From our original position, if we pressed the right key 20 times and the up key 40 times, our new position would be 40 on the X axis and 60 on the Y axis.

This is a very very basic overview of maths in game programming and how its used to position and move things around your screen. So far this was interesting (at least for me).

The real fun began though when I wanted to create a pinball game!!

Do you remember the days when you sat in a maths class listening to a teacher rattle on about Pythagoras and you thought “when am I ever going to use this in real life?”. That day has come!

It’s easy to work out if a flat surface bounces off another flat surface, what direction it should bounce in – it’s basic laws of reflection that the angle of incidence equals the angle of reflection on any plane – we all know that!!

However, any pool or snooker players out there will tell you that its not simple when it comes to curves or round things colliding. Instead of working out the movement of one flat side bouncing off another flat side (laws of reflection), we now have several possible degrees (360 I’ve been told) of movement.

If our red ball collides with the orange-bouncer, what direction does our ball go in?

How is this done?…… Enter, Mr Pythagoras and his friends sin, cos and tan. By working out angles and points of movement and collision, a triangle can be virtually created, giving you the direction of movement.

Again, this is not a game programming tutorial, but a general blog. If you want to get into coding games, refresh yourself with Newton and his laws, Snell’s laws, Mr Pythagoras and a host of other letters and acronyms that will make your code look like an address in Wales. Maths in games is a big part of it, and sometimes more complicated than the coding itself.

So if you do remember the days of boring-frustration in maths class, you’re not finished with them yet.