Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

2D 坐标系与 2D 变换

前言

This is a detailed overview of the available 2D coordinate systems and 2D transforms that are built in. The basic concepts are covered in 视口变换与画布变换.

Transform2D are matrices that convert coordinates from one coordinate system to an other. In order to use them, it is beneficial to know which coordinate systems are available in Godot. For a deeper understanding, the 矩阵与变换 tutorial offers insights to the underlying functionality.

Godot 2D 坐标系

The following graphic gives an overview of Godot 2D coordinate systems and the available node-transforms, transform-functions and coordinate-system related functions. At the left is the OS Window Manager screen, at the right are the CanvasItems. For simplicity reasons this graphic doesn't include SubViewport, SubViewportContainer, ParallaxLayer and ParallaxBackground all of which also influence transforms.

The graphic is based on a node tree of the following form: Root Window (embed Windows)Window (don't embed Windows)CanvasLayerCanvasItemCanvasItemCanvasItem. There are more complex combinations possible, like deeply nested Window and SubViewports, however this example intends to provide an overview of the methodology in general.

../../../_images/transforms_overview.webp

单击图片放大。

  • Item 坐标

    这是 CanvasItem 的局部坐标系。

  • 父 Item 坐标

    This is the local coordinate system of the parent's CanvasItem. When positioning CanvasItems in the Canvas, they usually inherit the transformations of their parent CanvasItems. An exceptions is CanvasItems.top_level.

  • 画布坐标

    As mentioned in the previous tutorial 画布层, there are two types of canvases (Viewport canvas and CanvasLayer canvas) and both have a canvas coordinate system. These are also called world coordinates. A Viewport can contain multiple Canvases with different coordinate systems.

  • 视口坐标

    This is the coordinate system of the Viewport.

  • 相机坐标

    This is only used internally for functionality like 3D-camera ray projections.

    <