Federico-Ciuffardi/GestureControlledCamera2D

Camera Limits

Closed this issue · 5 comments

First of all thanks for this great assett.

How can i set limits for the camera so it doesnt leave the scene.
I tried using the limits properties of camera2d in the editor, but it seems these are only working if the camera position is changed not the offset.

Any help would be much appreciated.
Cheers

after some research i found this godotengine/godot#30361
i changed all the offset property assignments to position and that did the trick.

I'm glad you could solve it, I had no idea about this.

I think I misinterpreted the meaning of offset.

If you can, make a pull request with the changes you made.

Otherwise tell me and I will make them myself, no problem.

i changed these lines (replaced offset with position)

func move(event):
position -= (event.relative*zoom).rotated(rotation)

func zoom(event):
...
position -= (from_camera_center_pos*zd).rotated(rotation)

func rotate(event):
...
position -= ((fccp_op_rot + fccp)*zoom).rotated(rotation-event.relative)

Fixed on the latest release v1.0.1

EDIT: fix/tweak my code

Hi, I had some trouble with the limits (3.5 years after this issue was discussed 😅). I changed the following:

Original

	if anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT:
		position_max_limit = Vector2(limit_right, limit_bottom) - camera_size
		position_min_limit = Vector2(limit_left , limit_top)
	elif anchor_mode ==  ANCHOR_MODE_DRAG_CENTER:
		position_max_limit = Vector2(limit_right, limit_bottom) - camera_size/2
		position_min_limit = Vector2(limit_left , limit_top) + camera_size  # added /2

Fix:

	if anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT:
		position_max_limit = Vector2(limit_right, limit_bottom) - camera_size
		position_min_limit = Vector2(limit_left , limit_top)
	elif anchor_mode ==  ANCHOR_MODE_DRAG_CENTER:
		position_max_limit = Vector2(limit_right, limit_bottom) - camera_size/2
		position_min_limit = Vector2(limit_left , limit_top)  + camera_size/2

I don't have an PR for this as I didn't tested it thoroughly enough, e.g. with rotations or mobile, but maybe someone would find this useful in the meantime.

Here's a demo of the fix (the big circles are on the camera limits):

GCC2D.maybe.fix.mp4