I need to come up with a formula to convert variable zoomed-in game space co-ords to UI co-ords. The engine I'm using is not traditional so there's no inbuilt function (convertToUISpace etc).

Now I have a feeling this is extremely easy, but I cannot get my brain to work the math...

It's a top down 2D game with 2 layers, game and UI. The screen is 640x360 res, but there is a zoom overlay on the game layer that can take it anywhere up to 400x225. The HUD layer remains the 640x360 res throughout though.

So I need to translate x,y positions on the game layer (at whatever zoom) to correctly position on the HUD. At the moment if I use exact co-ords they don't quite match up, unless the object is exactly in the screen centre.

Could anyone assist or clue me down the right path? I'm guessing I have to work out from the screen centre using the zoom ratio.

  • \$\begingroup\$ How does your zoom work? How do you translate coordinates between world-space and view-space? \$\endgroup\$ – dot_Sp0T Oct 26 '20 at 18:56
  • \$\begingroup\$ Have you tried simply mapping game-space coordinates to the UI-space? I.e., just like you can map a value from range 0-1 to 0-255, for example. So, if a coordinate has a value of 200 for the X in game-space, interpolating it to (200/400) * 640 in UI-space if the 400 is the width of the game screen (and ditto for the Y axis)? \$\endgroup\$ – verified_tinker Oct 26 '20 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.