Get camera extents rect? (2D)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By keke
:warning: Old Version Published before Godot 3 was released.

How can I get the (global) positions of the limits of what a camera can see? (I can get a reference to the camera) I want to account for rotation, position, and scaling. I’m having a hard time with all this canvas transform stuff…

I’ve had this problem before, in my game I need to spawn custom particles under the view rectangle. I’ll see if I can post it once I get home.

Zylann | 2017-04-06 18:20

@avencherus Thanks for the detailed response! I tried to limit the scope of my question by specifying that I wanted a 2d solution. I’m not trying to check the camera constraints against objects - just points. I’m trying to avoid _drawing lines I don’t have to - not making sprites or meshes invisible. I think I’ll try that Area2D idea for now - even if it’s a bit hacky…

@ Zylann That would be helpful! Thanks!

keke | 2017-04-09 21:21

This is how I get the min and max points of the view rectangle in 2D:

# Get view rectangle
var ctrans = get_canvas_transform()
var min_pos = -ctrans.get_origin() / ctrans.get_scale()
var view_size = get_viewport_rect().size / ctrans.get_scale()
var max_pos = min_pos + view_size

Probably not a general solution but it works for me^^

Zylann | 2017-04-10 00:04

Thank you! It works. I wish I properly understood it though. If you submit this as an answer, I’ll accept it. Maybe with a brief explanation…?

keke | 2017-04-20 04:44

I got with the code posted above these 2 errors:

E 0:00:01.018   get_canvas_transform: Condition "!is_inside_tree()" is
true. Returned: Transform2D()   <C++ Source> 
scene/2d/canvas_item.cpp:1262 @ get_canvas_transform()   <Stack Trace>
Ground.gd:9 @ _init()

E 0:00:01.018   get_canvas_transform: Condition "!is_inside_tree()" is
true. Returned: Transform2D()   <C++ Source> 
scene/2d/canvas_item.cpp:1262 @ get_canvas_transform()   <Stack Trace>
Ground.gd:9 @ _init()

BlockOG | 2020-12-03 19:06

The errors are because a Node2D/CanvasItems doesn’t have these transforms available until they’re inside the tree. I suppose it might make sense if they at least initialized to 0.

You can either execute the code some time you know when the node is inside the tree, or guard it with and if statement if(is_inside_tree()):.

avencherus | 2020-12-05 01:34

:bust_in_silhouette: Reply From: Zylann

This is how I get the min and max points of the view rectangle in 2D:

# Get the canvas transform
var ctrans = get_canvas_transform()

# The canvas transform applies to everything drawn,
# so scrolling right offsets things to the left, hence the '-' to get the world position.
# Same with zoom so we divide by the scale.
var min_pos = -ctrans.get_origin() / ctrans.get_scale()
# The maximum edge is obtained by adding the rectangle size.
# Because it's a size it's only affected by zoom, so divide by scale too.
var view_size = get_viewport_rect().size / ctrans.get_scale()
var max_pos = min_pos + view_size

# Note: rotation is not taken into account here. An improvement would be
# to use the inverse transform instead of this.

Probably not a general solution but it works for me^^