Action.x and action.y off by half a pixel

An odd new issue. The coordinates I’m getting from action.x and action.y are offset by half a pixel. +0.5 X and -0.5 Y.

Defold 1.2.183, Linux Mint 20.1 Ulyssa.

Reported on github: action.x and action.y are offset by half a pixel. · Issue #5925 · defold/defold · GitHub

Can someone else test this project to see if they get the same results? (without resizing the window of course.)
mouse-pos-test.zip (2.7 KB)
screenshot_2021-07-09_10-57-42
“gui pos” is action.x, action.y, “screen pos” is action.screen_x, action.screen_y.

Not only that, but also this:

And these are not new issues, this has been the case from the very beginning.

1 Like

MacOS Big Sur, Defold 1.2.184 on a retina Macbook Pro. How strange!

1 Like

Thank you for reporting and reminding us of these. The upside now is that the source code is available and accepting pull requests! :wink:

3 Likes

Thanks! I guess with a high-dpi screen the coordinates are halved, right?
If you double that gui pos, you get: 409.5, 638.5
Which is exactly the +0.5 X and -0.5 Y from screen pos that I have.

It’s really mind-boggling that I haven’t noticed this before. I thought I’d done the conversion between them dozens of times. I guess I usually only use screen coordinates outside of GUI because they don’t get stretched.

So, I tried looking at the source code to figure this out and this is what I found:

The 0.5 offset seems very intentional. I would like to see the reason of that explained, though I assume it’s to make a pixel coordinate centered.

The y coordinates being in [1, window_height] and not in [0, window_height - 1] as expected is likely due to off-by-one error in this line:

So, the simplest fix is

        input_action.m_ScreenY = window_height - 1 - action->m_Y;

Keep in mind though, that this is my first time looking at Defold source code, so I may be completely wrong :slight_smile: I also don’t have the knowledge to build and test the engine, so I can’t confirm the proposed fix actually works.

Correct. According to the very old commit it was due to picking:

You’re right. That’s probably it.