Numbers in Game development | Part 2

in gamedev •  8 months ago

zombie.jpg

Hi!, I am Adam Saudagar, this is the second part of the blog series “Numbers in Game Development” if you haven’t seen the previous one, I highly recommend to do so, link is below
Numbers in Game development | Part 1


Part 2
Maths functions, Whaaaat?
thinking.png
So, in the last part we decided to make a zombie game where the zombie spawn rate increases exponentially with respect to the amount of zombies you killed. So what does the word “exponentially” mean? You might have seen equations like in school, and you would know that its answer is



lets see what happens if we change this equation a bit, replace 4 with x and make that equal to y



This is known as exponential function, what does it mean then? It simply means that, if you want the answer to become 16 like before then we have to plug in x = 4,
graph1.png
so if you draw a line on x = 4, it will intersect the exponential curve on y = 16, as you see in the graph. Similar to x=4, if you plug any value for x and you will receive a value of y. Try it yourself by clicking on the graph above, drag the slider, you will get the value of y, for the x you have selected

I am growing Exponentially :D

growing_up.png

By trying the graph yourself, you might have noticed that as the value of x increases, not only y increases, but the rate of which y increases also increases. If you don’t understand this sentence, compare the exponential graph with linear graph, click the graph to try it out
graph3.png
In linear graph, the rate by which y increases with respect to x remains constant. In the case of given equation, if x is increased by 1, y will increase by 2, no mater what the previous value of x was. But in case of exponential function. If previous value of x was 1 and you increase it by 1, y will increase by 2, whereas if the previous value of x was 2 and you increase it by 1, y will increase by 4. This is what exponential function is.

Rise my zombies
As I told you before, looking at just the maths will confuse anyone, but when we see the maths working in a game, we will understand how these maths functions work. So lets see how we will implement this maths in the game. First I will show you how the game works with a constant frequency of zombie spawning, then we will use linear growth function to understand how the variables in the game and maths variables links. And then we will implement our exponential growth function. Piece of cake, isn’t it?

The undead variables

photo2.png

Lets just see what are the variables we will use

  • killCount (c) : this is the number of zombies we killed from the start
  • spawnFrequency (f) : this shows how many zombies will spawn in the map in 1 second, this is what we want to increase exponentially with respect to killCount
  • spawnIncreaseRate (r): this is the rate by which spawnFrequency increases
  • initialSpawnFrequency (fi) : this is the spawnFrequency in the start of the game
  • finalSpawnFrequency (ff) : this is the maximum value for spawnFrequency. We are keeping this because we don’t want our player to get destroyed by crazy amounts of zombies LOL
  • killCountThreshold (cf) : this is the threshold amount of kills after which frequency becomes constant.

Enough with the boring stuff

angry.jpg

Yeh, I know this has been boring enough, lets just see how the game works right now, with constant spawnFrequency.

I know the frequency is constant, but just to visualize it, lets plot a graph of killCount vs spawnFrequency, to have a better understanding of the situation
gamegraph1.png

As you can see, for any value of killCount, spawnFrequency stays the same. We don’t want that. So how do we want it to be?

Growing linear
What we want to do is, increase zombie spawnFrequency as the killCount increases, lets try to make a rough graph for it.

drawnGraph.jpeg

In the graph, we can see that when killCount is 0, the spawnFrequency is fi, that means in the start of the game when the player doesn’t have any kills, spawnFrequency will start at fi. We can see that from fi to ff the graph looks like a line, so we can plug in equation for line, which is
y=mx+c

Lets check it out in desmos and see how it looks, click the equation above, try changing m and c to see how it effects the graph.
We were dealing with the variables we defined earlier, but what are these new variables, y, m, x, c. By simply comparing the graph we drew and the graph in desmos we can see that, y represents the Y-axsis, which is spawnFrequency. x represents the X-axis which is killCount. m represents the rate at which y increases, this also known as gradient in mathematics, that is spawnIncreaseRate. And c is called y-intercept in mathematics, which means the point on y axis where the line cuts or the value for y when x is zero. Hence c = fi. So writing the equation in terms of variables we are using for the game we get

f = rc + fi

looking back at our graph, line just goes from fi to ff after that the frequency stays at ff so
if 0 <= c <= cf
 f = rc + fi
if c > cf
 f = ff

Coding It Linearly

cat-computer.jpg

We have finally calculated the equation for spawnFrequency but we can’t simply plug these formulae in the code. First we’ll have to decide what we want to adjust manually and use those to calculate rest of the formuales.
The varables we’ll need to enter manually are initalSpawnFrequency (fi), finalSpawnFreqency (ff) and spawnIncreaseRate (r). We will get killCount (c) from the game itself, as the player kill enemies killCount will increase. And as for f, we calculated the formulae in the previous section. Only variable left to calculate is cf. To calculate cf, what we will have to do is plug the point (cf, ff) into the equation (f = rc + fi) and solve for cf.

solvingcf.jpeg

Watching it work (Linear)
So the equation we got is cf, now lets code these equations and see the result for ourselves
code.png

values.png

When the game executes, Start() fuction runs once then, Update() function loops for each frame. In the Start() function, first we use the killCountThreshold formula that we calculated,
then we initialized spawnFrequency to initialSpawnFrequency,
then we start spawning process by calling StartCoroutine(spawn()).

The first statement in Update() function is called Ternary operator, its synatx it,


when the condition is true, result will be assigned to exp1, if the condition is false, result will be assigned to exp2. This statement will work as same as the code which is commented. We use this for the equations of spawnFrequency. Then we give custom values for the initalSpawnFrequency= 0.5, spawnIncreaseRate=0.5, finalSpawnFrequency=10.



gamegraph2.png

As you can see, we can literally visualize the graph that we calculated by maths into the game itself. This is the beauty of mathematics.

In the next part we will make it so the spawnFrequency grows exponentially instead of linearly, Thanks for reading my post, follow me to stay tuned for upcoming parts, and don’t forget to press the like button. I’ll see you guys later.

Squad fam out!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

you put in a lot of effort! take my upvote :)

·

i would recommend you post your articles on utopian-io ( you can get extra rewards [I got 25$ :)])

·
·

I joined utopian-io the other day, but felt that my tutorials wouldn't be right on there; given I'm trying to write a commerical game, thus my content isn't open source, am I right Wil1liam???

·
·
·
·

lol thanks :D