0

I found this response but the thread is closed:

https://gamedev.stackexchange.com/a/13639

level = constant * sqrt(XP)

I seem unable to properly get current level, this is how I tried to do it, and everything seems to work except the actual current level for some reason:

xpConstant = 0.05;
currentXp = 80;
-- with those two values, level 1 should require 20 xp:
-- xp = 1^2 / xpConstant;
-- with 80 xp, the player should be level 2, 2^2 / 0.05 = 80

-- this is 0.47 but it should be 2?
currentLevel = xpConstant * math.sqrt( currentXp ); 

-- and to retrieve xp until next level, this does seem to work:
nextLevel = currentLevel + 1;
XPUntilNextLevel = nextLevel ^2 / xpConstant;
XPUntilNextLevel = XPUntilNextLevel - currentXp;

so for some reason the currentLevel variable doesn't give 2 as expected,

2 Answers2

1

You need to multiply by the xpConstant, then get the square root. You can see this by rearranging your equation:

$$xp = level^2 / xpConstant$$ $$xp \cdot xpConstant = level^2$$ $$\sqrt{xp \cdot xpConstant} = level$$

Bálint
  • 14,887
  • 2
  • 34
  • 55
1

As Bálint says, your two formulae do not agree with one another:

$$level = constant \cdot \sqrt {xp} \\ xp = \frac {level^2} {constant}$$

If you choose to keep the xp formula as-is, then you need to change the level formula as shown in Bálint's answer.

Or, if you want to keep the level formula as it was in the original Q&A you're referencing, then you need to correctly solve for xp, by inverting each operation in sequence from the outside in:

$$\begin{align} level &= constant \cdot \sqrt {xp}\\ \frac {level} {constant} &= \sqrt {xp}\\ \frac {level^2} {constant^2} &= xp\\ \end{align}$$

So with constant = 0.05 your XP progression per level looks like:

 1:    400
 2:  1 600
 3:  3 600
 4:  6 400
 5: 10 000
DMGregory
  • 134,153
  • 22
  • 242
  • 357