introduction_to_2d.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. .. _doc_introduction_to_2d:
  2. Introduction to 2D
  3. ==================
  4. Godot's 2D game development tools include a dedicated 2D rendering engine, physics system,
  5. and features tailored specifically for creating 2D experiences. You can efficiently design
  6. levels with the TileMap system, animate characters with 2D sprite or Cutout animation,
  7. and leverage 2D lighting for dynamic scene illumination. The built-in 2D particle system
  8. allows you to create complex visual effects, and Godot also supports custom shaders to
  9. enhance your graphics. These features, combined with Godot's accessibility and
  10. flexibility, provide a solid foundation for creating engaging 2D games.
  11. .. figure:: img/2d_platformer_demo.webp
  12. 2D Platformer Demo available on the Asset Library.
  13. This page will show you the 2D workspace and how you can get to know it.
  14. .. tip:: If you would like to get an introduction to 3D, see :ref:`doc_introduction_to_3d`.
  15. 2D workspace
  16. ~~~~~~~~~~~~
  17. You will use the 2D workspace to work with 2D scenes, design levels, or create user
  18. interfaces.
  19. To switch to the 2D workspace, you can either select a 2D node from the scene tree,
  20. or use the workspace selector located at the top edge of the editor:
  21. .. image:: img/2d_editor_viewport.webp
  22. Similar to 3D, you can use the tabs below the workspace selector to change between currently
  23. opened scenes or create a new one using the plus (+) button. The left and right docks should
  24. be familiar from :ref:`editor introduction <toc-editor-interface>`.
  25. Below the scene selector is the main toolbar, and beneath the main toolbar
  26. is the 2D viewport.
  27. You can drag and drop compatible nodes from the FileSystem dock to add them to the
  28. viewport as nodes.
  29. Dragging and dropping adds the dragged node as a sibling of the selected node
  30. (if the root node is selected, adds as a child).
  31. Keeping :kbd:`Shift` pressed when dropping adds the node as a child of the selected node.
  32. Holding :kbd:`Alt` when dropping adds the node as a child of the root node.
  33. If :kbd:`Alt + Shift` is held when dropping, the node type can be selected if
  34. applicable.
  35. Main toolbar
  36. ------------
  37. Some buttons in the main toolbar are the same as those in the 3D workspace. A brief explanation
  38. is given with the shortcut if the mouse cursor is hovered over a button for one second.
  39. Some buttons may have additional functionality if another keypress is performed.
  40. A recap of main functionality of each button with its default shortcut is provided below
  41. from left to right:
  42. .. image:: img/2d_toolbar.webp
  43. - **Select Mode** (:kbd:`Q`): Allows selection of nodes in the viewport. Left clicking on a node
  44. in the viewport selects it.
  45. Left clicking and dragging a rectangle selects all nodes within the rectangle's boundaries,
  46. once released.
  47. Holding :kbd:`Shift` while selecting adds more nodes to the selection.
  48. Clicking on a selected node while holding :kbd:`Shift` deselects the node.
  49. In this mode, you can drag the selected node(s) to move, press :kbd:`Ctrl` to switch to the
  50. rotation mode temporarily, or use the red circles to scale it. If multiple nodes are
  51. selected, only movement and rotation are possible. In this mode, rotation and scaling
  52. will not use the snapping options if snapping is enabled.
  53. - **Move Mode** (:kbd:`W`): Enables move (or translate) mode for the selected nodes. See
  54. :ref:`doc_introduction_to_2d_the_viewport` for more details.
  55. - **Rotate Mode** (:kbd:`E`): Enables rotation mode for the selected nodes. See
  56. :ref:`doc_introduction_to_2d_the_viewport` for more details.
  57. - **Scale Mode** (:kbd:`R`): Enables scaling and displays scaling gizmos in both
  58. axes for the selected node(s). See :ref:`doc_introduction_to_2d_the_viewport` for more details.
  59. - **Show list of selectable nodes at position clicked**: As the description suggests,
  60. this provides a list of selectable nodes at the clicked position as a context menu, if
  61. there is more than one node in the clicked area.
  62. - **Rotation pivot**: Sets the rotation pivot to rotate node(s) around.
  63. An added node has its rotation pivot at ``x: 0``, ``y: 0``, by default, with
  64. exceptions. For example, the default pivot for a :ref:`Sprite2D <class_Sprite2D>` is its
  65. center if the ``centered`` property is set to ``true``. If you would like to change the
  66. rotation pivot of a node, click this button and choose a new location by left clicking.
  67. The node rotates considering this point. If you have multiple nodes selected, this icon
  68. will add a temporary pivot to be used commonly by all selected nodes. Pressing :kbd:`Shift`
  69. and clicking this button will create the pivot at the center of selected nodes. If any of
  70. the snap options are enabled, the pivot will also snap to them it when dragged.
  71. - **Pan Mode** (:kbd:`G`): Allows you to navigate in the viewport without accidentally selecting any nodes.
  72. In other modes, you can also hold :kbd:`Space` and drag with the left mouse button to do the same.
  73. - **Ruler Mode**: After enabling, click on the viewport to display the current global
  74. x and y coordinates. Dragging from a position to another one measures the distance in pixels.
  75. If you drag diagonally, it will draw a triangle and show the separate distances in terms
  76. of x, y, and total distance to the target, including the angles to the axes in degrees.
  77. The :kbd:`R` key also activates the ruler. If snapping is enabled, it also displays the
  78. measurements in terms of grid count:
  79. .. figure:: img/2d_ruler_with_snap.webp
  80. Using ruler with snapping enabled.
  81. - **Use Smart Snap**: Toggles smart snapping for move, rotate, and scale modes; and
  82. the rotation pivot. Customize it using the three-dot menu next to the snap tools.
  83. - **Use Grid Snap**: Toggles snapping to grid for move and scale mode, rotation pivot,
  84. and the ruler. Customize it using the three-dot menu next to the snap tools.
  85. You can customize the grid settings so that move mode, rotate mode, scale mode, ruler,
  86. and rotation pivot uses snapping.
  87. Use the three-dot menu for this:
  88. .. image:: img/2d_snapping_options_menu.webp
  89. - **Use Rotation Snap**: Toggles snapping using the configured rotation setting.
  90. - **Use Scale Snap**: Toggles snapping using the configured scaling step setting.
  91. - **Snap Relative**: Toggles the usage of snapping based on the selected node's current
  92. transform values. For example, if the grids are set to 32x32 pixels and if the selected node
  93. is located at ``x: 1, y: 1``, then, enabling this option will temporarily shift the grids by
  94. ``x: 1, y: 1``.
  95. - **Use Pixel Snap**: Toggles the use of subpixels for snapping. If enabled, the position values
  96. will be integers, disabling will enable subpixel movement as decimal values. For the runtime
  97. property, consider checking `Project Settings > Rendering > 2D > Snapping` property for
  98. Node2D nodes, and `Project Settings > GUI > General > Snap Controls to Pixels` for
  99. Control nodes.
  100. - **Smart Snapping**: Provides a set of options to snap to specific positions if they are enabled:
  101. - Snap to Parent: Snaps to parent's edges. For example, scaling a child control node while
  102. this is enabled will snap to the boundaries of the parent.
  103. - Snap to Node Anchor: Snaps to the node's anchor. For example, if anchors of a control
  104. node is positioned at different positions, enabling this will snap to the sides and
  105. corners of the anchor.
  106. - Snap to Node Sides: Snaps to the node's sides, such as for the rotation pivot or anchor
  107. positioning.
  108. - Snap to Node Center: Snaps to the node's center, such as for the rotation pivot or
  109. anchor positioning.
  110. - Snap to Other Nodes: Snaps to other nodes while moving or scaling. Useful to align nodes
  111. in the editor.
  112. - Snap to Guides: Snaps to custom guides drawn using the horizontal or vertical ruler. More
  113. on the ruler and guides below.
  114. .. image:: img/2d_snapping_options.webp
  115. - **Configure Snap**: Opens the window shown above, offering a set of snapping parameters.
  116. - Grid Offset: Allows you to shift grids with respect to the origin. ``x`` and ``y`` can
  117. be adjusted separately.
  118. - Grid Step: The distance between each grid in pixels. ``x`` and ``y`` can be adjusted separately.
  119. - Primary Line Every: The number of grids in-between to draw infinite lines as indication of
  120. main lines.
  121. - Rotation Offset: Sets the offset to shift rotational snapping.
  122. - Rotation Step: Defines the snapping degree. E.g., 15 means the node will rotate and snap
  123. at multiples of 15 degrees if rotation snap is enabled and the rotate mode is used.
  124. - Scale Step: Determines the scaling increment factor. For example, if it is 0.1, it will
  125. change the scaling at 0.1 steps if scaling snap is enabled and the scaling mode is used.
  126. - **Lock selected nodes** (:kbd:`Ctrl + L`). Locks the selected nodes, preventing selection and movement in the
  127. viewport. Clicking the button again (or using :kbd:`Ctrl + Shift + L`) unlocks the selected
  128. nodes. Locked nodes can only be selected in the scene tree.
  129. They can easily be identified by a padlock next to their node names in the scene tree.
  130. Clicking on this padlock also unlocks the nodes.
  131. - **Group selected nodes** (:kbd:`Ctrl + G`). This allows selection of the root node if any
  132. of the children are selected. Using :kbd:`Ctrl + G` ungroups them. Additionally, clicking
  133. the ungroup button in the scene tree performs the same action.
  134. - **Skeleton Options**: Provides options to work with Skeleton2D and Bone2D.
  135. - Show Bones: Toggles the visibility of bones for the selected node.
  136. - Make Bone2D Node(s) from Node(s): Converts selected node(s) into Bone2D.
  137. .. seealso:: To learn more about Skeletons, see :ref:`doc_cutout_animation`.
  138. - **Project Camera Override**: Temporarily replaces the active camera in the level
  139. (e.g., the camera following the player) with the camera in the editor's viewport, allowing
  140. you to move freely and inspect the level's different parts, while the game is running.
  141. - **View** menu: Provides options to control the viewport view. Since its options
  142. depend heavily on the viewport, it is covered in the :ref:`doc_introduction_to_2d_the_viewport`
  143. section.
  144. Next to the View menu, additional buttons may be visible. In the toolbar image
  145. at the beginning of this chapter, an additional *Sprite2D* button appears because a
  146. Sprite2D is selected. This menu provides some quick actions and tools to
  147. work on a specific node or selection. For example, while drawing a polygon, it
  148. provides buttons to add, modify, or remove points.
  149. Coordinate system
  150. -----------------
  151. In the 2D editor, unlike 3D, there are only two axes: ``x`` and ``y``. Also, the viewing
  152. angle is fixed.
  153. In the viewport, you will see two lines in two colors going across the screen infinitely:
  154. red for the x-axis, and green for the y-axis.
  155. In Godot, going right and down are positive directions.
  156. Where these two lines intersect is the origin: ``x: 0, y: 0``.
  157. A root node will have its origin at this position once added.
  158. Switching to the `move` or `scale` modes after selecting a node will display the gizmos at the
  159. node's offset position.
  160. The gizmos will point to the positive directions of the x and y axes.
  161. In the move mode, you can drag the green line to move only in the ``y`` axis.
  162. Similarly, you can hold the red line to move only in the ``x`` axis.
  163. In the scale mode, the gizmos will have a square shape. You can hold and drag the green and
  164. red squares to scale the nodes in the ``y`` or ``x`` axes.
  165. Dragging in a negative direction flips the node horizontally or vertically.
  166. .. _doc_introduction_to_2d_the_viewport:
  167. 2D Viewport
  168. -----------
  169. The viewport will be the area you spend the most time if you plan to design levels or user
  170. interfaces visually:
  171. .. image:: img/2d_editor_viewport_with_viewmenu.webp
  172. Middle-clicking and dragging the mouse will pan the view.
  173. The scrollbars on the right or bottom of the viewport also move the view.
  174. Alternatively, the :kbd:`G` or :kbd:`Space` keys can be used.
  175. If you enable `Editor Settings > Editors > Panning > Simple Panning`, you can activate
  176. panning directly with :kbd:`Space` only, without requiring dragging.
  177. The viewport has buttons on the top-left.
  178. **Center View** centers the selected node(s) in the screen. Useful if you have a large scene
  179. with many nodes, and want to see the node selected in the scene tree.
  180. Next to it are the zoom controls. **-** zooms out, **+** zooms in, and clicking on the number
  181. with percentage defaults to 100%.
  182. Alternatively, you can use middle-mouse scrolling to zoom in (scroll up) and out (scroll down).
  183. The black bars at the viewport's left and top edges are the **rulers**. You can use them to
  184. orient yourself in the viewport.
  185. By default, the rulers will display the pixel coordinates of the viewport, numbered at
  186. 100 pixel steps. Changing the zoom factor will change the shown values.
  187. Enabling `Grid Snap` or changing the snapping options will update the ruler's scaling and
  188. the shown values.
  189. You can also create multiple custom guides to help you make measurements or align
  190. nodes with them:
  191. .. image:: img/2d_editor_guidelines.webp
  192. If you have at least one node in the scene, you can create guides by dragging from the horizontal
  193. or vertical ruler towards the viewport. A purple guide will appear, showing its position, and will
  194. remain there when you release the mouse. You can create both horizontal and vertical guides
  195. simultaneously by dragging from the gray square at the rulers' intersection. Guides can be
  196. repositioned by dragging them back to their respective rulers, and they can be removed by
  197. dragging them all the way back to the ruler.
  198. You can also enable snapping to the created guides using the `Smart Snap` menu.
  199. .. note:: If you cannot create a line, or do not see previously created guides, make sure that
  200. they are visible by checking the `View` menu of the viewport. :kbd:`Y` toggles their visibility,
  201. by default. Also, make sure you have at least one node in the scene.
  202. Depending on the tool chosen in the toolbar, left-clicking will have a primary action in the
  203. viewport.
  204. For example, the `Select Mode` will select the left-clicked node in the viewport.
  205. Sometimes, left-clicking can be combined with a modifier (e.g., :kbd:`Ctrl`, or :kbd:`Shift`) to
  206. perform secondary actions.
  207. For example, keeping :kbd:`Shift` pressed while dragging a node in the Select or Move modes will
  208. try to snap the node in a single axis while moving.
  209. Right clicking in the viewport provides two options to create a node or instantiate a scene
  210. at the chosen position.
  211. If at least one node is selected, right clicking also provides the option to move the selected
  212. node(s) to this position.
  213. Viewport has a **View** menu which provides several options to change the look of the viewport:
  214. - **Grid**: Allows you to show grids all the time, only when using snapping, or not at all. You
  215. can also toggle them with the provided option.
  216. - **Show Helpers**: Toggles the temporary display of an outline of the node, with the previous
  217. transform properties (position, scaling, or rotation) if a transform operation has been
  218. initiated. For `Control` nodes, it also shows the sizing parameters. Useful to see the deltas.
  219. - **Show Rulers**: Toggles the visibility of horizontal and vertical rulers. See
  220. :ref:`doc_introduction_to_2d_the_viewport` more on rulers.
  221. - **Show Guides**: Toggles the visibility of created guides. See
  222. :ref:`doc_introduction_to_2d_the_viewport` for on how to create them.
  223. - **Show Origin**: Toggles the display of the green and red origin lines drawn at ``x: 0, y: 0``.
  224. - **Show Viewport**: Toggles the visibility of the game's default
  225. viewport, indicated by an indigo-colored rectangle. It is also the default window size on desktop
  226. platforms, which can be changed by going to `Project Settings > Display > Window > Size` and
  227. setting `Viewport Width` and `Viewport Height`.
  228. - **Gizmos**: Toggles the visibility of `Position` (shown with cross icon), `Lock`
  229. (shown with padlock), `Groups` (shown with two squares), and `Transformation` (shown with
  230. green and red lines) indicators.
  231. - **Center Selection**: The same as the **Center View** button inside the viewport. Centers the selected
  232. node(s) in the view. :kbd:`F` is the default shortcut.
  233. - **Frame to Selection**: Similar to `Center Selection`, but also changes the zoom factor to fit the
  234. contents in the screen. :kbd:`Shift + F` is the default shortcut.
  235. - **Clear Guides**: Deletes all guides from the screen. You will need to recreate them if
  236. you plan to use them later.
  237. - **Preview Canvas Scale**: Toggles the preview for scaling of canvas in the editor when the zoom
  238. factor or view of the viewport changes. Useful to see how the controls will look like after scaling
  239. and moving, without running the game.
  240. - **Preview Theme**: Allows to choose from the available themes to change the look of control items
  241. in the editor, without requiring to run the game.
  242. Node2D and Control node
  243. ~~~~~~~~~~~~~~~~~~~~~~~
  244. :ref:`CanvasItem <class_CanvasItem>` is the base node for 2D. :ref:`Node2D <class_Node2D>` is the base node
  245. for 2D game objects, and :ref:`Control <class_Control>` is the base node
  246. for everything GUI. For 3D, Godot uses the :ref:`Node3D <class_Node3D>` node.
  247. 3D in 2D
  248. --------
  249. It is possible to display 3D scenes in 2D screen. This is achieved by adding a
  250. :ref:`SubViewport <class_SubViewport>` as a child.
  251. Then, you can drag a 3D scene as a child of the SubViewport:
  252. .. image:: img/3d_in_2d_demo_editor.webp
  253. .. seealso:: You can check the demo on: `3D in 2D Viewport demo <https://godotengine.org/asset-library/asset/2804>`__.