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

Tilemap in a nested viewport in a project with scaling enabled, offsets the position of the mouse #32222

Open
db0 opened this issue Sep 20, 2019 · 8 comments

Comments

@db0
Copy link

db0 commented Sep 20, 2019

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

@KoBeWi
Copy link
Member

KoBeWi commented Sep 20, 2019

Isn't it the same as #30950?

@db0
Copy link
Author

db0 commented Sep 20, 2019

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.

@db0
Copy link
Author

db0 commented Sep 20, 2019

It does seem to be similar to #30215 though

db0 pushed a commit to db0/haldric that referenced this issue Sep 20, 2019
To avoid getting hit by godotengine/godot#32222 I had to add a workaround

Also adapted the function comments according to the standards
@db0
Copy link
Author

db0 commented Sep 20, 2019

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

@stolencatkarma
Copy link

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 )

@db0
Copy link
Author

db0 commented Oct 14, 2019

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

@LLBlumire
Copy link

Thank you so much for this workaround!

@Sauermann
Copy link
Contributor

I tried to replicate this issue in v4.0.beta.custom_build [bb5b45e], but it looks like this was fixed along the way.
Still present however in v3.5.1.stable.official [6fed1ff].

@KoBeWi KoBeWi added this to the 3.x milestone Nov 7, 2022
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

5 participants