Open greatOmouse opened 2 months ago
I'm not sure if I'm doing something wrong, please let me know.
You're mixing many different coordinate spaces. See Viewport and canvas transforms. Or, linked in there, a more detailed example in 2D coordinate systems and 2D transforms. Also Custom drawing in 2D (as CanvasItem.draw_x
methods work in the local coordinate system of the given CanvasItem).
Specifically:
More or less fixed MouseInput.gd
from the MRP:
extends Node2D
var mouse_position_root_window = Vector2.ZERO;
func _process(delta: float) -> void:
# Both should yield same-ish result:
mouse_position_root_window = DisplayServer.mouse_get_position() - get_tree().root.position
#mouse_position_root_window = get_viewport().get_screen_transform() * get_viewport().get_mouse_position()
var mouse_event = InputEventMouseMotion.new()
mouse_event.position = mouse_position_root_window
Input.parse_input_event(mouse_event)
queue_redraw()
func _draw():
var local_to_root_window: Transform2D = get_viewport().get_screen_transform() * get_global_transform_with_canvas()
var root_window_to_local: Transform2D = local_to_root_window.affine_inverse()
draw_circle(root_window_to_local * mouse_position_root_window, 20.0, Color.ANTIQUE_WHITE)
So I don't see any bug in here. But not closing the issue as seems like some docs can be improved further, e.g. Input.parse_input_event
.
Ah I understand now, thank you for the clarification.
Tested versions
System information
Windows 11 - Godot Engine v4.3.stable.mono.official
Issue description
This seems to have been fixed, but it may only have been for 2D? For a 3D project I had to use the manual position update when I move the camera to get mouse enter and exit to trigger: https://github.com/godotengine/godot/issues/69708
doing this I seem to get the same additional bug with 2D or 3D resulting from a few steps. So, when calling something like this to manually update the mouse position:
Mouse Enter & Mouse Exit Signals will fire on an object at a different location to the mouse position if the viewport has been scaled and the stretch mode is set to canvas_items.
i.e.
get_viewport().get_mouse_position()
seems to be accurately produced, and you can draw things to the screen or make things follow to that mouse location and it works and produces the correct location and output, but somewhere in the processing of handling the simulated mouse input above, it will seemingly use a different or incorrect position value (maybe due to scale).This causes other objects with mouse enter and exit signals to trigger when the mouse is not over them, or flickering to occur on the object you are mousing over.
I'm not sure if I'm doing something wrong, please let me know. Or in any case if there is a good workaround, please let me know.
Steps to reproduce
var mousePos = Vector2.ZERO;
Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void: var mouse_event = InputEventMouseMotion.new() mouse_event.position = get_viewport().get_mouse_position() Input.parse_input_event(mouse_event)
func _draw(): draw_circle(mousePos,20.0,Color.ANTIQUE_WHITE) pass
https://github.com/user-attachments/assets/14fccba0-d70b-475d-9e46-5899931a7f77
Minimal reproduction project (MRP)
ScalingMouseEnter_MRP.zip