Godot: Tilemap in a nested viewport in a project with scaling enabled, offsets the position of the mouse

Created on 20 Sep 2019  路  7Comments  路  Source: godotengine/godot

Godot version:

3.1.1

OS/device including version:

PC/Fedora Linux 30 (but seems to affect everything)

Issue description:

When you have a project where scaling is enabled and you have a tilemap within a viewport (i.e. not in the root viewport), the methods to return the mouse position start getting offset by a variable amount. The offset increases the further the mouse travels to the bottom-right. The offset also changes depending on the viewport zoom level.

Steps to reproduce:

  • Project where scaling is enabled
  • Create a 'Node2d > Viewport Container > Viewport > Tilemap' node tree
  • Create code to display the position of the mouse using get_local_mouse_position() as well as the current cell position.

You will see that the location of the mouse cursor, will not correspond to the cell your mouse is actually over. It's easier to see if you put a sprite to display the cell.

If you move the tilemap to have Node2D as the parent, the position will start working correctly. If you disable scaling in the project properties, it will also fix this issue.

I have also tried get_global_mouse_position() and others positioning options.
I also tried multiplying with various Transform2D objects during testing with no success.

Minimal reproduction project:

https://drive.google.com/file/d/1zUo4lsBz5Bn7MsgRhJcuCBg26qHxMJur/view?usp=sharing

bug input

Most helpful comment

Looks like my workaround stops working when the zoom level of the nested viewport changes. I need to see if I can tweak it further by taking the zoom level into account manually.

EDIT: Workaround for the above

var zoom = get_parent().get_parent().get_node("Camera2D").zoom
offset_position = get_tree().current_scene.get_global_mouse_position() - get_viewport_transform().origin
offset_position *= zoom

All 7 comments

Isn't it the same as #30950?

Not sure, this affects only nested viewports (using the root viewport works fine, unlike in that bug report) and the suggested workaround in the comments doesn't seem to work for me.

It does seem to be similar to #30215 though

I can confirm that the workaround mentioned in #30215 works for me as well

Putting the following code as the mouse position, fixes the variable offset. Although I do believe that get_local_mouse_position should always report the true mouse position autoscaled to the viewport.

get_tree().current_scene.get_global_mouse_position() - get_viewport_transform().origin

i can attest to this as well. my work around I believe is different to others.

var mousepos = .get_node("path/to/tilemap").get_global_mouse_position()
var coordpos = .get_node("path/to/tilemap").world_to_map( mousepos )

Looks like my workaround stops working when the zoom level of the nested viewport changes. I need to see if I can tweak it further by taking the zoom level into account manually.

EDIT: Workaround for the above

var zoom = get_parent().get_parent().get_node("Camera2D").zoom
offset_position = get_tree().current_scene.get_global_mouse_position() - get_viewport_transform().origin
offset_position *= zoom

Thank you so much for this workaround!

Was this page helpful?
0 / 5 - 0 ratings