0 votes

Scene Structure
Viewport in action

I have a sub-viewport with a 2D scene instanced as a child. On this scene I draw a grid and I want to be able to click inside of the grid and place a sprite on the tile that I clicked on.
To achieve this I use following code:

func _on_Preview_gui_input(p_event):

    if p_event.is_action_pressed("Mouse_Left"):
        var mouse_pos = a_preview_scene.get_global_mouse_position()
        var point = _pos_to_grid_point(mouse_pos)

        if a_point_vec != point || a_point == null:

This works fine as long as the window size doesn't change. I have stretch_mode 2D and aspect keep in the project settings, and if I resize the window to be wider or higher CanvasItem.get_global_mouse_position() starts to give me wrong values. (Note: In this case the size of the root viewport is still the default value, only black bars appear).

This offset caused by the black bars can be easily fixed by doing:

var root = get_tree().get_root()
var bars = (OS.window_size - root.size) / 2
mouse_pos -= bars

I believe you also have to take the viewport stretch into account if Viewport.size_override_stretch is active.
(By calculating the factor sub_viewport.size_override / sub_viewport.size)

But if I now happen to resize the root viewports size, either by making the window size smaller or by switching to full screen (The sub-viewport size stays the same), everything breaks apart and I'm left with a coordinate I have no idea how to make use of. And even if that worked, changing properties of the current camera 2D would break it too.

So I'm asking, how do I always get the correct global mouse coordinates inside a sub-viewport?

Minimal Reproduction Project: https://drive.google.com/drive/folders/1nE083OXXZd5RiV99HIwMIUe5D0zDrJx3?usp=sharing

Godot version v3.4.3.stable.official [242c05d12]
in Engine by (66 points)
edited by

did You try using Viewport.get_mouse_position() ?

That gives you local viewport screen coordinates, with the top-left of the viewport being 0,0 and the bottom-right viewport.size.
I want global world coordinates that take a Camera2D into account.

Can you show the question with the help of a picture?

I included two pictures in the original post, but I couldn't get them to show a preview. Anyways here are the links to the two pictures:

In the first one you can see the scene structure, but you just have to have a viewport besides the root viewport to encounter the issue.
In the second one you can see the viewport in action (rendering a 2D scene with a grid). If I click inside of the grid I want to change the global position of a Sprite2D to be the global mouse position in the world. But as soon as you resize the window the coordinates start to be off.

I will also include a minimal showcase project so you can see it for yourself.

I understood the question, if I'm not mistaken, I've seen similar questions, I don't know the answer, but I think this will help you


Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.