|
| 1 | +.. _doc_2d_coordinate_systems: |
| 2 | + |
| 3 | +2D coordinate systems and 2D transforms |
| 4 | +======================================= |
| 5 | + |
| 6 | +Introduction |
| 7 | +------------ |
| 8 | + |
| 9 | +This is a detailed overview of the available 2D coordinate systems and 2D transforms that are |
| 10 | +built in. The basic concepts are covered in :ref:`doc_viewport_and_canvas_transforms`. |
| 11 | + |
| 12 | +:ref:`Transform2D <class_Transform2D>` are matrices that convert coordinates from one coordinate |
| 13 | +system to an other. In order to use them, it is beneficial to know which coordinate systems are |
| 14 | +available in Godot. For a deeper understanding, the :ref:`doc_matrices_and_transforms` tutorial |
| 15 | +offers insights to the underlying functionality. |
| 16 | + |
| 17 | +Godot 2D coordinate systems |
| 18 | +--------------------------- |
| 19 | + |
| 20 | +The following graphic gives an overview of Godot 2D coordinate systems and the available |
| 21 | +node-transforms, transform-functions and coordinate-system related functions. At the left |
| 22 | +is the OS Window Manager screen, at the right are the :ref:`CanvasItems <class_CanvasItem>`. For |
| 23 | +simplicity reasons this graphic doesn't include :ref:`SubViewport <class_SubViewport>`, |
| 24 | +:ref:`SubViewportContainer <class_SubViewportContainer>`, :ref:`ParallaxLayer<class_ParallaxLayer>` |
| 25 | +and :ref:`ParallaxBackground<class_ParallaxBackground>` all of which also influence transforms. |
| 26 | + |
| 27 | +The graphic is based on a node tree of the following form: ``Root Window (embed Windows)`` ⇒ |
| 28 | +``Window (don't embed Windows)`` ⇒ ``CanvasLayer`` ⇒ ``CanvasItem`` ⇒ ``CanvasItem`` ⇒ |
| 29 | +``CanvasItem``. There are more complex combinations possible, like deeply nested Window and |
| 30 | +SubViewports, however this example intends to provide an overview of the methodology in general. |
| 31 | + |
| 32 | +.. image:: img/transforms_overview.webp |
| 33 | + :target: ../../../_images/transforms_overview.webp |
| 34 | + |
| 35 | +Click graphic to enlarge. |
| 36 | + |
| 37 | +- **Item Coordinates** |
| 38 | + This is the local coordinate system of a :ref:`CanvasItem <class_CanvasItem>`. |
| 39 | + |
| 40 | +- **Parent Item Coordinates** |
| 41 | + This is the local coordinate system of the parent's *CanvasItem*. When positioning |
| 42 | + *CanvasItems* in the *Canvas*, they usually inherit the transformations of their parent |
| 43 | + *CanvasItems*. An exceptions is |
| 44 | + :ref:`CanvasItems.top_level <class_CanvasItem_property_top_level>`. |
| 45 | + |
| 46 | +- **Canvas Coordinates** |
| 47 | + As mentioned in the previous tutorial :ref:`doc_canvas_layers`, there are two types of canvases |
| 48 | + (*Viewport* canvas and *CanvasLayer* canvas) and both have a canvas coordinate system. These |
| 49 | + are also called world coordinates. A *Viewport* can contain multiple *Canvases* with different |
| 50 | + coordinate systems. |
| 51 | + |
| 52 | +- **Viewport Coordinates** |
| 53 | + This is the coordinate system of the :ref:`Viewport <class_Viewport>`. |
| 54 | + |
| 55 | +- **Camera Coordinates** |
| 56 | + This is only used internally for functionality like 3D-camera ray projections. |
| 57 | + |
| 58 | +- **Embedder Coordinates / Screen Coordinates** |
| 59 | + Every *Viewport* (*Window* or *SubViewport*) in the scene tree is embedded either in a |
| 60 | + different node or in the OS Window Manager. This coordinate system's origin is identical to the |
| 61 | + top-left corner of the *Window* or *SubViewport* and its scale is the one of the embedder or |
| 62 | + the OS Window Manager. |
| 63 | + |
| 64 | + If the embedder is the OS Window Manager, then they are also called Screen Coordinates. |
| 65 | + |
| 66 | +- **Absolute Embedder Coordinates / Absolute Screen Coordinates** |
| 67 | + The origin of this coordinate system is the top-left corner of the embedding node or the OS |
| 68 | + Window Manager screen. Its scale is the one of the embedder or the OS Window Manager. |
| 69 | + |
| 70 | + If the embedder is the OS Window Manager, then they are also called Absolute Screen |
| 71 | + Coordinates. |
| 72 | + |
| 73 | + |
| 74 | +Node transforms |
| 75 | +--------------- |
| 76 | + |
| 77 | +Each of the mentioned nodes have one or more transforms associated with them and the combination of |
| 78 | +these nodes infer the transforms between the different coordinate systems. With a few exceptions, |
| 79 | +the transforms are :ref:`Transform2D <class_Transform2D>` and the following list shows details and |
| 80 | +effects of each of them. |
| 81 | + |
| 82 | +- **CanvasItem transform** |
| 83 | + *CanvasItems* are either *Control*-nodes or *Node2D*-nodes. |
| 84 | + |
| 85 | + For *Control* nodes this transform consists of a :ref:`position <class_Control_property_position>` |
| 86 | + relative to the parent's origin and a :ref:`scale <class_Control_property_scale>` and |
| 87 | + :ref:`rotation <class_Control_property_rotation>` around a |
| 88 | + :ref:`pivot point <class_Control_property_pivot_offset>`. |
| 89 | + |
| 90 | + For *Node2D* nodes :ref:`transform <class_Node2D_property_transform>` consists of |
| 91 | + :ref:`position <class_Node2D_property_position>`, :ref:`rotation <class_Node2D_property_rotation>`, |
| 92 | + :ref:`scale <class_Node2D_property_scale>` and :ref:`skew <class_Node2D_property_skew>`. |
| 93 | + |
| 94 | + The transform affects the item itself and usually also child-*CanvasItems* and in the case of a |
| 95 | + *SubViewportContainer* it affects the contained *SubViewport*. |
| 96 | + |
| 97 | +- **CanvasLayer transform** |
| 98 | + The *CanvasLayer's* :ref:`transform <class_CanvasLayer_property_transform>` affects all |
| 99 | + *CanvasItems* within the *CanvasLayer*. It doesn't affect other *CanvasLayers* or *Windows* in |
| 100 | + its *Viewport*. |
| 101 | + |
| 102 | +- **CanvasLayer follow viewport transform** |
| 103 | + The *follow viewport transform* is an automatically calculated transform, that is based on the |
| 104 | + *Viewport's* :ref:`canvas transform <class_Viewport_property_canvas_transform>` and the |
| 105 | + *CanvasLayer's* :ref:`follow viewport scale <class_CanvasLayer_property_follow_viewport_scale>` |
| 106 | + and can be used, if :ref:`enabled <class_CanvasLayer_property_follow_viewport_enabled>`, to |
| 107 | + achieve a pseudo 3D effect. It affects the same child nodes as the *CanvasLayer transform*. |
| 108 | + |
| 109 | +- **Viewport canvas transform** |
| 110 | + The :ref:`canvas transform <class_Viewport_property_canvas_transform>` affects all |
| 111 | + *CanvasItems* in the *Viewport's* default canvas. It also affects *CanvasLayers*, that have |
| 112 | + follow viewport transform enabled. The *Viewport's* active :ref:`Camera2D <class_Camera2D>` |
| 113 | + works by changing this transform. It doesn't affect this *Viewport's* embedded *Windows*. |
| 114 | + |
| 115 | +- **Viewport global canvas transform** |
| 116 | + *Viewports* also have a :ref:`global canvas transform <class_Viewport_property_global_canvas_transform>`. |
| 117 | + This is the master transform and affects all individual *Canvas Layer* and embedded *Window* |
| 118 | + transforms. This is primarily used in Godot's CanvasItem Editor. |
| 119 | + |
| 120 | +- **Viewport stretch transform** |
| 121 | + Finally, *Viewports* have a *stretch transform*, which is used when resizing or stretching the |
| 122 | + viewport. This transform is used for :ref:`Windows <class_Window>` as described in |
| 123 | + :ref:`doc_multiple_resolutions`, but can also be manually set on *SubViewports* by means of |
| 124 | + :ref:`size <class_SubViewport_property_size>` and |
| 125 | + :ref:`size_2d_override <class_SubViewport_property_size_2d_override>`. It's |
| 126 | + :ref:`translation <class_Transform2D_method_get_origin>`, |
| 127 | + :ref:`rotation <class_Transform2D_method_get_rotation>` and |
| 128 | + :ref:`skew <class_Transform2D_method_get_skew>` are the default values and it can only have |
| 129 | + non-default :ref:`scale <class_Transform2D_method_get_scale>`. |
| 130 | + |
| 131 | +- **Window transform** |
| 132 | + In order to scale and position the *Window's* content as described in |
| 133 | + :ref:`doc_multiple_resolutions`, each :ref:`Window <class_Window>` contains a |
| 134 | + *window transform*. It is for example responsible for the black bars at the *Window's* sides so |
| 135 | + that the *Viewport* is displayed with a fixed aspect ratio. |
| 136 | + |
| 137 | +- **Window position** |
| 138 | + Every *Window* also has a :ref:`position <class_Window_property_position>` to describe its |
| 139 | + position within its embedder. The embedder can be another *Viewport* or the OS Window Manager. |
| 140 | + |
| 141 | +- **SubViewportContainer shrink transform** |
| 142 | + :ref:`stretch <class_SubViewportContainer_property_stretch>` together with |
| 143 | + :ref:`stretch_shrink <class_SubViewportContainer_property_stretch_shrink>` declare for a |
| 144 | + *SubViewportContaner* if and by what integer factor the contained *SubViewport* should be |
| 145 | + scaled in comparison to the container's size. |
0 commit comments