[SOLVED] Camera world to screen problem

is camera world_to_screen broken? trying to convert game objects coordinates to guicoodinates but results are wrong.

What i’m trying to achieve: i have tiles, trying to spawn rectangles on top them on gui side.

Display width = 720

Display_height = 1280

Camera projection = ortograhic

Camera zoom mode = auto_fit (to cover whole screen)

main.script

go.property("width", 7)
go.property("height", 7)
go.property("gap_size", 4)
go.property("tile_size", 90)

function init(self)
	local total_w = self.width * self.tile_size + (self.width - 1) * self.gap_size
	local total_h = self.height * self.tile_size + (self.height - 1) * self.gap_size
	local start_x = -total_w * 0.5
	local start_y = -total_h * 0.5
	
	self.tiles = {}
	for i = 1, self.width * self.height do
		local x = (i - 1) % self.width
		local y = math.floor((i - 1) / self.width)
		local pos = vmath.vector3(
			start_x + x * (self.tile_size + self.gap_size),
			start_y + y * (self.tile_size + self.gap_size),
			0
		)
		self.tiles[i] = factory.create("#factory", pos)
	end

	msg.post(".", "acquire_input_focus")
end

function on_input(self, action_id, action)
	if action_id == hash("touch") and action.pressed then
		local positions = {}
		for i = 1, #self.tiles do
			positions[i] = go.get_position(self.tiles[i])
		end
		msg.post(msg.url("main", "/main", "play"), hash("tiles_loaded"), { payload = positions })
	end
end

gui script

function init(self)
	self.prefabs = {}
end

function on_message(self, message_id, message)
	if message_id == hash("tiles_loaded") then
		-- clear
		if #self.prefabs ~= 0 then
			for i = 1, #self.prefabs do
				gui.delete_node(self.prefabs[i])
				self.prefabs[i] = nil
			end
			return
		end

		-- create
		for i = 1, #message.payload do
			local prefab = gui.get_node("prefab")
			local clone = gui.clone(prefab)
			gui.set_enabled(clone, true)
			gui.set_position(clone, camera.world_to_screen(message.payload[i]))

			self.prefabs[i] = clone
		end
	end
end

MRP:

World Screen.zip (77.1 KB)

press mouse to toggle rectangles.

You have to use gui.set_screen_position .

1 Like