Why I love maths

Leave a comment

November 9, 2015 by jonmillymiles

Maths tends to be one of those subjects that people either love, or loath. Those who love it like its pure nature – the is a method and as long as you follow that method all it will always work. Others, like me, love the way that you can solve problems with it and use it to describe the world around us. Then there are those people that hate it, and who can blame them, because it is often such an abstract concept that it seems almost meaning less – as my daughter once said, “Who would ever use trigonometry anyway???”.

I do.

So what does this have to do with procedurally generated content… well everything really. But more importantly, at the moment, what does this have to do with my 2D array that failed… well let me explain…

An array is a sequential structure of data elements, basically a list. You access it through an index (a pointer in programming speak) which identifies an element. A 2D array, an array of 1D arrays has two pointers, as mentioned before, so the trick is to know how to make a 2D array out of a 1D array… and for that we need maths.

In essence what we do is break our list down in to our rows, sections of X many elements. This looks like the picture below:

1D Array-UE4It works, try it. And because it is maths it works for any width and depth of map.

And so that’s how I intend to build it – using this formula as the basis for everything within the map where I need to reference the map data.

But to begin with I need to try it out to make sure that it works, last time I didn’t try it until I had developed all of the code so this time I’ll do a little bit first as it is less complicated.

To begin with I added another feature to my Create Tile function – Test Text. This would then display the relative X and Y values of each tile.

Test Text

I then set about rebuilding my functions. Firstly I used the Resize node to instantly build an array of the correct size and this is where I had an epiphany. If I add one to the Width and Length of the map variables I gain an the capacity to loop through the for loops without taking one from the map width and length each time. While this may seem somewhat trivial it saves the number of instructions used each cycle and therefore optimizes the game… slightly.

So, first we size the array – (Width+1) x (Length + 1) – my default value is 20 for both so this is essentially 21 x 21 or 441 tiles.

Size Array

Then we need to populate the array. We could do this at the same time but I want the array to already exist so that there is less confusion in the code.

Fill Array

This code gives us a 1 in 5 chance of creating a tile, purely so that we can see it working. I may add some code in later to place a one square border around the map just so that it stands out a little more.

Now I need to access the array and draw it using the Create Tile function I made earlier.

Draw Array

When this now runs you get a scattering of tiles over the map, all labelled accordingly and correctly and randomized each time it is run as below.

What I then decided to do was to link in the room generation code so that it ran over the top, this would create some rooms, but also double up on some tiles. I also changed the tile colour so that the rooms were distinctly different.

As you can see when it works it works well, but there are some problems:

  • Some of the rooms overlap
  • The rooms overlap the tiles
  • When the rooms overlap we get glitching
  • There are no structured corridors – yes we need to raise this one now

In essence what I need to do is to move the room building algorithm into the map building blueprint and get it working in the same space as the random tiles. This should be relatively easy to do. Once they are drawn from the same array, and hence only one tile rather than 3 in some cases the glitching will stop.

Stopping the overlap will be more challenging as I will have to test each square to see if it occurs before I place the room. Once we have the rooms we can maybe look at the corridors.

Oh well… to finish, here is the map with a border to define the play area – I’ve made it a dark red to so that it stands out.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog Stats

  • 12,284 hits

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 580 other followers


%d bloggers like this: