+4 votes

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...

in Engine by (147 points)

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.

@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!

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^^

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..?

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()

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()):.

1 Answer

+7 votes

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^^

by (27,593 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.