Issues setting GUI to world position

i’m having a lot of issues setting a GUI node to a world position.
to start, i’m not using a standard render function.

here’s the one i’m using. the purpose of which is to allow the program to scale when the vertical size is changed, but change the aspect ratio when the horizontal size is changed. it also changes it so that the render output of a camera is centered on its position

local function fixed_fit_projection_no_scale(near, far, zoom) 
    zoom=1/zoom
    local projected_width = render.get_window_width()
    local projected_height = render.get_window_height()
    
    local default_width=render.get_width()*zoom
    local default_height=render.get_height()*zoom

    local xs= zoom*projected_width/render.get_width()
    local ys= zoom*projected_height/render.get_height()

    local top=default_height/2
    local bottom=-default_height/2
    local left=-xs*default_width/(2*ys)
    local right=xs*default_width/(2*ys)
    
    return vmath.matrix4_orthographic( left, right, bottom,  top, near, far)
end

as for the trouble with the gui nodes, i’m using a function that translates world position to the ui position

function worldtoscreen(x,y)
		nx=1920/2 --the default screen size
		ny=1080/2

		nx=nx+((x*programdata.hs)-(19*32*programdata.hs))
		ny=ny+((y*programdata.hs)-(15*32*programdata.hs)) --here programdata.hs is the horizontal scaling factor

		return nx,ny
	end

the trouble is that it’s not quite right.
this is what it look like at normal-ish scaling (the green dots are where the UI is thinking the tiles are)
correct.PNG
but then it all falls apart once the screen is resized in a significant way
incorrect
i’ve tried a lot of messing around with what’s being multiplied and the node scaling modes, but nothing seems to make it work as intended.

in fact, it was working better a bit ago but i somehow made it worse by changing something (i don’t even know what)

I’m not sure what is going on with your setup, but I use britzl’s orthographic camera for this stuff. Might be worth a try. It takes minutes to drop it in and get it working, and there functions to do screen and world space conversions. His libraries are very robust and easy to use with just reading the quick start readme.md. Example app here.

1 Like

just use this

function func.convert_coord(action)
	local screen_size_x, screen_size_y = window.get_size()
	local config_size_x, config_size_y = tonumber(sys.get_config("display.width")), tonumber(sys.get_config("display.height"))

	local zoom = math.min(screen_size_x / config_size_x, screen_size_y / config_size_y)
	local projected_width = screen_size_x / zoom
	local projected_height = screen_size_y / zoom
	local xoffset = -(projected_width - config_size_x) / 2
	local yoffset = -(projected_height - config_size_y) / 2

	return vmath.vector3((action.x / config_size_x) * projected_width + xoffset, (action.y / config_size_y) * projected_height + yoffset, 0)
end