Click-area shifting on different aspect ratios

Godot Version

3.3

Question

Hi everyone,

a Sprite (tree-oder: Node2D/Control/Node2D/Sprite) has this method:

func _input(event):

# second condition tests whether the click occured within the area of the sprite

if Input.is_action_just_pressed("mousebuttonclick") && get_rect().has_point(to_local(event.position)):

click_pos = event.position

This works fine, so tapping on the Sprite registers my finger right on position (touch screen on phone…).

The problem is that with changing the window’s aspect ratio (dynamically on desktop or when loading up on different phones) the registering tapping area shifts off of the Sprite’s position.

I have this auto-load script:

extends Node

onready var viewport = get_viewport()

onready var game_size = Vector2(

ProjectSettings.get_setting("display/window/size/width"),

ProjectSettings.get_setting("display/window/size/height"))

func _ready():

viewport.connect("size_changed", self, "resize_viewport")

resize_viewport()

func resize_viewport():

var new_size = OS.get_window_size()

var scale_factor

if new_size.x < game_size.x:

scale_factor = game_size.x/new_size.x

new_size = Vector2(new_size.x*scale_factor, new_size.y*scale_factor)

if new_size.y < game_size.y:

scale_factor = game_size.y/new_size.y

new_size = Vector2(new_size.x*scale_factor, new_size.y*scale_factor)

viewport.set_size_override(true, new_size)

Settings/General/Dispay/Window/Stretch Mode is “2d”, Aspect is “ignore”.

This way instead of black letter-/pillar-boxes, background-graphics from outside the original viewport fill the added areas.

I tried all the combinations of stretch-mode and -aspect (with and without the autoload-script).

Mode “viewport” with Aspect "keep"shows letterboxes, but the tapping area-position is correct (with the autoload-script turned off).

I’m at a loss, there must be a way to get both the position on spot and no letterboxes, right? Why does the tap-area shift away in the first place? Shouldn’t “get_rect().has_point(to_local(event.position))” just stay in place no matter the screen-size?
Any help is much appreciated.

In Godot 3 there are several aspects regarding coordinate systems, that are not handled correctly.
If it is feasible for you, could you try, if that use-case works in Godot 4?

Oh my, I intended to finish the project before switching to Godot 4 in order to avoid any issues. Now that you suggest switching though, I assume it’s possible to have 3 and 4 side by side (keeping 3 as is), right? “Installing” 4 wouldn’t risk proper functionality of 3, would it? I’m just really paranoid in that regard, obviously… but if 4 could solve the issue it might be worth a try. Thanks for your suggestion.
EDIT: GLES2 is a must in my project, so no Godot 4 for me yet ; )

To answer your question, you don’t install godot, you just run the exe file. And having two godot exe files, 4, and 3 won’t affect each other.

1 Like