Home > Software design >  Math problem trying to make a progressbar in project zomboid
Math problem trying to make a progressbar in project zomboid

Time:01-23

I am making a mod for project Zomboid and I cant seem to figure out a math problem, so the value I am getting ranges from 0 to 1 and I want my progress bar to start at the max width and then descend as the value is increasing.

The first one was easy I got a value between a 100 and 0 so how do this with a value starting at 0?

I tried searching this on google but I am really bad at math and could not find an answer.

function panel:render()
    self:drawRectBorder(30, 30, self:getWidth() - 1, 50, 1.0, 1.0, 1.0, 1.0);
    --print((bt_core.player:getBodyDamage():getOverallBodyHealth() / 100) * self:getWidth());
    self:drawRect(31, 31, (bt_core.player:getBodyDamage():getOverallBodyHealth() / 100) * self:getWidth() - 3, 48, 1, 0, 0, 1);

    self:drawRectBorder(30, 110, self:getWidth() - 1, 50, 1.0, 1.0, 1.0, 1.0);
    print(bt_core.player:getStats():getFatigue());

    if bt_core.player:getStats():getFatigue() == 0 then
        self:drawRect(31, 111, self:getWidth() - 3 , 48, 1, 0, 0, 1);
    else
        self:drawRect(32, 111,bt_core.player:getStats():getFatigue() / (self:getWidth() - 3) , 48, 1, 0, 0, 1);
    end
end

CodePudding user response:

To get variable in range 100..0 from variable in range 0..1, you can use y = 100 - x*

CodePudding user response:

So you have a value 0..1 and you want to map it to 100..0.

Multiplying your value with 100 gives you 0..100. To invert this you subtract that from 100. 100-0 is 100, 100-100 is 0...

local newVal = 100 - val * 100

or

local newVal = 100 * (1-val)
  •  Tags:  
  • Related