Aligning surface.DrawRect

Hello, I am making a HUD and I would like the armor bar to reduce from the left rather than the default right. I have posted a video that might help explain.
Here is the code I am currently using.


armor = LocalPlayer():Armor()
surface.SetDrawColor(120,120,255)
surface.DrawRect(ScrW() / 2 - (44 + (armor * 2.5)), ScrH() - 34, armor * 2.5, 30)

Video: https://youtu.be/N4M5RUsLarU

My main question: Is there a way to align a surface.DrawRect to the right without having it jitter like in the video? Yes I have OCD.
Any and all help is appreciated.


surface.DrawRect(ScrW() / 2 - (44 + (armor * 2.5)), ScrH() - 34, armor * 2.5, 30)

Into:


surface.DrawRect(ScrW() / 2 - (44 - (armor * 2.5)), ScrH() - 34, armor * 2.5, 30)

Remember to reposition it at the end of bar.

I don’t think you understand what I mean, I want the armor to increase from the center, as in from the avatar and increase outwards. What you told me to do just messed up what was already a very strange line of code. Thanks for your suggestion though.

I’m guessing you are new to lua and I can’t afford repeating myself.

About that jitter tho - it’s caused by the fact that your bar can’t cover half or quarter of a pixel, it can only be 10, 200, 2000 pixels wide, never 200.4, 50.6 (floats).
You can fix it tho by using Lerp() or math.Approach() to animate transition.

Wouldn’t a simple rounding fix it?

He needs to smoothen the transitions - in fact we would most likely come up with a solution if he posted code.

False, you can’t divide a pixel, althought interpolation works because it ramp

[lua]
lua_run_cl hook.Add(“HUDPaint”,“Decimals”, function()
surface.SetDrawColor(235,235,235)
surface.DrawRect(0,0,31,8)
surface.DrawRect(0,9,31.5,8)
surface.DrawRect(0,18,32,8)
surface.DrawRect(0,27,32.5,8)
end)
[/lua]

Stepped animation that looks smooth thought

[lua]
local i = 0
hook.Add(“HUDPaint”,“Decimals”, function()
surface.SetDrawColor(235,235,235)
surface.DrawRect(0,0,i-1,8)
surface.DrawRect(0,9,i-1+.5,8)
surface.DrawRect(0,18,i,8)
surface.DrawRect(0,27,i+.5,8)
i = i + 1
end)
[/lua]

The code I posted was of the armor bar, can you not use that code to help me out?
When you first commented you changed a - to a + and that just broke my code. I still don’t quite understand and yes I am new to lua xD

How is that false? You literally proved my point.
His bar jitters because it moves 2-3px per armor point.

His armor isn’t going to be a float when charged using that thing, thus his bar will be sequentally 0, 3, 5, 8, 10, 13 etc. wide.
He can smoothen it using Lerp() so it reaches target width with a delay.

How i did prove you were right, i’ve exactly told you that just because lerp uses float doesnt make the path looks smoothed, as i said, increasing stepped values can cause the bar looks smooth altought you have to do it with solid values, i would preffer to go with stepped values due decimal precision and as i ve took my time to show you that pixels cant be dividided, to go into ints value more than floats, because you should be aware about how interpolation works right?

I have not questioned the usage of lerp, i’ve argued your explanation about how this works
Also he’s not addint stepped values into armor var, he’s applying the full value into it

But its okay, its about what kind of value do you need and how do you want to apply…if you care about it and perfect ocd (because as i said, an interpolated value NEVER reachs to goal)
You will now that you will find a missing pixel or an annoying bar trying to be filled after five seconds while doing it stepped it’s just frames

Sorry if theres a wird wo, im taking a shit


local w = math.Round(armor * 2.5)
surface.DrawRect(ScrW() / 2 - (44 + w), ScrH() - 34, w, 30)

I didn’t explain how Lerp works at all, I simply told him to use it to animate the transition - it would definitely make it smoother.

Thank you for your help guys. I kept it simple and instead of doing armor x 2.5 I just rounded it to armor x 3.