Cursor with a camera acting odd

Hi,

I have attempted to create a cursor object using the following example:

go.property("speed", 350) -- <1>

function init(self)
    msg.post(".", "acquire_input_focus") -- <2>
end

function on_input(self, action_id, action)
    if action_id == hash("touch") or not action_id then -- <3>
        local current_pos = go.get_position() -- <4>
        local target_pos = vmath.vector3(action.x, action.y, 0) -- <5>
        local distance = vmath.length(target_pos - current_pos) -- <6>
        local duration = distance / self.speed -- <7>
        go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, target_pos, go.EASING_LINEAR, duration, 0) -- <8>
    end
end

--[[
1. The speed of the game object in pixels/second
2. Tell the engine that this game object ("." is shorthand for the current game object) should listen to input. Any input will be received in the `on_input()` function.
3. Check if we received mouse movement (no action id) or an input action named "touch" (touch or mouse click)
4. Get the current position of the game object.
5. Set the target position to the position of the mouse or touch.
6. Calculate the distance (length) between the current and target position.
7. Calculate the time it takes to travel the distance given the speed of the game object.
8. Animate the game object's ("." is shorthand for the current game object) position to `target_pos`.
--]]

This code works great right up until I add a camera object with the following script:

function init(self)
	msg.post("#camera", "acquire_camera_focus") -- <1>
	msg.post("@render:", "use_camera_projection") -- <2>
end

Once I add this, the cursor just flies off screen.

I need the camera because my pixel art is very small (16x16).

What am I missing?

Thanks

Once you have an active camera you need to account for the camera projection when using mouse coordinates. When using a camera the lower left corner of the screen may no longer be at pixel coordinates 0,0. You need to do screen to world coordinate conversion using the camera projection, like this:

2 Likes

This was the answer. Thank you.

How would you convert screen to GUI coordinates?

I have something in my render script and utility function that works but is defintely not a matrix operation :sweat_smile: