From cf59d74b15a7375144534fc091badab708a35439 Mon Sep 17 00:00:00 2001 From: Nessa Teal Date: Wed, 21 May 2025 21:22:06 +0300 Subject: [PATCH] Expose Camera2D current rotation --- doc/classes/Camera2D.xml | 9 ++++++++- scene/2d/camera_2d.cpp | 5 +++++ scene/2d/camera_2d.h | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/classes/Camera2D.xml b/doc/classes/Camera2D.xml index 714288de845..b0c5d1e86c7 100644 --- a/doc/classes/Camera2D.xml +++ b/doc/classes/Camera2D.xml @@ -7,7 +7,7 @@ Camera node for 2D scenes. It forces the screen (current layer) to scroll following this node. This makes it easier (and faster) to program scrollable scenes than manually changing the position of [CanvasItem]-based nodes. Cameras register themselves in the nearest [Viewport] node (when ascending the tree). Only one camera can be active per viewport. If no viewport is available ascending the tree, the camera will register in the global viewport. This node is intended to be a simple helper to get things going quickly, but more functionality may be desired to change how the camera works. To make your own custom camera node, inherit it from [Node2D] and change the transform of the canvas by setting [member Viewport.canvas_transform] in [Viewport] (you can obtain the current [Viewport] by using [method Node.get_viewport]). - Note that the [Camera2D] node's [code]position[/code] doesn't represent the actual position of the screen, which may differ due to applied smoothing or limits. You can use [method get_screen_center_position] to get the real position. + Note that the [Camera2D] node's [member Node2D.global_position] doesn't represent the actual position of the screen, which may differ due to applied smoothing or limits. You can use [method get_screen_center_position] to get the real position. Same for the node's [member Node2D.global_rotation] which may be different due to applied rotation smoothing. You can use [method get_screen_rotation] to get the current rotation of the screen. https://godotengine.org/asset-library/asset/2727 @@ -47,6 +47,13 @@ [b]Note:[/b] The exact targeted position of the camera may be different. See [method get_target_position]. + + + + Returns the current screen rotation from this camera's point of view. + [b]Note:[/b] The screen rotation can be different from [member Node2D.global_rotation] if the camera is rotating smoothly due to [member rotation_smoothing_enabled]. + + diff --git a/scene/2d/camera_2d.cpp b/scene/2d/camera_2d.cpp index b2bb56cf70e..0f18d1b8fff 100644 --- a/scene/2d/camera_2d.cpp +++ b/scene/2d/camera_2d.cpp @@ -800,6 +800,10 @@ Point2 Camera2D::get_camera_screen_center() const { return camera_screen_center; } +real_t Camera2D::get_screen_rotation() const { + return camera_angle; +} + Size2 Camera2D::_get_camera_screen_size() const { if (is_part_of_edited_scene()) { return Size2(GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_width"), GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_height")); @@ -996,6 +1000,7 @@ void Camera2D::_bind_methods() { ClassDB::bind_method(D_METHOD("get_target_position"), &Camera2D::get_camera_position); ClassDB::bind_method(D_METHOD("get_screen_center_position"), &Camera2D::get_camera_screen_center); + ClassDB::bind_method(D_METHOD("get_screen_rotation"), &Camera2D::get_screen_rotation); ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &Camera2D::set_zoom); ClassDB::bind_method(D_METHOD("get_zoom"), &Camera2D::get_zoom); diff --git a/scene/2d/camera_2d.h b/scene/2d/camera_2d.h index 70de9614caf..ae52e4e200c 100644 --- a/scene/2d/camera_2d.h +++ b/scene/2d/camera_2d.h @@ -202,6 +202,7 @@ public: Vector2 get_zoom() const; Point2 get_camera_screen_center() const; + real_t get_screen_rotation() const; void set_custom_viewport(Node *p_viewport); Node *get_custom_viewport() const;