Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I'm currently making a game. I was given some numbers the leader wants me to stick close to. They are used for leveling; level 1, for example, needs 100 experience points, level 2 needs 250 experience points, etc.

Is there any way, maybe with a computer program, to calculate a function that will do something like this, instead of having to hard-code it?

Thanks in advance!

Simon.

share|improve this question
2  
Probably the best option is to approximate with an exponential function (since you'll want growth that is more or less exponential). –  Arturo Magidin May 18 '11 at 19:19
    
Maybe you should try polynomial interpolation of data, en.wikipedia.org/wiki/Polynomial_interpolation, which is a standard methot to do what you want. If I understood your question correctly :) –  Andy May 18 '11 at 19:20
    
That might indeed be the best way, Andy :) Please add it as an answer, so I can accept it when it's the best. –  Simon Verbeke May 18 '11 at 19:28
3  
This sounds like a really bad idea, games are really hard to balance properly, and just using an exponential function for every problem like this makes no sense. I actually have designed a lot of games, and it's so important to micromanage all the math in game design, and the best way to do things depends heavily on the game mechanics. This question is better asked in a forum devoted to game design. –  Matt Calhoun May 19 '11 at 6:07

6 Answers 6

up vote 9 down vote accepted

There are plenty of ways; it all depends on what kind of function you want.

One way is to use Lagrange interpolation to get a polynomial that will have the values you want (e.g., $f(1)=100$, $f(2)=250$, etc). This, however, may not give you the "correct" long-term behavior, and the long-term growth is not going to be exponential. You will "hit" all the right values for the points you are given, but you may not have good control later.

(For example, if you use Lagrange polynomial interpolation to find a function that satisfies $$f(0)=1,\quad f(1)=2,\quad f(2)=4,\quad f(3)=8,\quad\text{and}\quad f(4)=16,$$ then this function will give $f(5)=31$ (rather than the perhaps expected $32$).

A more likely long-term behavior will be given by exponential functions. If the rate of growth is relatively constant, then that's the way to go. Consider the sequence of quotients of successive values: $$\frac{f(2)}{f(1)},\quad\frac{f(3)}{f(2)},\quad\frac{f(4)}{f(3)},\quad\frac{f(5)}{f(4)},\ldots$$ and if these are all reasonably close to one another, then take $a$ to be their average. You can approximate the values pretty well with $$f(n) = f(1)a^{n-1}.$$ This is what Ross gives you, with $a=2.5$ and $f(1)=100$, except he's writing it as $$f(n) = 100(2.5)^{n-1} = \left(\frac{100}{2.5}\right)2.5^n = 40\times 2.5^n.$$

If the rate of growth is (the successive factors) are changing a lot, though, it's going to be a lot harder to come up with a good exponential approximation.

share|improve this answer

Depending on the number of levels that you have in your game, it may make more sense to hard code the values. Really depends if these experience points are never going to change, or if in a couple weeks your leader has a new set of values (and thus a new function you have to produce).

Ask your leader how he/she came up with the values and they have a heuristic that they are using. Likely it will be something like "$x$ times as many points as the previous level required".

Otherwise, you can go to wikipedia and read up on Regression analysis.

share|improve this answer

I think interpolation is not the way to go, as levels are discrete in any way and especially the lagrange interpolation might really not do what you want (most answers suggest to use it to interpolate the few first values, but then the function can even be negative for higher levels although it fits what you originally wanted, and if you interpolate over all values you could directly use a table, I see no advantage here).

The experience function is something that should be carefully designed as it is one of the main goals for the player to level up (and the game should not get too boring after a few levels, at the same time it shouldn't be too fast to level up). Also you have to consider that the experience you gain from defeating enemys should maybe also increase over the time.

Therefore there are a few things you should consider in your formula:

  • The level of the character (of course, this will be the main parameter)

  • The average amount of experience the character gets by defeating an enemy

  • A difficulty factor?

As an example you can learn from the experience function of this popular game. I can assure you that they thought a lot about balance and especially experience. For example in patch 2.3 they added a different factor to speed up the level process in certain levels because it was too difficult to level up. I also recommend this. I would recommend you to use a similar approach and maybe you should use a hardcoded table.

share|improve this answer

I know you said you wanted to avoid a hardcoded table, but honestly that's what you should use. The mathematical approaches given here are all well and good, but using them you will probably have to settle for an approximation of what you really want, and it may not be easy to make changes later.

Just write an array indexed by level with the XP needed to level up. This is what World of Warcraft does (WoW) as do most MMOs. It's a tiny bit of memory, super-fast, and completely flexible.

share|improve this answer
    
Hey, thanks for the tip! I did not know they used hardcoded tables too, because everything can be calculated with a function. –  Simon Verbeke May 19 '11 at 14:50

It depends upon how accurately you want to reproduce the numbers. For example, you could use $40*2.5^{level}$. This gives 100, 250,625,1562.5,3906.25 and so on. The first two match exactly. Exponential growth seems to be what many games want. Does this meet your needs?

share|improve this answer
    
I was actually just giving an example with the numbers. What I'm really looking for is a way to do it myself, so I don't have to ask around all the time. Thanks anyways! –  Simon Verbeke May 18 '11 at 19:26
1  
In my very limited experience, you want something exponential. The base, 2.5 here, is how much more experience you need for each successive level. Then the mulitplier, here 40, sets the overall scale, so should be chosen to make the first or last level what you want. If you want to use polynomial interpolation, you should interpolate the logarithm of the desired breakpoints, to make the polynomial fit well. –  Ross Millikan May 18 '11 at 19:35

Depending on every data your given, you might want to choose some polynomial interpolation.

You will notice that there are different approaches to find such a polynomial, but since it is unique, you'll always find the same.

I'm not an expert either in numerical methods nor in programming, so there might be other ways I'm not aware of.

Also, like Arturo says, this will be best if your scale of levels doesn't go too far to infinity (see his answer for more insight).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.