Maths In Games Algebra

x and y positions example for maths in games

In the last “Maths In Games” post (click here to read Maths In Games Intro), we used X and Y coordinates to place our character on screen.

x and y positions example for maths in games

This time we are going to talk about algebra and moving our character around.

Algebra is the part of mathematics in which letters and other general symbols are used to represent numbers and quantities in formulas and equations.

We will first use algebra to make basic movements to our character and then after that we will look at other variables to make some movements a bit more lifelike.


Maths In Games : Algebra

From the last post we could see our hero’s x and y positions were first set as

  • hero.x = 0
  • hero.y = 10

To move our character we would add code for when a key is pressed.  For the right key, we want to increase the X position and for the down key we want to increase the Y position.  The left and up keys would be the opposite.

When we first added our character to the screen, we defined the x position as 0 and y position as 10.  Can you guess how we would define the x and y positions after pressing the right or down keys?

For the right key we would add 1 to the current x position (hero.x).  Code for the right key might look like this:

  • hero.x = hero.x + 1

The second “hero.x” represents the numeric value of our hero’s current x position and will be used as part of the calculation to get our new hero.x position, which will be equal to the current x position (0) plus 1.

After pressing the right key once, our new x position will be 1.

If we press the right key again the new x position will be 2, which is equal to the current x position (1) plus 1, and so on.

Our Y position is not changing here when we press the right key, so we leave it as it is.

But, if we were pressing the up or down keys then we would want to change the Y position.

The code for each arrow key might look like this:

  • Left: hero.x = hero.x -1
  • Right: hero.x = hero.x+1
  • Up: hero.y = hero.y-1
  • Down: hero.y = hero.y+1

This is an overview on the basic movements in a game, these are very basic movements.

If you were playing a platform game, you want to limit how high the character can jump, so you can’t just keep pressing the up key so your character effectively flies.

If you had a space ship moving around in space you might want to add things like thrust, and acceleration, or you could add friction and gravity to change how your character moves.


Maths In Games: Collision Detection

Another use of x and y coordinates in games would be to detect a collision, if two ‘items’ occupy the same space what do you do?

If your character walked into a wall, you don’t want him to walk through the wall, so you would limit his movement in the direction of the wall.

If you shoot an enemy, your enemy is hit when the bullet collides with the enemy.

If your character jumps up to a higher platform, you want him to land on the platform (a new Y position) and not to fall back down to where he started from.



They X and Y coordinates along with some other variables can be used to improve the physics of your game world, or they can be used to make it harder.  In the next post I will talk about some of these.

Click Here to Leave a Comment Below 0 comments

Get every new post delivered to your Inbox

Join other followers:

%d bloggers like this: