Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am currently making an XNA game in C#. I want to increase health as the player level up. I am currently using this equation.

MaxHealth = Convert.ToInt32(100 * Math.Pow(1.17, level - 1));

Its fine before the first 20 levels, but afterwards the health starts to increase extremely fast:

Level 1: 100 Hp.

Level 20: 1.975 Hp.

Level 30: 9.493 Hp.

Level 40: 45.630 Hp.

Level 100: 658.546.089 Hp.

Level 109: Crash: value too big for an integer.

How should i create these growth algorithms, so that the growth of hit points pr. level, will increase slowly. This is just an example, if you know a better growth rate, I will be happy to know that too.

level 2: Hp = base health + 60 (Base health = 100)

level 3: Hp = base helath + 60 + 120

level 4: Hp = base helath + 60 + 120 + 180.

But the max health formular will run on every update/tick, so all this should happen in the same calculation.

Edit:

Thank you all for your input, I really appreciate it. But while away from the question, I found a solution myself. Thank you very much. The solution:

if (level < 35)
    this.baseMaxHealth = Convert.ToInt32((vitality * 10) + Math.Pow(level, 2) * 4);
else
    this.baseMaxHealth = Convert.ToInt32((vitality * (level - 25)) + Math.Pow(level, 2) * 4);

I added vitality as an upgrade you can spend skill points or something on

EDIT 2:

I improved the code a bit

        if (level < 35)
            this.baseMaxHealth = Convert.ToInt32((vitality * 10) + Math.Pow(level, 2) * 4);
        else if (level > 125)
            this.baseMaxHealth = Convert.ToInt32((vitality * 100) + Math.Pow(level, 2) * 4);
        else
            this.baseMaxHealth = Convert.ToInt32((vitality * (level - 25)) + Math.Pow(level, 2) * 4);
share|improve this question
    
HP in my experience has always been something that increases very slowly, XP required for next level tends to be the big exponential. I would go with your second example or you could try reducing the 1.17 in your pow call to something like 1.02 for a more gradual increase –  Wardy Aug 28 at 13:34
    
You could also consider using a float instead of an int or using a level cap :) –  Wardy Aug 28 at 13:38
1  
Your question isn't quite a duplicate of this one, but this answer to it applies to yours just as well. –  XGundam05 Aug 28 at 13:39
    
+1 to XGundam05, just pick a function you find suit your needs. Technically, this is not question about games - it is about functions and their plots. Choose the plot you like/need. Suggested logarithmic, linear or sqrt(0-1) functions might suit you. –  wondra Aug 28 at 13:52

4 Answers 4

Long ago I did the math for different growth functions for an RPG (that I didn´t use in the end). I was playing around with five basic growth curves, as show below.

Different growth curves

The curves are:

  • Red: Exponential. Grows slowly at the beginning, very fast at the end.
  • Blue: Quadratic. Average growth curve.
  • Black: Linear.
  • Green: Flipped quadratic. Grows more slowly with time.
  • Yellow: Flipped exponential. Grows quite fast at the beginning, quite slow at the end.

The formulas that govern each one of these curves are the following (I'm omitting linear in the rest of the discussion for simplicity).

  • Red: y=z^x
  • Blue: y=(x/z)^2
  • Green: y=-((x-MAXIMUM LEVEL)/z)^2+MAXIMUM VALUE
  • Yellow: y=-z^(-(x-MAXIMUM LEVEL))+MAXIMUM VALUE

Where MAXIMUM LEVEL is the maximum level you expect to stop at, MAXIMUM VALUE is the maximum value you expect for that level, and z is a coefficient you need to calculate. Also note that all curves start at 0 for x = 0 (not exactly, more on this later).

So, how do you calculate this z? For that, you need to use the Blue and Red functions.

For the Blue (quadratic), you know that at x=MAXIMUM LEVEL, y=MAXIMUM VALUE. Therefore you can take (MAXIMUM LEVEL/z)^2=MAXIMUM VALUE and solve for z.

For instance, you want the max level to be 200 and the maximum HP to be 7000. Therefore you would calculate

(200/z)^2 = 7000
sqrt 7000 = 83.6660026534
200/x = 83.6660026534
200/83.6660026534 = z
2.39045721867 = z

And therefore the function is y=(x/2.390)^2. Notice that the same value can be used for the Green curve, and therefore that function would be -((x-200)/2.390)^2+7000.

For the Red curve (exponential) you know that z^MAXIMUM LEVEL=MAXIMUM VALUE. Therefore you only need to calculate z as the MAXIMUM LEVEL'th root of MAXIMUM VALUE.

For instance, following the previous example, z would be the 200'th root of 7000, or 1.0452627896377216... This value can be applied to the Red and Yellow curve to obtain y=1.0452627896377216^x and -1.0452627896377216^(-(x-200))+7000.

I hope that helps you.

Some observations

There are a couple of details I would like to point out but are not essential to the discussion.

Curve shape

It is possible to change the value of the exponent of the Blue curve to make it more or less pronunciate. As the example below shows, the lower the exponent, the closer to a line, and the higher, the close to a step.

Different shapes for a quadratic curve

Similar results can be obtained by playing around with exponential exponents, although the result is much less satisfactory.

Different shapes for an exponential curve

Exponential gap

Although it is hard to appreciate in the image, the exponential curves have a small gap at the first (Red) or last (Yellow) points. This occurs because z^x is 1 for x=0, not 0 as we would expect. In order to create a really proper curve that passes through the first and last point, you need to define two curves, and then blend them together in a smooth transition. For our example of value 100 at point 100, the curves would have this aspect.

  • Red: y=((100-x)/100)*(1.04712^x-1)+((x)/100)*1.04712^x
  • Yellow: y=((100-x)/100)(-1.04712^(-(x-100))+100)+(x/100)(-1.04712^(-(x-100))+101)

Adjusted and blended exponential curves

Caveat: I am no mathematician, is late at night and I am sleepy. This analysis may contain errors. Use at your own discretion.

share|improve this answer

You could use a logarithmic function:

enter image description here

Example function:

double increment = Math.Log(level + 1);

Example output:

Level 1 increment: 0.693147180559945

Level 2 increment: 1.09861228866811

Level 3 increment: 1.38629436111989

Level 4 increment: 1.6094379124341

Level 5 increment: 1.79175946922805

Level 6 increment: 1.94591014905531

Level 7 increment: 2.07944154167984

Level 8 increment: 2.19722457733622

Level 9 increment: 2.30258509299405

Level 10 increment: 2.39789527279837

...

Level 99 increment: 4.60517018598809

share|improve this answer

There are lots of simple functions to realize those curves.

(x^2)*40 - (x/8) + 200

This one grows in a smooth curve since I used x^1.5:

(x^1.5)*5 - (x/9) + 200

Play around with the values.

share|improve this answer

Since these are possible solutions I figured why not ...

HP in my experience has always been something that increases very slowly, XP required for next level tends to be the big exponential. I would go with your second example or you could try reducing the 1.17 in your pow call to something like 1.02 for a more gradual increase.

You could also consider using a float instead of an int or using a level cap :)

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.