Get screen in global coords? get_visible_rect() not working?
Hi, I'm having an issue getting the coordinates of the screen's rect. I need to know where its bounds are relative to the game world, and I thought that get_viewport().get_visible_rect() would do the trick. However, the Rect2 returned by that method is always at position == (0, 0), even though there's a Camera2D following a player scene. I've dug through the docs on viewports and cameras and I can't figure out why this would be.
Player is the player controller. All the input handling is decoupled from the actual physics body because the core mechanic of the game revolves around continuously switching which entity you're controlling. In this tree, the last node under Player (blueship) is the currently controlled ship. The camera is given a reference to the current player ship and then lerps its position to follow it:
The call to get_viewport().get_visible_rect() happens in player.gd. It's the first thing that happens in its `_process()` method, if it's in the relevant state.
I'm not sure what else would be relevant here, but I'd be happy to post anything else you need to see to solve this. I can't understand why "Return the visible rectangle in global screen coordinates" (Viewport's get_visible_rect()) produces a Rect2 that seems to be totally unrelated to the screen position. To be clear, it appears to have the same dimensions as the screen, but it is definitely not at the screen's position. It doesn't even start there, it starts at the global origin, making it offset from the screen to begin with.
I assume there's something I don't understand about how cameras work. Thanks in advance for any advice you can give!
Posted Mar 19, 2021, 5:03 PM UTC
Probably that's what you're looking for:
What am I looking at here? And do you know where the flaw in my reasoning was?
How to obtain viewport rect in global coordinate space of the given CanvasItem (some Node2D/Control). A little more verbosely:
So what does
get_visible_rect()do then? It sounds like it should be a shortcut to accomplishing this, but apparently it isn't?Edit: And why do you need the inverse transform? Like I said in the OP there's clearly something I don't understand here. Can you explain how this works please?
get_visible_rectreturns viewport's rect in its own coordinate space. So yeah, its position is always (0, 0).
As described here
get_viewport_transformreturns a transform transforming from the given CanvasItem's global coordinate space to screen coordinate space. You want to transform viewport rect the other way around thus you need an inverse of that transform.
Using only a Viewport you can transform its rect to the default CanvasLayer coordinate space like this (but note that if some CanvasItem is in some other CanvasLayer then it most likely has different global coordinate space):
That's pretty much all you need to know. However when in doubt and docs are not clear for me I just check what given method does by looking at the source code. Here you can find source code for nodes for 3.2.3-stable version. For example source code for: