# 2d Scale width and height based on distance

I'm drawing a health bar on top of each player. I transform 3d position to 2d. I would like to draw it smaller when distance increases. Any simple math for that? Let's say bar width is 30 and height is 10 it looks good only on distance == 100. What formula should I use to make it decrease in size when distance increases?

Should resolution of the game be taken into consideration?

• This sounds a lot like actual 3D rendering, so any papers/tutorials on it could be useful for you. – ZEKE Oct 30 '17 at 19:55
• @ZEKE it's about math here, width, height and scale based on distance. Rendering is done in 2D. – Konrad Oct 30 '17 at 20:01

w' = w / Z

• One detail, if you find it looks right at its base scale at referenceDistance = 100 then throw that in as a multiplicative factor: w' = w * referenceDistance / distance - that way the multiplier comes out to 1 at the reference distance you've chosen, and scales appropriately closer & farther away. – DMGregory Oct 31 '17 at 3:57