How to get the center of the screen?
Asked Answered
C

12

0

What seems to be of a simple task has proven thus to be the cause of the greatest lasting war in centuries, Me and GUI centering problems.
I have done every tutorial that exists out there, but they have either proven to produce broken hacky results or outdated.
But now, I have reached the conclusion that all I must do is to find the center of the screen, so I gladly ask for help from the ever-growing Godot Community.

I am using Godot 3, by the way.

Caroylncarp answered 20/3, 2023 at 3:29 Comment(0)
C
0

If you are making a GUI with Control Nodes, then you don't find the center. You're not allowed to manually position objects. However, you can use Controls like a VBox or HBox or CenterBox and use the layout controls to center objects (or move them to the corners, etc.) The options are on the top above the viewport, it looks like a square.

Celestecelestia answered 20/3, 2023 at 3:56 Comment(0)
P
0

And then he solves it in 5 seconds after that and facepalms 😃 been there.

Plumage answered 20/3, 2023 at 7:59 Comment(0)
P
0

Caroylncarp I have done every tutorial that exists out there

wwaaaooooo !! Amazing ! I guess you're crazy as hell to inflict so much pain to yourself 🙂

It would be great to have a definitive guide to UI and containers in Godot 4 because documentation is far too light on this and devs made so much changes to this part. In fact, it's quite a complete mess and anchors behaviour with content of a container works very weirdly, for instance a NinePatchRect does not auto adjust its size despite its content (another container) is bigger, whereas it looks so easy when everything is set-up correctly like in this GDQuest vdeo.

Projectionist answered 20/3, 2023 at 9:5 Comment(0)
S
0

In a 2D project in Godot 3.5.2, I use this to get the center as a Vector2:
get_viewport_rect().size / 2.0

It's only used for positioning my player, who remains at the center.

Sedulous answered 20/3, 2023 at 10:3 Comment(0)
C
0

I forgot to mention that I am using Godot 3, so I have now updated my post to include that.

Caroylncarp answered 20/3, 2023 at 19:33 Comment(0)
C
0

Sedulous How are you able to use "get_viewport_rect()", it always returns "isn't declared in current class"?

Caroylncarp answered 20/3, 2023 at 19:45 Comment(0)
S
0

What is your exact Godot version? There are many releases that start with "3".

Sedulous answered 20/3, 2023 at 19:50 Comment(0)
C
0

Sedulous Godot_v3.5.1

Caroylncarp answered 20/3, 2023 at 20:20 Comment(0)
S
0

Caroylncarp How are you able to use "get_viewport_rect()", it always returns "isn't declared in current class"?

It's a method of CanvasItem ("Base class of anything 2D"), so it should work in any script that's attached to a node that inherits that class, which includes anything that's displayable as 2D, which a GUI would normally be.
https://docs.godotengine.org/en/3.5/classes/class_canvasitem.html?highlight=get_viewport_rect#class-canvasitem-method-get-viewport-rect

Sedulous answered 20/3, 2023 at 22:11 Comment(0)
C
0

Projectionist This is why I am only gonna switch to Godot 4 for a new game project after I finish my current game, and by the time that happens, Godot 4 will hopefully be as fleshed out as Godot 3.

Sedulous Ah, now it works, but it produces really weird results which, for your information, appear the same across both windows and mobile devices (I tested this).
Anchors and stuff didn't do anything to fix this. Here are screenshots of what I am talking about (the center node is supposed to be that big placeholder astronaut character. The darkened astronaut is the player and that other darkened astronaut is just a player duplicate that I was just fooling around with):
This was the code I used for it (I took the "get_viewport_center" function from someone else's answer since it seemed to work better than just the plain "get_viewport_rect().size / 2" which returned results that were out of the screen):

func _process(_delta):
	rect_position = _get_viewport_center()

func _get_viewport_center() -> Vector2:
	var transform : Transform2D = get_viewport_transform()
	var scale : Vector2 = transform.get_scale()
	return -transform.origin / scale + get_viewport_rect().size / scale / 2

And now to wait for answers.

Caroylncarp answered 21/3, 2023 at 0:29 Comment(0)
C
0

Celestecelestia I am now starting to wonder if there is anything in project settings that could be the possible source of this whole mess...

Caroylncarp answered 24/3, 2023 at 23:59 Comment(0)
C
0

You might want to read this:

https://docs.godotengine.org/en/stable/tutorials/rendering/multiple_resolutions.html

Celestecelestia answered 25/3, 2023 at 2:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.