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_local_mouse_position does not work under certain circumstances #85705

Closed
mogoh opened this issue Dec 3, 2023 · 5 comments · Fixed by #86841
Closed

get_local_mouse_position does not work under certain circumstances #85705

mogoh opened this issue Dec 3, 2023 · 5 comments · Fixed by #86841

Comments

@mogoh
Copy link

mogoh commented Dec 3, 2023

Godot version

Godot v4.2.stable (46dc277)

System information

Godot v4.2.stable (46dc277) - Freedesktop SDK 23.08 (Flatpak runtime) - Wayland - Vulkan (Forward+) - dedicated AMD Radeon RX 5500 XT (RADV NAVI14) () - AMD Ryzen 5 5600X 6-Core Processor (12 Threads)

Issue description

If:

  • Godot 4.2
  • A scene transition happened first
  • Click on a TileMap
  • That TileMap has a TileSet
  • That TileSet has an Atlas

Then:

  • get_local_mouse_position is calculated wrong

More specific:

Under the said circumstances, to get_local_mouse_position the negated offset_left and negated offset_top is added which adds up to the wrong result.

However, this does not happen if the TileMap has not TileSet or the TileSet has not Atlas or if the scene is loaded directly.
The behaviour should be consistent.
If the TopLeft corner of a TileMap is clicked, get_local_mouse_position should return (0, 0).

Steps to reproduce

See the issue description or the minimal reproduction project.

Minimal reproduction project

get_local_mouse_position-test.zip

@kleonc
Copy link
Member

kleonc commented Dec 3, 2023

Indeed seems like TileMap's global transform ends up in incorrect state. Recalculating it manually gives correct result which means it must be an issue with not marking it as dirty or something like that.

In the MRP:

var gt := Transform2D()
var ci: CanvasItem = tile_map
while ci:
	gt = ci.get_transform() * gt
	ci = ci.get_parent() as CanvasItem
prints(tile_map.global_position, gt.origin)
  • In v4.1.3.stable.official [f06b6836a] it correctly outputs (-284, -116) (-284, -116).
  • In v4.2.stable.official [46dc277] it outputs (-860, -440) (-284, -116).

@mogoh
Copy link
Author

mogoh commented Dec 3, 2023

Thank you for confirming the Issue.
I think this specific commit introduced the bug: 8c1e282
I checked out the one before and it did not had the problem.

@mogoh
Copy link
Author

mogoh commented Dec 4, 2023

@groud Do you have an idea, where this issue might come from?
As you are the author of 8c1e282 , you might have an idea, what the root cause might be.
I don't want to put you under any pressure and I hope it is ok to ping you.

@groud
Copy link
Member

groud commented Dec 4, 2023

Hmm. Might be due to some errors in computing the global position indeed. The TileMap node has some logic to handle animated movement and sync it with physics. I don't know what would cause it exactly though, I might need to investigate.

@mogoh
Copy link
Author

mogoh commented Dec 4, 2023

This is my workaround for now:

extends TileMap

func get_local_mouse_position_() -> Vector2:
	var gt: Transform2D = Transform2D()
	var ci: CanvasItem = self
	while ci != null:
		gt = ci.get_transform() * gt
		ci = ci.get_parent() as CanvasItem
	return get_global_mouse_position() - gt.origin

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

Successfully merging a pull request may close this issue.

5 participants