Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_global_mouse_position() is shifted in the viewport's transform #30215

Closed
ghsoares opened this issue Jun 30, 2019 · 12 comments
Closed

get_global_mouse_position() is shifted in the viewport's transform #30215

ghsoares opened this issue Jun 30, 2019 · 12 comments

Comments

@ghsoares
Copy link

Godot version:
3.1.1 stable oficial

OS/device including version:
Win 10

Issue description:
When I use the get_global_mouse_position() in a normal scene, the coordinates is right, but when I use in a scene that uses a viewport container to render pixelated the scene, the coordinates is inacurated.

Steps to reproduce:
-Create a scene that draws a circle of the global mouse position (to_local() to make it a local position).

-Put this scene in other scene that render with a viewport container (the child viewport node has the same game display size).

@KoBeWi
Copy link
Member

KoBeWi commented Jul 1, 2019

Related (if not duplicate) to #29570

@giulianob
Copy link
Contributor

@KoBeWi I am experiencing the same issue but I am not attaching the viewport to the screen like in the issue you mentioned. Calling get_global_mouse_position() on a node inside of the viewport is giving some really wrong results. In my case, I have a node2d with a camera and I am calling get_global_mouse_position() on the node2d.

To get the correct mouse position and to offset the camera I had to do this: stage.GetGlobalMousePosition() - player.GetViewport().CanvasTransform.origin where stage is my root node (outside of the viewport) and player is my node inside of the viewport.

BTW everything was working then I simply moved my level to be inside of a ViewportContainer/Viewport and then the inputs were off.

@ghsoares
Copy link
Author

@KoBeWi This issue is talking exclusively about the inputs in _input(event) and Controls inside the Sub-Viewports and my issue is about the mouse position in the scene inside the Sub-Viewports.

@giulianob Yes, i'm calling get_global_mouse_position() from a node that's outside the Sub-Viewport, and as the coordinate is global, it works for nodes inside the Sub-Viewport. But if a camera2d, that's the current camera2d inside the Sub-Viewport, changes the zoom, the zoom is not applied in the get_global_mouse_position().

And yes, the Sub-Viewport nullify any _input(event) and controls inside it, I needed to connect (via signals) the _input(event) from a singletone to every node inside the Sub-Viewport

So, there's somethings that need to be changed in future update I think.

@KoBeWi
Copy link
Member

KoBeWi commented Aug 25, 2020

Can anyone still reproduce this bug in Godot 3.2.3 rc4 or any later release?

If yes, please ensure that an up-to-date Minimal Reproduction Project (MRP) is included in this report (a MRP is a zipped Godot project with the minimal elements necessary to reliably trigger the bug). You can upload ZIP files in an issue comment with a drag and drop.

@JohnEmhoff
Copy link

Attached is a project that reproduces this on 3.2.3.stable.official -- it draw a circle centered on get_global_mouse_position(), and you can see clearly that it's very off. Only happens when the node is inside the Viewport node.

scale-bug.zip

@amoriqbal
Copy link
Contributor

I have a question regarding the behaviour of get_global_mouse_position.
Suppose we have two sub-windows of the Godot Editor open at a time, say Project Settings (viewport V1) and Editor Settings (viewport V2). At any instance of time, if we call V1.get_global_mouse_position() and V2.get_global_mouse_position(). Should they give the same result (ideally)?

@JohnEmhoff
Copy link

Are you asking to clarify or getting feedback for how the feature should work? If the latter, it seems to me that they should return the same world coordinate. I interpret this function as a way to answer the question, "Where in the game world is the mouse pointing?" but I'm not sure if that's correct -- the documentation isn't very clear: https://docs.godotengine.org/en/stable/classes/class_canvasitem.html#class-canvasitem-method-get-global-mouse-position

@amoriqbal
Copy link
Contributor

Are you asking to clarify or getting feedback for how the feature should work? If the latter, it seems to me that they should return the same world coordinate. I interpret this function as a way to answer the question, "Where in the game world is the mouse pointing?" but I'm not sure if that's correct -- the documentation isn't very clear: https://docs.godotengine.org/en/stable/classes/class_canvasitem.html#class-canvasitem-method-get-global-mouse-position

Indeed the docs are not clear. I am asking to clarify. It is because the internal code depends on that function at many points. And all such parts, that are placed in a different Window from the main Godot Editor window, are breaking down in the master branch. For example, the Project Settings and the Editor Settings. After you drag a spin slider or a numeric field, the mouse reappearance position is determined by this function. The position is shifted,depending on the position of the Settings window on the screen.

Presently, ** this -> get_global_mouse_position() calculates the global mouse position wrt the origin of this viewport **. My question is: Is that the intended functionality of the function? Or does it need to be fixed?

@KoBeWi
Copy link
Member

KoBeWi commented May 24, 2021

Global mouse position should be relative to viewport origin. Moving window shouldn't affect it.

Not sure about SubViewports. If users use SubViewportContainer, they'd probably expect that their mouse position is in screen space, i.e. relative to the topmost viewport. But maybe it's fine if it wasn't, as long as it's documented.

@ACB-prgm
Copy link

Global mouse position should be relative to viewport origin. Moving window shouldn't affect it.

For me in 3.3.2, resizing the window affects it if I am using a subviewport.

Not sure about SubViewports. If users use SubViewportContainer, they'd probably expect that their mouse position is in screen space, i.e. relative to the topmost viewport. But maybe it's fine if it wasn't, as long as it's documented.

Is there a way to get the mouse position relative to the screen rather than the viewport?

My case is a little odd, I am making a 2D game that is displayed on a viewport in a 3D scene with the viewport projected on a mesh instance tilted like 25º in order to give it a 2.5D perspective. Sorry if that didn't make sense. Using get_viewport().get_mouse_position does not work on either the 2D scene or the 3D scene which is the root. Oddly, the get_global_mouse_position works perfectly when the window size is at the project resolution, but not if I resize the window. Going to look into the stretch settings right now and see if that has something to do with it. otherwise, I will try and come up with a formula to adjust the Vector based upon the screen size

@KoBeWi
Copy link
Member

KoBeWi commented May 27, 2021

Is there a way to get the mouse position relative to the screen rather than the viewport?

If you mean screen as "global, including area outside game window", you can try using OS.window_position and some viewport maths.

@KoBeWi
Copy link
Member

KoBeWi commented May 2, 2022

Seems fixed in 4.0 alpha7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants