# Dynamic division - math failure...

I am working on a PC game and am attempting to do some dynamic math to get a players health level for each of X health icons out of a total starting health.

Lets say:

StartingHealth = 150

PlayerHealth = StartingHealth (Dyanmic, decrements by a number based on enemy strength)

HealthIconCount = 6

4 health increments per / HealthIcon (100, 75, 50, 25)

I need to, based on player's current health, decrement each of the 6 health icons, sequentially based on if they have reached 0% themselves, between their 4 increment states in order.

So if a players health starts at 150 and is currently at 125, what state should the first of 6 health icons with 4 increments be set to and is there a simple/elegant equation to solve this? 100%, 75%, 50% or 25%? Because the other 5 icons are still 100...

Does this make sense?

Im lost... as usual

Last edited:

#### romsek

MHF Helper
let me see if I understand this.

total of 150 health

you have 6 icons indicating health level

each icon has 4 sub levels, and I assume can be drawn 4 different ways indicating which increment they are currently at

The way I would do it is as follows

$nicons = \dfrac{h}{25}$, and round this down to the nearest integer. This is how many full icons to display.

$lasticonlevel = \dfrac{h - 25 nicons}{6.25}$ and round this down or up as you see fit.

we display $nicons$ full icons, and display the last icon at level $lasticonlevel$

Let's say health is currently at 135.

we see that we would display 5 full icons, and would display the last icon at level 1 or 2 (the actual level is 1.6, it's up to you to decide how to display this as 1 or 2)

I see h/25 will give me the number of icons full. I'm lost a lasticonlevel. I don't get the same numbers you do? 135-25/6.25 = 17.6? I was actually looking to get the percentage of each health icon's allotted 100% health, based on the current health of player / 6 (number of active icons). As I said, i never was good with math

Last edited:

#### romsek

MHF Helper
I see h/25 will give me the number of icons full. I'm lost a lasticonlevel. I don't get the same numbers you do? 135-25/6.25 = 17.6? I was actually looking to get the percentage of each health icon's allotted 100% health, based on the current health of player / 6 (number of active icons). As I said, i never was good with math
you want all 6 icons to tick down health together? Why have 6?

I'm sorry I explained it wrong, you're first impression was correct. I want each individual icon to tick down from 100, 75, 50, 25, sequentially. What I don't understand is I need each icon to have an integer between 0 and 100 that represents its state at the current moment. This is so I can display a different image for every icon when it hits it's increment points. I also don't understand how you got 1.6 or what 6.25 is suppose to represent.

My idea was to do

n = 150/6
135(currenthealth) / 25 = 5.4

but I don't know how to convert 5.4, so 5.4 remaining, but .6 puts the first icon in which state, 100, 75, 50, 25? This is the missing piece.

I guess I need to take a decimal that will be between 0 and 1 and convert it to 100s

Last edited:

#### romsek

MHF Helper
I'm sorry I explained it wrong, you're first impression was correct. I want each individual icon to tick down from 100, 75, 50, 25, sequentially. What I don't understand is I need each icon to have an integer between 0 and 100 that represents its state at the current moment. This is so I can display a different image for every icon when it hits it's increment points. I also don't understand how you got 1.6 or what 6.25 is suppose to represent.
oh ok, that's no biggie.

first lets make sure we're on the same page

150 is 6 full icons

125 is 5 full icons

100 is 4

75 is 3

50 is 2

25 is 1

ok..now you're saying for each icon you want a number 0-100 for how full that icons gets drawn

let current health be $H$

the number of full icons to be drawn is

$n = \left \lfloor \dfrac{H}{25} \right \rfloor$, i.e. just take the integer part of $\dfrac{H}{25}$

the lvl of the last icon to be drawn will be

$lvl = 4(h - 25 n)$

I'm back to where I was, n = 135/25 = 5.4 .... Now lvl = is hard for me, 4 * (135 - 25 * 5.4) = ??? (25*5.4 = 135 - 135 = 0 * 4 = 0??). Sorry like I said for the inability to grasp math.

#### romsek

MHF Helper
like I said just keep the integer part of that, the 5

ignore the decimal part

there may be a function in whatever language you're writing this in called Floor that will do this for you