Moving camera ruining input values! (SOLVED)

Hi all,

I have a factory spawning game objects upon clicking at the location of action.x, action.y.

However, if I move the camera, when I click the objects are appearing in a different location from where I clicked!

They seem to spawn in the worldspace relative to where my mouse is on my monitor. But because I have moved the camera the monitor.x monitor.y locations do not match the worldspace.x worldspace.y, locations if you will.

Can anyone suggest a solution?

N.B. action.screen_x and action.screen_y do not work either.

action.x and action.y will give you the coords for your screen, not the world coords.
Just take the action coords and add the camera coords and it will be correct?

Thats how I do it:

--- render script
...

local current_width = render.get_window_width()
local current_height = render.get_window_height()
msg.post("main_sc:/controller", "screen_size", { width = current_width , height = current_height, scale = zoom_factor })
...


--- controller script

local w = tonumber(sys.get_config("display.width"))
local h = tonumber(sys.get_config("display.height"))

function on_message(self, message_id, message, sender)
    if (message_id == hash("screen_size")) then
    	self.width = message.width
    	self.height = message.height
    	self.scale = message.scale
    end
end

function on_input(self, action_id, action)
    local mousex = self.width * action.x / w -- if window is resized
    local mousey = self.height * action.y / h

    local pos = go.get_position("/camera") -- camera position
    go.set_position(vmath.vector3(pos.x + (mousex -  self.width * 0.5) / self.scale, pos.y + (mousey - self.height * 0.5) /  self.scale, 10), "/target")
end

self.scale (zoom_factor) is optional, if u have pixelart graph

1 Like

In the end it wasn’t the camera position that had to be added, it was the vector difference between the current camera position and the starting position of the camera :slight_smile: