Input actionX and actionY while resizing the game window

Hi,
Please help me understanding the concept behind this.
I have set the sprite at some location on game window with use_fixed_fit_projection and register for input events .
On touching the sprite I am getting

actionX: 204.5 window_size_x: 640

after resizing the window, and touching same sprite I am getting following:

actionX: 266.37240600586 window_size_x: 1450

while setting sprites to position or go.get_position are synchronised with previous coordinates even after resizing the window.
but input coordinates are updating as per new window but within game set width and height but are scaling.

How do I get the input coordinates as if the game coordinates?
is following calculation correct ?

actionX / ((window_size_x - game.width) / game.width)

or any other better way

sorry for confusing way to write my query
hope you understand it.

Thanks

It’s a little more complicated than that, as you have to take into account the view/projection.

If you use an extension like https://github.com/britzl/defold-orthographic/ there are APIs to handle converting from screen space to world space, e.g. the screen_to_world function.

1 Like

Thanks for pointing to beautful code,
I read that and I found, as I am not using camera and making only 2D game
so for me
following works:

-- screen values
local SV = {

    game = {
        width = tonumber(sys.get_config("display.width")) or 640,
        height = tonumber(sys.get_config("display.height")) or 1136
    },

    screen = {
        width = tonumber(sys.get_config("display.width")) or 640,
        height = tonumber(sys.get_config("display.height")) or 1136,
    },

    scaling_factor = 1
}

function window_callback(self, event, data)
    if event == window.WINDOW_EVENT_RESIZED then
        SV.screen.width, SV.screen.height = data.width, data.height

        -- If screen_aspect_ratio > game_aspect_ratio
        -- game_plane will match the screen_plane's height
        if SV.screen.width / SV.screen.height > SV.game.width / SV.game.height then
            SV.scaling_factor = SV.screen.height / SV.game.height
        else
            SV.scaling_factor = SV.screen.width / SV.game.width
        end
    end
end

function actionScreenToGame(action)

    action.x = (action.screen_x - (SV.screen.width - SV.game.width * SV.scaling_factor) / 2) / SV.scaling_factor
    action.y = (action.screen_y - (SV.screen.height - SV.game.height * SV.scaling_factor) / 2) / SV.scaling_factor

    return action
end

in init function

 local screen_w, screen_h = window.get_size()
    window_callback(self, window.WINDOW_EVENT_RESIZED , {width = screen_w, height = screen_h})
    window.set_listener(window_callback)

wherever required

action = actionScreenToGame(action)
4 Likes

Very interesting indeed. I was struggling with that kind of problem for few moments… Thanks!

You can also link to the orthographic camera https://github.com/britzl/defold-orthographic

and then use the appropriate:

Screen coordinates

This refers to the actual mouse pixel position within the window, scaled to the display size specified in game.project. These are the values from action.x and action.y in on_input().

Window coordinates

This refers to the actual mouse pixel position within the window. These are the values from action.screen_x and action.screen_y in on_input(). Window coordinates should be provided as is, without compensation for High DPI (this will be done automatically).

1 Like