Dealing With Numbers (Part 2)
Author: | Muz
|
Submitted: | 3rd July, 2009
|
Views: | 5780
| Rated: |
|
|
This article assumes you've read Part 1 and know the basics of putting numbers together. Now, it's time for the harder stuff.
Nonlinear math
Sometimes a linear formula just won't do it. Here, I'll show you some non-linear shapes. Getting the shape is one thing, but the values will be much harder to balance! Use them only when you feel it's necessary. This article is just to tell you how to get the shape of the graph you want. You'll have to figure out the number yourself... feel free to multiply or add to parts of the equations and see what you get.
Exponentials
Exponentials means that more you have of something, the more of an effect it has.
However, they are extremely powerful, so you should consider having a maximum on them.
Guess which is the exponential?
http://i262.photobucket.com/albums/ii89/smuz86/articles/2x.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/ex.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/10x.png
From left to right, it's 2^x, 2.7183^x, and 10^x.
In case you haven't realized it, 2.7183^x = exp(x).
The constant you set it on affects the steepness of the curve.
Note that the value is always 1 when x = 0. When x is less than 0, it goes down very slowly.
An easier way to dampen it is to set it to exp(x/C), where C is any number.
http://i262.photobucket.com/albums/ii89/smuz86/articles/exover1.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/exover2.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/exover12.png
From left to right, it's e^x, e^(x/2), e^(x/12).
As it goes lower, it becomes a lot less steep.. eventually becoming a straight line as x approaches 0.
Examples of exponential use
Exponentials tend to throw things out of balance. The Gompertz function (see below) is better for when you need an exponential, because it puts a maximum on them.
Interestingly, games that deal with money usually function on an exponential scale. Say, if one house costs $1000 and gives you $100 per month, you need $1000 to have your first house. After 10 months, you get a second house, then another 5 months before your third, 4 before 4th, 2 for 5th... and it increases at a faster and faster scale.
That's when you'll need to let the prices of higher buildings to be exponential. One sample formula would be
Price = 1000*exp(Level/2)
So, you get:
Lvl 0: cost $1000 Lvl 1: cost $1650
Lvl 2: cost $2720 Lvl 3: cost $4480
And so on. This also works very nicely with things like research costs or unit costs. But remember, this only works in games where money makes more money. In games like Starcraft or Age of Empires, the money runs out eventually, so the cash flow goes up then back down.
Logarithms
A logarithm is pretty much the opposite of an exponential.
While exponentials become more significant the higher it is, logs have less of an effect as x increases.
In fact (ln(exp(M)) = M, so it's the exact opposite.
http://i262.photobucket.com/albums/ii89/smuz86/articles/logx.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/logxmillion.png
To the left is log(x). Right is log(x*1000000).
Get it? While exponentials are like a mutation that gives a variable super strength.. logarithms are ultra-powered tranqualizer darts that can take down The Hulk. Even if you have a huge number, a logarithm will hold it back.
Example of logarithm use
Logs make more less effective. My favorite use for it is for bonuses.
For example, hiring a receptionist gives your company a bonus. Two receptionists will give it a slightly higher bonus. If you hire twenty, it's not really going to help much. Sample formula:
Extra customers = 10*log(NumberOfReceptionists*100000+1)
There's a +1 at the end, because log(0) doesn't work.
With this formula, you get 0 extra customers for 0 receptionists, 115 customers for 1 receptionist, 122 customers for 2, and 145 customers for 20 of them.
Ok, that's a poor example, but you get the point.
Negative exponentials
When you make the x negative, you get what's basically an upside down exponential.
http://i262.photobucket.com/albums/ii89/smuz86/articles/e-x.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/e-xover2.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/e-xover12.png
From left, it's exp(-x), exp(-x/2), and exp(-x/12).
Example of negative exponentials
Armor piercing is a good example for this. If you have high armor piercing, but no armor to pierce, it doesn't do as much effect.
Final = Armor*exp(-APStrength/Resistance)
Let's say Resistance is that armor's resistance to AP. Give it some number like Resistance = 5.
Armor then becomes..
Lvl 1 AP: 81.8% Lvl 2 AP: 67.0%
Lvl 3 AP: 54.9% Lvl 4 AP: 44.9%
It has a strong impact early on, but at higher levels it becomes less effective.
Sigmoid curve (S-Curve)
The S-Curve gets you a nice, S shaped function. This is useful for a lot of things, like things that give you an increasing bonus if you collect a few, but is capped.
The formula for a S-curve is y = 1/(1+exp(-x)). This is what it looks like:
http://i262.photobucket.com/albums/ii89/smuz86/articles/scurve.png
Note that the bottom half of it is less than 0.
You can apply the exp(-x/C) there, to get a steeper or less steep curve.
Gompertz function
The reason I didn't go into detail for the S-curve is because the Gompertz function is much more useful for writing formulas.
y = a*e^(b*e^(c*x))
a is the upper limit (maximum) of the function.
c is the growth rate you want it to be.
b and c both have to be negative.
I'll rip the link from Wikipedia, since it's such a nicer one:
http://upload.wikimedia.org/wikipedia/en/c/c5/Gompertz_com.PNG
Gompertz function example
This is one of my favorite non-linear functions, because you use it for things that doesn't work linearly. The formula is already there, just place the numbers. It's useful for things that give bonuses as well.
Say, hiring some guards will increase the security level of your place. Too few guards don't help because it makes them prone to being backstabbed by enemy ninjas. Too many guards won't help either, because they'd get in each other's way during a firefight.
Let's set the maximum security to 100. So, a = 100. The others can be experimented with.
We get something like this...
Security = 100*exp(-2*exp(-0.5*Guards))
1 Guard: 29, 2 Guards: 48, and so on, until it becomes 99 with 11 guards and 99.9 at 15 guards.
You can tweak b to require a slower start and tweak c to get a different rate of increase.
Sine and cosine
These two are pretty much the same thing. It's just that one lags behind the other. Up to you to figure which.
http://i262.photobucket.com/albums/ii89/smuz86/articles/sinx.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/cosx.png
Left is sin(x), right is cos(x).
Pick whichever you like.
You can change the lag by making it sin(x+L), where L = the amount of lag you want. Setting L = pi/2 will turn your sine function into a cosine and vice versa.
The length of one function does before repeating itself is 2*pi (right, the length of a circle).
You can change the rate it goes back to start by multiplying or dividing x. For example, if you want to set it to go full circle at 10, you set it to sin(10/(2*pi)) or sin(5/pi). The pi is because that's how a sin works.
A nice thing is that it's easy to change. The maximum and minimum of sin and cos are 1 and -1. If you want to change it to say, between 0 and 2, you just shift it up by adding 1 to it. If you want to make it bigger, like between 2 and -2, multiply it by 2.
Uses for sine and cosine
What they do is that they repeat the same thing over and over. So, if you want something that goes around in circles or oscillates between two points, that's what the sin and cos are for. Can't really think of one at the moment.
Powers
You can try power formulas, like x^2, x^3, x^4. Let's call it x^N
Generally, there's two shapes for these.
If N is an even number, you get a U shape...
http://i262.photobucket.com/albums/ii89/smuz86/articles/x2.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/x6.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/x12.png
From left, it's x^2, x^6, and x^12. As the powers get higher, the curve becomes sharper. At x^infinity, it becomes a straight line up. You shouldn't ever need to use x^infinity, though.
If N is an odd number, you get a.. squiggly shape.
http://i262.photobucket.com/albums/ii89/smuz86/articles/x3.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/x7.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/x13.png
From left, that's x^3, x^7, and x^13. Higher powers, sharper curve.
And here's one interesting trick:
http://i262.photobucket.com/albums/ii89/smuz86/articles/xabs5.png
That's Abs(x^5). See, it's like an even power. Unfortunately, I can't think of an easy way to get the odd power shape easily with a formula, but the difference between two powers is so small that you shouldn't have to.
Sinh and cosh
Sinh(x) does pretty much the same thing as the odd powers of N.
Cosh(x) also acts like an even power of N.
You can choose those instead if you like.
Dealing with randomness
Randomness is a complicated matter, so I wrote about it in a separate article:
http://www.create-games.com/article.asp?id=1958
If you have a standard Random(10) or so, you'll get an equal chance of getting any random number from 0 to 9.
The more you of them you have, e.g. (Random(10) + Random(10)), the more likely it is to become a bell curve, and the more randoms make it more narrow. Note that this isn't the same as multiplying it!
Here's a link to what a bell curve should look like, courtesy of Wikipedia:
http://en.wikipedia.org/wiki/File:Normal_Distribution_PDF.svg
This means most of your stuff should end up in the middle, with far less at the edges.
You can use proper statistics, such as variance and mean to find out how many percent should be in certain areas, but that won't be useful nor would it be accurate. Your bell curve will be skewed in a direction.. probably the direction that your players think is more powerful/more fun. If there's more of an advantage to making a high Strength character, you can expect the Strength graph to be skewed as higher than average and not the typical bell curve.
What should numbers be?
Remember how we used averages, maximum and minimum to determine a number? There's a few more tricks you can use here.
Say we have Strength, Agility, Constitution, Charisma, and Intelligence. The players get to split up some points. But you want to make it so that all of them are equally balanced. When it's balanced, the player can play with their own style and not worry about having to pick the most powerful combo.
Comparing multiple variables
You can try to note down all the points in 3D, but it's easier to place them on each other and see their effect in 2D. Let's go with something simple like attack and defense.
Attack = Str*3 + Agi + Int/3
Defense = Str + Agi*3 + Int/3
To get the graphs, we set turn off all unrelated variables. Then compare one variable to another.
What we do is draw separate graphs for each, Att vs Str, Att vs Agi, Att vs Int, Def vs Str, Def vs Agi, Def vs Int.
I'll just post the links here, because if I shrink it, you can't see the numbers:
http://i262.photobucket.com/albums/ii89/smuz86/articles/attstr.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/attagi.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/attint.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/defstr.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/defagi.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/defint.png
Balance
Here you assign a balanced value.
Let's say that to be balanced, attack has to be equal to defense.
Attack = Defense
Attack - Defense = 0 <--- Ideal balance is if advantage is 0!
Graphs:
http://i262.photobucket.com/albums/ii89/smuz86/articles/stragibal.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/agistrbal.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/strbal.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/agibal.png
But wait.. there's some imbalance there. A guy with high strength can beat another guy with equally high strength.
A guy with high agility can't beat another guy with high agility.
But they don't have any advantage in fighting each other.
Also, some graphs with intelligence:
http://i262.photobucket.com/albums/ii89/smuz86/articles/intbal.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/intstrbal.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/intagibal.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/agiintbal.png
http://i262.photobucket.com/albums/ii89/smuz86/articles/strintbal.png
Intelligence has equal advantage over each other. But while a high Int character has only a tiny penalty to attacking someone with high Str, a high Str character can take them down with no problem. And while high Agi characters only have a slight advantage over high Int characters, a high Int character has a very strong disadvantage when attacking a high Agi character.
So, if this is a combat game, where Int isn't used elsewhere, you should scrap it completely as there's no advantage to taking it.
Symmetry
At first glance it seems that Str is more powerful, because they can beat each other, while Agi can't.
But look at the vertical symmetry for both. Since they're the same, the result is that Str can beat each other and Agi can't beat anyone.
And that sounds just fine for a playing style where the high Agi characters can play defensive, while high Str can be very offensive.
Conclusion
In conclusion, you should avoid as much of this stuff as possible. It's not good for self motivation to do things that are too complex.
But this was written for those times when you really need a tool to help. A lot of professional game designers avoid all this stuff completely, so that's a good enough excuse for you to
|
|
|