mathematics – How can I calculate current level from total XP, when each level requires proportionally more XP?


When working out the maths and solving for the Level conditional on experience XP, we obtain:

$$Level = frac{1 + sqrt{1 + 8 times XP div 50}}{2}$$

For example, what is the player’s level for $XP = 300$?

$$ frac{1 + sqrt{1 + 8 times 300 div 50}}{2} = 4 $$

As requested.

Or, what is the level for XP = 100000?

$$ frac{1 + sqrt{1 + 8 times 100000 div 50}}{2} = 63 $$

More generally expressed, for an arbitrary starting threshold at Level 1:

$$ Level = frac{1 + sqrt{1 + 8 times threshold div 50}}{2} $$

You can also do the reverse and calculate the XP needed for any given level by solving the above formula for XP.

$$ XP = frac{(Level^2-Level) times threshold}{2} $$

Note that the above formula works with fractions but you need to round down to the next integer value. For example in C++/C# you could use (int)Level.

To obtained the above closed form formula, I used difference equations, Gauss summation and a quadratic formula.

If you are interested in the solution of this formula step by step…

We do an recursive algorithm by starting our considerations that ultimately Experience(next_level) = Experience(current_level) + current_level*50.

For example, to obtain $XP_{Level3}$ we have:

$$ XP_{Level3} = XP_{Level2} + 2 times 50 $$

Where, 2*50 comes from the OP’s request that experience needed to reach the next level is current level*50.

Now, we substitute $Xp_{Level2}$ with the same logic into the formula. That is:

Substitute $XP_{Level2} = XP_{Level1} + 2 times 50$ into the above formula:

$$ Xp_{Level3} = Xp_{Level1} + 1 times 50 + 2 times 50 $$

and $ Xp_{Level1} $ is just 50, which is our starting point. Hence

$$ Xp_{Level3} = 50 + 2 times 50 = 150 $$

We can recognize a pattern for recursively calculating higher levels and a finite chain of summations.

$$ Xp_{LevelN} = 50 + 2 times 50 + 3 times 50 + … + (N-1) times 50 = sum_{i=0}^{n-1} i times 50$$

Where N is the level to be achieved. To get the XP for the level N, we need to solve for N.

$$ Xp_{LevelN} div 50 = sum_{i=0}^{n-1} i $$

Now the right hand side is simply a summation from 1 to N-1, which can be expressed by the famous Gaussian summation $ N times (N+1)div2-N $. Hence

$$ Xp_{LevelN} div 50 = N(N+1) div 2-N $$

or just

$$ 2*(Xp_{LevelN} – 50) div 50 = N(N+1)-2N $$

Finally, putting everything on one side:

$$ 0 = N^2-N-2 times Xp_{LevelN} div 50 $$

This is now a quadratic formula yielding a negative and positive solution, of which only the positive is relevant as there are no negative levels. We now obtain:

$$ N = frac{1 + sqrt{frac{1+4 times 2 times Xp_{LevelN}}{50}}}{2} $$

The current level conditional on XP and linear threshold is therefore:

$$ Level = frac{1 + sqrt{1+8times XP div threshold}}{2} $$

Note Knowing these steps can be useful to solve for even more complex progressions. In the RPG realm you will see besides a linear progression as here, the actually more common fractional power or square relationship, e.g. $Level = frac{sqrt{XP}}{5.0}$. However, for game implementation itself, I believe this solution to be less optimal as ideally you should know all your level progressions beforehand instead of calculating them at runtime. For my own engine, I use therefore pre-processed experience tables, which are more flexible and often faster. However, to write those tables first, or, to just merely ask yourself what XP is needed to, let’s say, obtain Level 100, this formula provides the quickest way aimed at answering the OP’s specific question.

Edit: This formula is fully working as it should and it outputs correctly the current level conditional on XP with a linear threshold progression as requested by the OP. (The previous formula outputted “level+1” by assuming the player started from Level 0, which was my erring–I had solved it on my lunch break by writing on a small tissue! 🙂