1
$\begingroup$

I am running into an issue when it comes to calculating a player's level based on the total experience points they have. I came up with this formula for calculating XP needed for each level

 EXP_PER_LEVEL =  75*(CURRENT_LEVEL-1) + 200

So it takes 75*0+200 to reach level 2 (200 xp)

and 875 XP to reach level 11 (75*(10-1)+200)

This is the formula for calculating TOTAL XP from player's level

 TOTAL_XP =  37.5*(pow(CURRENT_LEVEL, 2)) + 87.5*CURRENT_LEVEL - 125;

Now I have problem with the reversal formula. In simple terms, based on these formulas, how would I calculate the player's level by just using the experience points they have ? Could you provide a math example of how it would work?

$\endgroup$
4
  • 1
    $\begingroup$ Thanks for downvoting this question without explaining what I did wrong :/ $\endgroup$ – Xefa974290823499093 Jan 3 '17 at 14:00
  • $\begingroup$ I come here a lot and I also don't understand the downvote. On an unrelated note, it looks like it should be 875 points for level 11. $\endgroup$ – user307169 Jan 3 '17 at 14:05
  • $\begingroup$ @tilper Yes, thank you :) I fixed my question $\endgroup$ – Xefa974290823499093 Jan 3 '17 at 14:08
  • $\begingroup$ @tilper This formula calculates experience needed to reach next level based on the current level :), So to calculate XP needed for level 3 it should be 75(2−1)+200= 275 XP needed to level up. Level 4: 350 level 5: 500 level 10: 875 $\endgroup$ – Xefa974290823499093 Jan 3 '17 at 14:27
2
$\begingroup$

It sounds like you want to solve the following equation for $x$, where $x$ represents the level and $y$ represents the total experience points: $$ y = \frac{75}2 x^2 + \frac{175}2 x - 125 $$

First subtract $y$ from both sides: $$ 0 = \frac{75}2 x^2 + \frac{175}2 x - 125 - y $$

Then multiply both sides by $2$ to simplify things. Not a necessary step, but it'll help simplify a little. $$ 0 = 75 x^2 + 175 x - 250 - 2y $$

Now use the quadratic formula, $x = \dfrac{-b \pm \sqrt{b^2 - 4ac}}{2a}$, where $a$, $b$, and $c$ are as follows: $$ 0 = \underbrace{75}_a x^2 + \underbrace{175}_b x \underbrace{- 250 - 2y}_c $$

To be clear: \begin{align*} a &= 75\\ b &= 175\\ c &= -250 - 2y \end{align*}

Finally, take only the positive root, i.e., $$x = \frac{-b + \sqrt{b^2 - 4ac}}{2a}$$ (explained below) and then you'll want to round your answer down to the nearest integer (also explained below).


Untested PHP code snippet example:

$y = 1100;  // 1100 total experience points
$a = 75;
$b = 175;
$c = -250 - 2 * $y;

$x = (-$b + sqrt($b * $b - 4 * $a * $c)) / (2 * $a);  // $x is about 4.667
$x = floor($x);  // Always round down.  Player is level 4 in this example.

Explanation:

The quadratic formula actually gives us two roots because of the $\pm$ sign in the numerator: $$ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$

But for this application we only want the $+$ sign. This is because $x$ must be positive since it represents the player's current level, but if we take the root with the $-$ sign then we'll (always) get a negative number.

This example also highlights why we always want to round down. If we rounded $4.667$ up as per the standard rounding rules, then we would get $x=5$, which means the player is level 5, but according to the total XP formula, the player can't be level 5 until the player has 1250 XP: $$ 37.5 \cdot 5^2 + 87.5 \cdot 5 - 125 = 1250 $$

In other words, a player hasn't actually reached level 5 until the player is actually at level 5. Therefore being "close" to level 5 (e.g., being at "level 4.667") doesn't count as being at level 5.

$\endgroup$
5
  • $\begingroup$ Sir, while your response is very clear, I have issues with porting this formula into PHP (I was never good with quadrantic formulas). Could you show me an example in Code? You could use Java, I think my slow brain shouldn't have problems with it. ;) $\endgroup$ – Xefa974290823499093 Jan 3 '17 at 15:09
  • $\begingroup$ @Xefa974290823499093, yes, I'll update answer in a few minutes. $\endgroup$ – user307169 Jan 3 '17 at 15:10
  • $\begingroup$ @Xefa974290823499093, see PHP example plus more edits. $\endgroup$ – user307169 Jan 3 '17 at 15:28
  • 1
    $\begingroup$ Code example aside, I wanted to thank you for the detailed explanation secion! I wish all the best to you in this year! :) $\endgroup$ – Xefa974290823499093 Jan 3 '17 at 15:36
  • $\begingroup$ @Xefa974290823499093, no problem. Thanks, you too! $\endgroup$ – user307169 Jan 3 '17 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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