Skip to content

Clarify what "global" means in the context of mouse position in different coordinate systems #3866

Description

@madmiraal

Describe the project you are working on

Godot engine

Describe the problem or limitation you are having in your project

The word "global" implies that there is one "true" coordinate system that everything else is relative to. But there are at least five coordinate systems:

  • World
  • Canvas Layer
  • Viewport
  • Window
  • Screen

And, there can be multiples of each of these.

Godot receives mouse positions in window coordinates and it needs to translate these into the appropriate position in the appropriate coordinate system. When referring to position especially global_position in properties and methods, it's often unclear which coordinate system this position is or even should be in.

When working in 3D it's easy to notice the difference between world coordinates and viewport coordinates, because one is in 3D and one is in 2D. However, until one adds a camera to a 2D game, the difference between world coordinates, canvas layer coordinates and viewport coordinates is not obvious, because they're all the same. The current best explanation for the difference between all of these in the documentation is: Viewport and canvas transforms. However, it adds to the confusion by identifying two types of global canvas transforms:
viewport_transforms2

As already described in #2139, adding viewports is confusing. However, even without adding additional worlds, canvas layers, viewports or even a camera, the user is expected to understand the difference between viewport coordinates and window coordinates (which persistently remains a source of confusion see #2822, #3769 and godotengine/godot#47522).

Clarifying the difference between viewport coordinates and window coordinates is required before we can even fix issues like godotengine/godot#25023 and godotengine/godot#28665.

In addition, before we can fix issues like godotengine/godot#30950, get_mouse_position() and warp_mouse(position) need to refer to the same coordinate system, but which one? Viewport or Window?

Furthermore, when working with additional canvas layers or viewports i.e. there is a difference between the coordinate systems, even the conversion between the location of the mouse pointer and the position in the world fails (see godotengine/godot#30215 and godotengine/godot#35965).

However, before we can fix issues like godotengine/godot/issues/35965, we need to understand whether CanvasItem.get_global_mouse_position() should return the "global" mouse position in the the canvas layer or the viewport (see godotengine/godot#35965 (comment)). Similarly, just as InputEventMouse position changes, not only depending on which node is interested in the event, but whether the it's received in the _input(event), _gui_event(event) or _unhandled_event(event) callback, should the global_position change to reflect the appropriate coordinate system i.e. the (root) viewport or canvas layer? Note: It currently returns the mouse's position in the window coordinates, which is practically useless.

Moreover, before we can fix issues like godotengine/godot#30215, we need to be able to answer questions like godotengine/godot#30215 (comment):

I have a question regarding the behaviour of get_global_mouse_position.
Suppose we have two sub-windows of the Godot Editor open at a time, say Project Settings (viewport V1) and Editor Settings (viewport V2). At any instance of time, if we call V1.get_global_mouse_position() and V2.get_global_mouse_position(). Should they give the same result (ideally)?

These questions are all pre-requisites to fixing problems like godotengine/godot#48368 and godotengine/godot#20619, which is actually a reopening of godotengine/godot#1383, which was closed by disabling the functionality in e997c0d with the comment:

it' s better to just disable selection of stuff inside a viewport, as it' s too difficult to figure out what' s going on inside.

Finally, as identified in godotengine/godot#38444, even the definition of position in Control and Node2D is inconsistent. And the difference between a global point and a local point shouldn't require the current lengthy explanations for to_global and to_local, but addressing these issues is beyond the scope of this proposal.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Ensure that anything referring to position; especially global_position uses the correct coordinate system. Just like Node2D and Node3D positions refer to their position in world coordinates, and CanvasItems' position refers to their position in CanvasLayer coordinates, any reference to position or global_position should be in the same coordinate system as the caller.

Specifically:

More generally, when providing or returning a mouse's position it should never be in window (never mind screen) coordinates. Mappings between the game world and the mouse should always be via the Viewport's coordinates.

Finally, we need to make a point of not using the word "screen" when we mean "window", or "window" when we mean "viewport". This includes doing a better job of explaining the difference between Viewport coordinates and window coordinates and how stretch modes work, because the current documentation is failing to do this (see #2479). This will be especially important for 4.0 since we now have a new Window class that derives from Viewport.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

N/A

If this enhancement will not be used often, can it be worked around with a few lines of script?

N/A

Is there a reason why this should be core and not an add-on in the asset library?

N/A

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions