r/godot•
Orphaeus

Get screen in global coords? get_visible_rect() not working?

Help

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.

My scene tree

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:

​

camera.gd

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

36
kleoncProgrammer•

Probably that's what you're looking for:

var ci: CanvasItem = ... # any CanvasItem in the viewport you're interested in
var visibleRectGlobal: Rect2 = ci.get_viewport_transform().affine_inverse().xform(ci.get_viewport_rect())
1
OrphaeusOP•

What am I looking at here? And do you know where the flaw in my reasoning was?

1
kleoncProgrammer•

How to obtain viewport rect in global coordinate space of the given CanvasItem (some Node2D/Control). A little more verbosely:

var ci: CanvasItem = ... # any CanvasItem in the viewport you're interested in
var viewportRect: Rect2 = ci.get_viewport_rect()
var globalToViewportTransform: Transform2D = ci.get_viewport_transform()
var viewportToGlobalTransform: Transform2D = globalToViewportTransform.affine_inverse()
var viewportRectGlobal: Rect2 = viewportToGlobalTransform.xform(viewportRect)
1
OrphaeusOP•

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?

1
kleoncProgrammer•

So what does get_visible_rect() do then? It sounds like it should be a shortcut to accomplishing this, but apparently it isn't?

get_visible_rect returns viewport's rect in its own coordinate space. So yeah, its position is always (0, 0).

​

Edit: And why do you need the inverse transform?

As described here get_viewport_transform returns 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):

# again verbose version
var v: Viewport = ...
var viewportRect: Rect2 = v.get_visible_rect()
var globalToViewportTransform: Transform2D = v.get_final_transform() * v.canvas_transform
var viewportToGlobalTransform: Transform2D = globalToViewportTransform.affine_inverse()
var viewportRectGlobal: Rect2 = viewportToGlobalTransform.xform(viewportRect)

Like I said in the OP there's clearly something I don't understand here. Can you explain how this works please?

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:

1