Math - Exponential Functions
/* This is my first tutorial, so I'll start with something simple */
What are Exponential Functions?
Most people can tell you that a gradient of a line is the difference vertically between two points on the line, divided by the difference horizontally between those two points, and can also be thought of as the rate of change of the line. However, this value is not always a constant number. In the case of curves, the rate of change of the curve is also changing, the gradient of the line also having a gradient of it's own. The Mathematician Euler found a number (approximately 2.718), which when raised to the power x has a self gradient, meaning the gradient of the curve is the same equation as the curve itself: e^x. An exponential function is any function that uses this magic constant to define it's structure.
What use are Exponential Functions?
Euler's number is found naturally in many things such as the half-life of radioactive atoms, and the current discharge from a capacitor. It can also be used to calculate a strong level-up calculator for a Role-Playing Game.
The AS Function
Math.exp(x)
This function raises Euler's Constant to a power x and returns the result. Understanding how the number works can make a smooth level-up curve without you having to do much work at all.
Implementing The Function
Let's try the RPG example.
Choose a step length, this will be your x value. For this purpose, I am choosing 1/2 as my step length. This will determine how far horizontally along the curve the next level will be to reach (as an addition to what has already been attained). In a real example, experience points will probably be in the hundreds or thousands, so the required experience to get to a certain level from a previous one we will assume to be 1000*Math.exp((x-1)/2). Why x-1? So that the first level will be e^0, which will yield 1000. A nice round number to scale every other level.
We can create an array to hold the values of experience needed to level up, using iterative code. Remember these values are the extra experience needed to reach the next level, not the total. Let's generate 20:
A_ExpNeeded = new Array();
for (i=1;i<20;i++) {
A_ExpNeeded[i] = Math.round(1000*Math.exp((i-1)/2));
}
Believe it or not, this is the most efficient way to create a level curve which is in a perfect balance of fairness to the user. Using different step lengths and different multipliers you can scale it to however you wish. If you want more, easier levels then divide x by an even larger number. Through experimentation you can easily find a system people will love, and everybody can enjoy the magic of Euler's number.
/* I may follow this up with another tutorial on more complex examples of exponential curves, depending on how this is appreciated/*
~Sekky~