Maths In Games Variables and Physics

maths in games image showing how physics will be demonstrated

This is the 3rd post in the Maths In Games series.

The previous posts have given a very brief overview of some of the ways that maths can be used in computer games.

Had algebra been explained to me in school using examples of its use in games, it might have made things more interesting for me.

image of character looking bored with books

 

Maths In Games: Recap

We have seen from previous posts in this series, that characters and items have to be placed on screen with X and Y coordinates.

The top left of your screen is 0 on both the X and Y axes.

As you move down away from the top, the Y value increases and as you move away from the left the X value increases.

To position our character (hero) 50 pixels down from the top and 100 pixels from the left hand side of the screen you would use something like:

  • hero.x = 100
  • hero.y = 50

 

If the player pressed the right key, you want to increase the X position by 1 pixel for example.

To do this, you define the new hero.x value as the current hero.x value plus 1.  This might look like:

  • hero.x = hero.x + 1

The left key would reduce the X position, the up key would reduce the Y position and the down key would increase the Y position.

 

Maths In Games: Variable And Physics

In the last section, when I mentioned moving a character using “hero.x = hero.x + 1”, hero.x is a variable used to represent the X position of our hero.

The first hero.x will define the new X position of hero.

The second hero.x represents the current X position of our here and will be used in an equation to get our new position.

These are very basic, and after some time (depending on your game) some very boring movements.

We can add some physics to our movements using more variables in our equations.

maths in games image showing how physics will be demonstrated

In the image above, the screen is divided up into the world above water and the world below water.

Above water, we want our character to move faster than under water, but we also want to make him prone to gravity which drags them down unless the up key is used again to reduce the Y position.

Under water we want our character to move slower, but we also want to make him prone to floating which will raise him unless the down key is used to increase the Y position.

 

The character’s Y position can be checked to see if he is above or below the water level.

Additional variables can be created and changed if the character is above or below water.

  • gravity = 1.05 above water and 0.95 below water
  • friction = 1 above water and .05 below water

The equation for pressing the right and down keys, respectively could be:

  • hero.x = hero.x + 1 + friction
  • hero.y = hero.y + (1 * gravity)

Taking the right key first.  If our hero’s current X position is 50 and he is above water, pressing the right key will update as follows:

  1. hero.x = 50 + 1 + 1:   52
  2. hero.x = 52 + 1 + 1:   54
  3. hero.x = 54 + 1 + 1:   56

Starting again from position 50, if our character was below water, pressing the right key 3 times will update as follows:

  1. hero.x = 50 + 1 + 0.5:   51.5
  2. hero.x = 51.5 + 1 + 0.5:   53
  3. hero.x = 53 + 1 + 0.5:   54.5

As you can see the hero is moving slower under water.  After 3 key presses he moved from X position 50 to 56 above water and from 50 to 54.5 under water.

 

Now let’s look at what happens when we press the down key.

First above water, we will start from Y position 30 and press the down key 3 times, this will look like this:

  1. hero.y = 30 + (1 * 1.05) :  31.05
  2. hero.y = 31.05 + (1 * 1.05):  32.1
  3. hero.y = 32.1 + (1 * 1.05):  33.15

Next, if our starting Y position was 70 and the hero was underwater, pressing the down key 3 times would look like this:

  1. hero.y = 70 + (1 * 0.95):   70.95
  2. hero.y = 70.5 + (1 * 0.95):   71.9
  3. hero.y = 71.9 + (1 * 0.95):  72.85

Above water, our hero moved faster (3.15 pixels) because gravity was helping the fall, but underwater he only moved down by 2.85 pixels because buoyancy was slowing his descent.

The gravity and friction variables can also be used even when a key is not being pressed.

If the character is underwater a loop can repeat until the character is at the water level.

This loop might be “hero.y = hero.y * gravity”.  Without pressing any keys our character’s Y position will gradually decrease which will look like they are slowly floating to the top.

Likewise, if the hero was above water you might have similar code, but this will gradually increase the Y position which will look like the character is falling.

 

I have made games on the past which were based in space and used a thrust variable along with friction.

The idea was that instead of pressing the right key and moving the character 1 pixel to the right, the character was moved with an initial thrust to the right and continued moving to the right, but slowing down as the initial acceleration decreased.

 

In the next Maths In Games post I will look at the dreaded Pythagorean theorem!

When I was working on a pinball game I had to use this theorem, and it was at that moment I first got my flash back to sitting in school wondering when would I ever use algebra, trigonometry, etc. in the real world!

email
Click Here to Leave a Comment Below 0 comments
Follow

Get every new post delivered to your Inbox

Join other followers:

%d bloggers like this: